0

I am resizing images using the code below, but their is problem with resizing png images with transparent background that it fills the background with black color.

Below is the full code of image resizing:

function store_uploaded_image($html_element_name, $new_img_width, $new_img_height ,$size ,$target_dir) {




    $target_file = $target_dir .'image-'.$size.'.png';


    $image = new SimpleImage();
    $image->load($_FILES[$html_element_name]['tmp_name']);
    $image->resize($new_img_width, $new_img_height);
    $image->save($target_file);
    return $target_file; //return name of saved file in case you want to store it in you database or show confirmation message to user
}


class SimpleImage {

   var $image;
   var $image_type;

   function load($filename) {

      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {

         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {

         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {

         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_PNG, $compression=75, $permissions=null) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image,$filename);
      }
      if( $permissions != null) {

         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image);
      }
   }
   function getWidth() {

      return imagesx($this->image);
   }
   function getHeight() {

      return imagesy($this->image);
   }
   function resizeToHeight($height) {

      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }

   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }

   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }

   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }      

}

EDIT:

as @Marc B suggested, using imagealphablending and imagesavealpha before imagecopyresampled made it to work.

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );
  • Try to create a white image first and the ["merge"] (http://php.net/manual/en/function.imagecopymerge.php) with your original image. – Guilherme Ferreira Apr 22 '15 at 14:45
  • 2
    You need to enable the transparency options, e.g. [imagealphablending](http://php.net/manual/en/function.imagealphablending.php) – Marc B Apr 22 '15 at 14:45
  • possible duplicate of [How do I resize pngs with transparency in PHP?](http://stackoverflow.com/questions/279236/how-do-i-resize-pngs-with-transparency-in-php) – Daniel Aranda Apr 22 '15 at 15:27

0 Answers0