4

I'm having a issue when resizing pictures with PHP, especially with PNG files with transparent backgrounds, instead keeping the transparent background, it turns into a black background. How can I fix this?

This is the script for resizing:

<?php         
class resize{

   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_JPEG, $compression=85, $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;
   }      

}
?>

And this is how I call it:

$picture_directory="images/" . $_FILES["file"]["name"];

    include('arqinc/resizing.php');
    $image = new resize();
    $image->load($picture_directory);
    $image->resize(660,780);
    $image->save($picture_directory);

EDIT:

ive changed my resize function to this:

function resize($width,$height) {

      $new_image = imagecreatetruecolor($width, $height);
      $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127));
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }  

But now it keeps the background white, not transparent.

EDIT 2:

Solved, this resize function is broken, this one works flawlessly:

http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/

Thanks to Tahir Yasin for linking it :D

Community
  • 1
  • 1
user1773801
  • 55
  • 2
  • 7

3 Answers3

6

I found another post with same question, according to that post following code can be used to preserve transparancy.

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

Reference: Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

Please vote-up if it helps you. :)

Community
  • 1
  • 1
Tahir Yasin
  • 11,489
  • 5
  • 42
  • 59
  • hey thanks for aswner; but i think im doing something wrong, or putting it on the wrong line, can you tell me where i have to put it ? – user1773801 Oct 25 '12 at 10:26
  • 1
    please put it before imagecolorallocatealpha() function. – Tahir Yasin Oct 25 '12 at 10:31
  • my god im so pissed, ive tried eeverything, nothing is working :@ – user1773801 Oct 25 '12 at 10:42
  • @user1773801 paste your re-size code here and I will tell you how to make it work. – Tahir Yasin Oct 25 '12 at 11:05
  • this is the resize funcion: `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; }` by the way, its up there too, its the last function on the script – user1773801 Oct 25 '12 at 11:38
  • @user1773801 Please visit this page http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/ – Tahir Yasin Oct 25 '12 at 11:56
  • or see http://stackoverflow.com/questions/279236/how-do-i-resize-pngs-with-transparency-in-php#answer-279310 – Tahir Yasin Oct 25 '12 at 11:57
  • put this code in start of your function body after $new_image = imagecreatetruecolor($width, $height); imagealphablending( $new_image, false ); imagesavealpha( $new_image, true ); – Tahir Yasin Oct 25 '12 at 12:04
  • dude, i love you, thanks you so much <3, my funcion was screwd, not even imagealphableding was working, that first link solved it. – user1773801 Oct 25 '12 at 12:20
  • love you too & happy to help :) – Tahir Yasin Oct 25 '12 at 12:26
0

You need to use imagepng($new_image,$file_name) after your imagecopyresampled() code line. replace $file_name with the name you want save the image.

php imagepng() — Is use to output a PNG image from your created image

vinu
  • 658
  • 10
  • 20
-1

this function works

function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127));
    imagealphablending($new_image, false);
    imagesavealpha($new_image, true);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}  
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • this function works.... function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); $transparent=imagefill($new_image, 0, 0, imagecolorallocatealpha($new_image, 255, 255, 255, 127)); imagealphablending($new_image, false); imagesavealpha($new_image, true); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } – no name Mar 06 '16 at 17:03