Ver Mensaje Individual
  #11 (permalink)  
Antiguo 05/07/2011, 11:03
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: se pierde la transparencia del png cuando redimensiono

Como que no te funciona

Acuérdate de indicar en la clase el segundo parametro, que indica que quieres que sea transparente.

Trata de esta forma para que veas que funciona
Código PHP:
Ver original
  1. <?php
  2. /**
  3.  * File: ResizePicture.php
  4.  * Author: Simon Jarvis
  5.  * Copyright: Simon Jarvis
  6.  * Date: 08/11/06
  7.  * Original link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8.  *
  9.  * Modified Date: 05/14/11
  10.  */
  11.  
  12. class ResizePicture {
  13.     private $_image;
  14.     private $_imageType;
  15.     private $_transparent;
  16.  
  17.     public function __construct($fileName=null, $transparent=false)
  18.     {
  19.         $this->setTransparent($transparent);
  20.  
  21.         if(!is_null($fileName)){
  22.             $this->load($fileName);
  23.         }
  24.     }
  25.  
  26.     public function setTransparent($bool)
  27.     {
  28.         $this->_transparent = (boolean)$bool;
  29.     }
  30.  
  31.     public function load($fileName)
  32.     {
  33.         $imageInfo = getimagesize($fileName);
  34.         $this->_imageType = $imageInfo[2];
  35.  
  36.         if($this->_imageType == IMAGETYPE_JPEG){
  37.             $this->_image = imagecreatefromjpeg($fileName);
  38.         }
  39.         elseif($this->_imageType == IMAGETYPE_GIF){
  40.             $this->_image = imagecreatefromgif($fileName);
  41.         }
  42.         elseif($this->_imageType == IMAGETYPE_PNG){
  43.             $this->_image = imagecreatefrompng($fileName);
  44.         }
  45.     }
  46.  
  47.     public function save($fileName, $compression=75, $permissions=null)
  48.     {
  49.         if($this->_imageType == IMAGETYPE_JPEG){
  50.             imagejpeg($this->_image, $fileName, $compression);
  51.         }
  52.         elseif($this->_imageType == IMAGETYPE_GIF){
  53.             imagegif($this->_image, $fileName);        
  54.         }
  55.         elseif($this->_imageType == IMAGETYPE_PNG){
  56.             imagepng($this->_image, $fileName);
  57.         }
  58.  
  59.         if(!is_null($permissions)) {
  60.             chmod($fileName, $permissions);
  61.         }
  62.     }
  63.  
  64.     public function output()
  65.     {
  66.         if($this->_imageType == IMAGETYPE_JPEG){
  67.             imagejpeg($this->_image);
  68.         }
  69.         elseif($this->_imageType == IMAGETYPE_GIF){
  70.             imagegif($this->_image);        
  71.         }
  72.         elseif($this->_imageType == IMAGETYPE_PNG){
  73.             imagepng($this->_image);
  74.         }  
  75.     }
  76.  
  77.     public function getWidth()
  78.     {
  79.         return imagesx($this->_image);
  80.     }
  81.  
  82.     public function getHeight()
  83.     {
  84.         return imagesy($this->_image);
  85.     }
  86.  
  87.     public function getImageType()
  88.     {
  89.         switch($this->_imageType){
  90.             case IMAGETYPE_JPEG:
  91.                 $imageType = 'image/jpeg';
  92.                 break;
  93.             case IMAGETYPE_GIF:
  94.                 $imageType = 'image/gif';
  95.                 break;
  96.             case IMAGETYPE_PNG:
  97.                 $imageType = 'image/png';
  98.                 break;
  99.             default:
  100.                 $imageType = null;
  101.         }
  102.         return $imageType;
  103.     }
  104.  
  105.     public function resizeToHeight($height)
  106.     {
  107.         $ratio = $height / $this->getHeight();
  108.         $width = $this->getWidth() * $ratio;
  109.         $this->resize($width,$height);
  110.     }
  111.  
  112.     public function resizeToWidth($width)
  113.     {
  114.         $ratio = $width / $this->getWidth();
  115.         $height = $this->getHeight() * $ratio;
  116.         $this->resize($width, $height);
  117.     }
  118.  
  119.     public function scale($scale)
  120.     {
  121.         $width = $this->getWidth() * $scale / 100;
  122.         $height = $this->getHeight() * $scale / 100;
  123.         $this->resize($width, $height);
  124.     }
  125.  
  126.     public function resize($width, $height)
  127.     {
  128.         $newImage = imagecreatetruecolor($width, $height);
  129.         if($this->getImageType() == 'image/png' && $this->_transparent === true){
  130.             imagealphablending($newImage, false);
  131.             imagesavealpha($newImage, true);
  132.             $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
  133.             imagefilledrectangle($newImage, 0, 0, $width, $height, $transparent);
  134.         }
  135.         imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  136.         $this->_image = $newImage;  
  137.     }      
  138. }
  139. ?>
  140. <html>
  141. <head>
  142. <style>
  143. body{ background-color: #369; }
  144. </style>
  145. </head>
  146. <body>
  147. <?php
  148. if(!empty($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK) {
  149.     $image = new ResizePicture($_FILES['image']['tmp_name'], true);
  150.     $image->resizeToWidth($image->getWidth());
  151.     $original = $_FILES['image']['name'];
  152.     $image->save($original);
  153. ?>
  154. Imagen grabada:
  155. <img src="/<?php echo $original; ?>" />
  156. <?php } ?>
  157.  
  158. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
  159.     <input type="file" name="image" />
  160.     <input type="submit" name="submit" value="Upload" />
  161. </form>
  162. </body>
  163. </html>
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos