Transparency
Posted: Wed Dec 10, 2008 2:18 pm
~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:
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
~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:
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: