Page 1 of 1

Transparency

Posted: Wed Dec 10, 2008 2:18 pm
by peterj
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello,
I think I am finally getting close with a resample image function and transparency.
I have managed to remove black from a gif image that has been resized by using:

$black = imagecolorallocate($image_p, 0x00, 0x00, 0x00);
imagecolortransparent($image_p,$black);

but this is not working to great satisfaction.

I believe the below function should work better for my purposes if only I could get it to work.
Could someone please have a look at my code and see if there is any obvious reason that it is not working?

Much appreciated.

Regards Peter

Code: Select all

 
public function resampleImage(){
    $imageName = $this->imageName;
    $filename    = $this->path.$imageName;
 
    // Set a maximum height and width
    $width = $this->maxWidth;
    $height = $this->maxHeight;
 
    // Content type (create one for gifs)
    header('Content-type: image/jpeg');
    header('Content-type: image/gif');
    header('Content-type: image/png');
 
    // Get dimensions & work out resample ratio and reset width/height vars depending:
    list($width_orig, $height_orig, $imgInfo) = getimagesize($filename);
    $ratio_orig = $width_orig/$height_orig;
    if ($width/$height > $ratio_orig) {
       $width = $height*$ratio_orig;
    } else {
       $height = $width/$ratio_orig;
    }
 
switch ($imgInfo) {
  case 1: $image = imagecreatefromgif($filename); break;
  case 2: $image = imagecreatefromjpeg($filename);  break;
  case 3: $image = imagecreatefrompng($filename); break;
  default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
 }
 
    $image_p = imagecreatetruecolor($width, $height);
 
 
    if(($imgInfo == 1) OR ($imgInfo==3)){
      imagealphablending($image_p, false);
      imagesavealpha($image_p,true);
      $transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
      imagefilledrectangle($image_p, 0, 0, $width, $height, $transparent);
     }
 
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
 
    //If white is the colour that needs to be made transparent
    $black  = imagecolorallocate($image_p, 0x00, 0x00, 0x00);
    imagecolortransparent($image_p,$black);
 
    // Output
    $thumbnail = "tn_".$imageName;
    $this->thumbnail = $thumbnailPath = $this->path.$thumbnail;
    $sampleRate = $this->sampleRate;
 
    switch ($imgInfo) {
      case 1: imagegif($image_p, $thumbnailPath, $sampleRate); break;
      case 2: imagejpeg($image_p, $thumbnailPath, $sampleRate);  break;
      case 3: imagepng($image_p, $thumbnailPath, $sampleRate); break;
      default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
     }
 
   //return $newfilename;
 
    if(!$result){
        return "imagejpeg creation was not successful";
    } else {
        return "resampled";
    }
 
}

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Transparency

Posted: Wed Dec 10, 2008 2:21 pm
by pickle
What exactly isn't working? There's lots of work you can do yourself to go from "this function isn't working" to "this particular line isn't doing what I expect" or "I don't know how to do X"

Re: Transparency

Posted: Wed Dec 10, 2008 3:05 pm
by peterj
I see, My apologies. Will post correctly from now on.
Currently, I am getting "imagejpeg creation was not successful" returned to me which is the result of no result.

Re: Transparency

Posted: Wed Dec 10, 2008 4:40 pm
by pickle
Ok, then debug backwards. Where is $result supposed to be set?

Re: Transparency

Posted: Wed Dec 10, 2008 4:45 pm
by Jade
.jpg files can't have transparency. Are you saving it as a .png or .gif?

Re: Transparency

Posted: Wed Dec 10, 2008 4:51 pm
by peterj
I have png images with transparent backgrounds which I am trying to save as png's also with transparent backgrounds

Re: Transparency

Posted: Wed Dec 10, 2008 4:57 pm
by peterj
I believe !$result should in fact be !$imgInfo in the case, however, I have just tried uploading a png image. The resulting thumbnail image has been saved as a jpeg. My switch may not be working as I planned?

Re: Transparency

Posted: Wed Dec 10, 2008 5:43 pm
by pickle
Not sure. In any case, you shouldn't be comparing it to 1,2,3, etc, you should be comparing it to IMAGETYPE_PNG, IMAGETYPE_JPEG, etc, in case those values change in the future.