resample image 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

resample image transparency

Post by peterj »

Fairly new to PHP.
I am using the below resample image function but when I insert a gif or png-24 image into my site the thumbnail has a black background.
I have read a couple of similar posts but am a little confused. How can I give my thumbnails a transparent background.

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) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
$image = imagecreatefromgif($filename);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
$thumbnail = "tn_".$imageName;
$this->thumbnail = $thumbnailPath = $this->path.$thumbnail;
$sampleRate = $this->sampleRate;
$result = imagejpeg($image_p, $thumbnailPath, $sampleRate);
if(!$result){
return "imagejpeg creation was not successful";
} else {
return "resampled";
}

}


Thanks
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: resample image transparency

Post by josh »

Post Reply