By default, you will get black background if you resize a transparent image. Play Daisy slots to see how good the quality of the background images are. 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]); ?>
  1. </pre>
  2. <span id="more-133"></span>
  3. Full source code for resizing image:
  4. <pre lang="enc__php"> filename: imageresize.php
  5. <?
  6. function resize($img, $w, $h, $newfilename) {
  7.  
  8.  //Check if GD extension is loaded
  9.  if (!extension_loaded('gd') && !extension_loaded('gd2')) {
  10.   trigger_error("GD is not loaded", E_USER_WARNING);
  11.   return false;
  12.  }
  13.  
  14.  //Get Image size info
  15.  $imgInfo = getimagesize($img);
  16.  switch ($imgInfo[2]) {
  17.   case 1: $im = imagecreatefromgif($img); break;
  18.   case 2: $im = imagecreatefromjpeg($img);  break;
  19.   case 3: $im = imagecreatefrompng($img); break;
  20.   default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
  21.  }
  22.  
  23.  //If image dimension is smaller, do not resize
  24.  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]) {
  25.    $nWidth = $w;
  26.    $nHeight = $imgInfo[1]*($w/$imgInfo[0]);
  27.   }else{
  28.    $nWidth = $imgInfo[0]*($h/$imgInfo[1]);
  29.    $nHeight = $h;
  30.   }
  31.  }
  32.  $nWidth = round($nWidth);
  33.  $nHeight = round($nHeight);
  34.  
  35.  $newImg = imagecreatetruecolor($nWidth, $nHeight);
  36.  
  37.  /* Check if this image is PNG or GIF, then set if Transparent*/  
  38.  if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
  39.   imagealphablending($newImg, false);
  40.   imagesavealpha($newImg,true);
  41.   $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  42.   imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
  43.  }
  44.  imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
  45.  
  46.  //Generate the file, and rename it to $newfilename
  47.  switch ($imgInfo[2]) {
  48.   case 1: imagegif($newImg,$newfilename); break;
  49.   case 2: imagejpeg($newImg,$newfilename);  break;
  50.   case 3: imagepng($newImg,$newfilename); break;
  51.   default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
  52.  }
  53.    
  54.    return $newfilename;
  55. }
  56. ?>

Script usage:

filename showimage.php
  1. &lt;?
  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.  
  8. $thumbnail = resize($img, $w, $h, $newfilename);
  9.  
  10. echo "<img src="&quot;.$thumbnail.&quot;" />";
  11. ?&gt;

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