Transparency

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peterj
Forum Newbie
Posts: 10
Joined: Fri Dec 05, 2008 5:19 pm

Transparency

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Transparency

Post 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"
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
peterj
Forum Newbie
Posts: 10
Joined: Fri Dec 05, 2008 5:19 pm

Re: Transparency

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Transparency

Post by pickle »

Ok, then debug backwards. Where is $result supposed to be set?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Transparency

Post by Jade »

.jpg files can't have transparency. Are you saving it as a .png or .gif?
peterj
Forum Newbie
Posts: 10
Joined: Fri Dec 05, 2008 5:19 pm

Re: Transparency

Post by peterj »

I have png images with transparent backgrounds which I am trying to save as png's also with transparent backgrounds
peterj
Forum Newbie
Posts: 10
Joined: Fri Dec 05, 2008 5:19 pm

Re: Transparency

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Transparency

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply