Advertisement
Shaun_B

Creating a thumbnail of an image with alpha/transparency

Feb 28th, 2013
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1.     /**
  2.      * This accepts two parameters, the file path to the image that you want
  3.      * to create a thumbnail of ($file) and the directory path and file name
  4.      * where you want to put the thumbnail ($directory).
  5.      * For the purposes of this module, the directory path to the images are:
  6.      * ../images/mypictures/example.jpg
  7.      * and the place to put the thumbnail is:
  8.      * ../images/mypicturesthums/thumb_example.jpg
  9.      * Returns true if everything has worked out okay, false if there has been
  10.      * a problem when calling the function
  11.      * @param string $file
  12.      * @param string $directory
  13.      **/
  14.     public function createThumb( $file='', $directory = '' ) {
  15.         // Booleans used to set the type of images that we'll deal with later
  16.         // (all false by default):
  17.         $jpeg = false; $png = $jpeg; $gif = $jpeg;
  18.         // Gets the type of image from the array:
  19.         $type = $this->imgData['exts'];
  20.         // Checks for each case to ensure that the relevant filetype is called:
  21.         switch($type) {
  22.             case 'gif':
  23.             $img = imagecreatefromgif($file);
  24.             // Checks for transparency:
  25.             $transparentIndex = imagecolortransparent($img);
  26.             if($transparentIndex!=(-1)) {
  27.                 $transparentCol = imagecolorsforindex($img, $transparentIndex);
  28.             }
  29.             $gif = true;
  30.             break;
  31.             case 'jpeg':
  32.             case 'jpg':  
  33.                 $img = imagecreatefromjpeg($file);
  34.                 $jpeg = true;
  35.             break;
  36.             case 'png':
  37.                 $img = imagecreatefrompng($file);
  38.                 // Checks for alpha channel:
  39.                 imagealphablending($img, true);
  40.                 imagesavealpha($img, true);
  41.                 $png = true;
  42.             break;
  43.             default:
  44.                 return false;
  45.             break;
  46.         }
  47.         // This should be the same as saying $newX = imagesx($img)/8, but might
  48.         // be a bit quicker:
  49.         $newX = imagesx($img) >> 3;
  50.         $newY = imagesy($img) >> 3;
  51.         // Creates instance of thumbnail at 1/8 the size, and scales the parent image appropriately:
  52.         $small = imagecreatetruecolor($newX, $newY);
  53.         imagecopyresampled($small, $img, 0, 0, 0, 0, $newX, $newY, imagesx($img),imagesy($img));
  54.         // Saves new image according to type:
  55.         if($jpeg) {
  56.             imagejpeg($small, $directory, 100);
  57.         } else if ($gif) {
  58.             imagegif($small, $directory, 100);
  59.         } else if ($png) {
  60.             imagepng($small, $directory, 100);
  61.         } else {
  62.             // If no boolean is set then something has gone wrong:
  63.             return false;
  64.         }
  65.         // Releases resources:
  66.         imagedestroy($img);
  67.         imagedestroy($small);
  68.         // You success:
  69.         return true;
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement