Share Pastebin
Guest
Public paste!

Żałoba

By: a guest | Apr 12th, 2010 | Syntax: PHP | Size: 1.60 KB | Hits: 718 | Expires: Never
Copy text to clipboard
  1. <?
  2.  
  3. $img = $_GET['img'];
  4. $type = strtolower(array_pop(explode('.', $img)));
  5. try {  
  6.         switch($type) {
  7.                 case 'jpg':
  8.                 case 'jpeg':
  9.                         $im = imagecreatefromjpeg($img);
  10.                         $type = 'jpeg';
  11.                         break;
  12.  
  13.                 case 'gif':
  14.                         $imo = imagecreatefromgif($img);                       
  15.                         $im = imagecreatetruecolor(imagesx($imo), imagesy($imo));
  16.                         setTransparency($im, $imo);
  17.                         imagecopy($im, $imo, 0, 0, 0, 0, imagesx($imo), imagesy($imo));
  18.                         $type = 'gif';
  19.                         break;
  20.  
  21.                 case 'png':
  22.                         $im = imagecreatefrompng($img);
  23.                         $type = 'png';
  24.                         break;
  25.         }
  26.  
  27.         if(!$im) throw new Exception();
  28.         if(!file_exists('cache/'.md5($img).'.'.$type)) {
  29.                 $func = 'image'.$type;
  30.                 imagealphablending($im, false);
  31.                 imagesavealpha($im, true);             
  32.                 imagefilter($im, IMG_FILTER_GRAYSCALE);        
  33.                 $func($im, 'cache/'.md5($img).'.'.$type);
  34.         }      
  35.  
  36.         header('Content-type: image/'.$type);
  37.         readfile('cache/'.md5($img).'.'.$type);
  38.  
  39. }
  40. catch(Exception $e) {
  41.         $type = strtolower(array_pop(explode('.', $img)));
  42.         header('Content-type: image/'.$type);
  43.         readfile($img);
  44. }
  45.  
  46. function setTransparency($new_image,$image_source)
  47. {
  48.  
  49.         $transparencyIndex = imagecolortransparent($image_source);
  50.         $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
  51.  
  52.         if ($transparencyIndex >= 0) {
  53.                 $transparencyColor    = imagecolorsforindex($image_source, $transparencyIndex);  
  54.         }
  55.  
  56.         $transparencyIndex    = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
  57.         imagefill($new_image, 0, 0, $transparencyIndex);
  58.         imagecolortransparent($new_image, $transparencyIndex);
  59.         return $new_image;
  60.  
  61. }