I want to resize an image PNG with transparence plz help. Here is the code :
function createThumb($upfile, $dstfile, $max_width, $max_height){
$size = getimagesize($upfile);
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height)) {
$tn_width = $width;
$tn_height = $height;
} elseif (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
} else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
if($size['mime'] == "image/jpeg"){
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imageinterlace( $dst, true);
ImageJpeg($dst, $dstfile, 100);
} else if ($size['mime'] == "image/png"){
$src = ImageCreateFrompng($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($dst, 255, 255, 0);
// removing the black from the placeholder
imagecolortransparent($dst, $background);
// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($dst, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dst, true);
Imagepng($dst, $dstfile);
} else {
$src = ImageCreateFromGif($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
imagegif($dst, $dstfile);
}
}
The image source :
The current result (that I don't want) is :
How can I resize the image and maintain the transparency of the background color? Need help, please. Thanks.
Images can be resized using ImageMagick or GD functions. If GD's functions are used, the size of the image file is also reduced when raw digital camera images are sampled.
If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.
imagecopyresampled
is in the wrong place. This should be called after you have set the background colour:
$src = ImageCreateFrompng($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
// use imagecolorallocatealpha to set BG as transparent:
$background = imagecolorallocatealpha($dst, 255, 255, 255, 127);
// turning off alpha blending as it's not needed
imagealphablending($dst, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dst, true);
// Do this here!
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
Imagepng($dst, $dstfile);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With