PHP-GD: Resize Transparent Image PNG & GIF
By default, you will get black background if you resize a transparent image. To fix it, you need set alpha channel imagecolorallocatealpha to 127.
With imagecolorallocatealpha, it will allocate a color for an image.
Usage:
int imagecolorallocatealpha ( resource image, int red, int green, int blue, int alpha)
From PHP manual:
imagecolorallocatealpha() behaves identically to imagecolorallocate() with the addition of the transparency parameter alpha which may have a value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.
Returns FALSE if the allocation failed.
Before using it, you must set to false the blending mode for an image and set true the flag to save full alpha channel information.
Example:
-
<?
-
$newImg = imagecreatetruecolor($nWidth, $nHeight);
-
imagealphablending($newImg, false);
-
imagesavealpha($newImg,true);
-
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
-
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
-
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
-
?>
Full source code for resizing image:
-
<?
-
function resize($img, $w, $h, $newfilename) {
-
-
//Check if GD extension is loaded
-
if (!extension_loaded('gd') && !extension_loaded('gd2')) {
-
trigger_error("GD is not loaded", E_USER_WARNING);
-
return false;
-
}
-
-
//Get Image size info
-
$imgInfo = getimagesize($img);
-
switch ($imgInfo[2]) {
-
case 1: $im = imagecreatefromgif($img); break;
-
case 2: $im = imagecreatefromjpeg($img); break;
-
case 3: $im = imagecreatefrompng($img); break;
-
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
-
}
-
-
//If image dimension is smaller, do not resize
-
if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) {
-
$nHeight = $imgInfo[1];
-
$nWidth = $imgInfo[0];
-
}else{
-
//yeah, resize it, but keep it proportional
-
if ($w/$imgInfo[0] > $h/$imgInfo[1]) {
-
$nWidth = $w;
-
$nHeight = $imgInfo[1]*($w/$imgInfo[0]);
-
}else{
-
$nWidth = $imgInfo[0]*($h/$imgInfo[1]);
-
$nHeight = $h;
-
}
-
}
-
$nWidth = round($nWidth);
-
$nHeight = round($nHeight);
-
-
$newImg = imagecreatetruecolor($nWidth, $nHeight);
-
-
/* Check if this image is PNG or GIF, then set if Transparent*/
-
if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
-
imagealphablending($newImg, false);
-
imagesavealpha($newImg,true);
-
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
-
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
-
}
-
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
-
-
//Generate the file, and rename it to $newfilename
-
switch ($imgInfo[2]) {
-
case 1: imagegif($newImg,$newfilename); break;
-
case 2: imagejpeg($newImg,$newfilename); break;
-
case 3: imagepng($newImg,$newfilename); break;
-
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
-
}
-
-
return $newfilename;
-
}
-
?>
Script usage:
-
<?
-
include_once("resizeimage.php");
-
$image = "some/dir/image.png"; // File image location
-
$newfilename = "thumb_image.png"; // New file name for thumb
-
$w = 100;
-
$h = 100;
-
-
$thumbnail = resize($img, $w, $h, $newfilename);
-
-
echo "<img src='".$thumbnail."'>";
-
?>
No, i’m not provide any downloadable source-code.
Just copy-paste this code, and use it well.
–



9 Responses to “PHP-GD: Resize Transparent Image PNG & GIF”
By hendry on Jul 19, 2008 | Reply
wow just simple, it’s work !!! thanks
By joy on Jul 24, 2008 | Reply
wow nice!!!
By rivaldo on Jul 25, 2008 | Reply
Fantastic job!
By kurroman on Jul 31, 2008 | Reply
First, my english is a little…
I have a problem. Im trying to open a png file with a transparent object inside (not all, just the border). Then i write some text and save the file as another .png file.
The problem is that de quality of the object border is too bad (¿pixeled?). How can i fix that?
By vandai on Jul 31, 2008 | Reply
have u tried this code?
i have the same problem with you before.
the image quality is sooo bad.
but this code has fix it.
By Christopher on Aug 8, 2008 | Reply
Many thanks! Just the help I needed.
By TAF on Aug 23, 2008 | Reply
The script gets hinkey if you have a file that is smaller in height than the minimum, but longer in width… you end up resizing huge in width to get the right height.
It needs to resize down by which ever length is too long, or both if they are both too long.
I HAVE AN IMPROVEMENT THAT FIXES THIS:
//If image dimension is smaller, do not resize
if ($imgInfo[0] <= $w && $imgInfo[1] = $w && $imgInfo[1] <= $h) {
$nWidth = $w;
$nHeight = $imgInfo[1]*($w/$imgInfo[0]);
}else{
//if h larger but not w, resize it, but keep it proportional
if ($imgInfo[0] = $h) {
$nWidth = $imgInfo[0]*($h/$imgInfo[1]);
$nHeight = $h;
}else{
//if both w and h are larger, resize it, but keep it proportional
if ($w/$imgInfo[0] > $h/$imgInfo[1]) {
$nWidth = $w;
$nHeight = $imgInfo[1]*($w/$imgInfo[0]);
}else{
$nWidth = $imgInfo[0]*($h/$imgInfo[1]);
$nHeight = $h;
}
}
}
}
$nWidth = round($nWidth);
$nHeight = round($nHeight);
By ghprod on Aug 29, 2008 | Reply
this is one i’m looking for
Thnx for this great code!
By TAF on Aug 29, 2008 | Reply
Ahhh… just realised through lotsa testing that the line:
if ($w/$imgInfo[0] > $h/$imgInfo[1]) {
Should be
if ($w/$imgInfo[0] < $h/$imgInfo[1]) {