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:

  1. <?
  2. $newImg = imagecreatetruecolor($nWidth, $nHeight);
  3. imagealphablending($newImg, false);
  4. imagesavealpha($newImg,true);
  5. $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  6. imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
  7. imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
  8. ?>

Full source code for resizing image:

filename: imageresize.php
  1. <?
  2. function resize($img, $w, $h, $newfilename) {
  3.  //Check if GD extension is loaded
  4.  if (!extension_loaded(‘gd’) && !extension_loaded(‘gd2’)) {
  5.   trigger_error(“GD is not loaded”, E_USER_WARNING);
  6.   return false;
  7.  }
  8.  //Get Image size info
  9.  $imgInfo = getimagesize($img);
  10.  switch ($imgInfo[2]) {
  11.   case 1: $im = imagecreatefromgif($img); break;
  12.   case 2: $im = imagecreatefromjpeg($img);  break;
  13.   case 3: $im = imagecreatefrompng($img); break;
  14.   default:  trigger_error(‘Unsupported filetype!’, E_USER_WARNING);  break;
  15.  }
  16.  //If image dimension is smaller, do not resize
  17.  if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) {
  18.   $nHeight = $imgInfo[1];
  19.   $nWidth = $imgInfo[0];
  20.  }else{
  21.                 //yeah, resize it, but keep it proportional
  22.   if ($w/$imgInfo[0] > $h/$imgInfo[1]) {
  23.    $nWidth = $w;
  24.    $nHeight = $imgInfo[1]*($w/$imgInfo[0]);
  25.   }else{
  26.    $nWidth = $imgInfo[0]*($h/$imgInfo[1]);
  27.    $nHeight = $h;
  28.   }
  29.  }
  30.  $nWidth = round($nWidth);
  31.  $nHeight = round($nHeight);
  32.  $newImg = imagecreatetruecolor($nWidth, $nHeight);
  33.  /* Check if this image is PNG or GIF, then set if Transparent*/
  34.  if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
  35.   imagealphablending($newImg, false);
  36.   imagesavealpha($newImg,true);
  37.   $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  38.   imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
  39.  }
  40.  imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
  41.  //Generate the file, and rename it to $newfilename
  42.  switch ($imgInfo[2]) {
  43.   case 1: imagegif($newImg,$newfilename); break;
  44.   case 2: imagejpeg($newImg,$newfilename);  break;
  45.   case 3: imagepng($newImg,$newfilename); break;
  46.   default:  trigger_error(‘Failed resize image!’, E_USER_WARNING);  break;
  47.  }
  48.    return $newfilename;
  49. }
  50. ?>

Script usage:

filename showimage.php
  1. <?
  2. include_once(“resizeimage.php”);
  3. $img = “some/dir/image.png”; // File image location
  4. $newfilename = “thumb_image.png”; // New file name for thumb
  5. $w = 100;
  6. $h = 100;
  7. $thumbnail = resize($img, $w, $h, $newfilename);
  8. echo “<img src='”.$thumbnail.”‘>”;
  9. ?>

No, i’m not provide any downloadable source-code.
Just copy-paste this code, and use it well.

Advertisements
By Rz Rasel Posted in Php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s