Advertisement
cornedor

Untitled

Nov 28th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | None | 0 0
  1. <?php
  2.     getThumb($_GET['url'], $_GET['width'], $_GET['height'], false);
  3.     function getThumb($url, $width, $height, $savePath)
  4.     {
  5.         // Get image
  6.         $handle = fopen($url, 'rb');
  7.         $img = new Imagick();
  8.         $img->readImageFile($handle);
  9.  
  10.         // Do math
  11.  
  12.         $imgWidth = $width;
  13.         $imgHeight = $height;
  14.         $wah = $imgWidth > $imgHeight;
  15.         $r;
  16.         if(!$wah)
  17.         {
  18.             $r = $img->width/$img->height;
  19.             $imgWidth = $imgHeight*$r;
  20.         }
  21.         else
  22.         {
  23.             $r = $img->height/$img->width;
  24.             $imgHeight = $imgWidth*$r;
  25.         }
  26.  
  27.         // Resize iamge
  28.         $img->resizeImage($imgWidth, $imgHeight, 0, 0);        
  29.  
  30.         // Crop image
  31.         $offsetX = ($img->width - $width)/2;
  32.         $offsetY = ($img->height - $height)/2;
  33.         $img->cropImage($width, $height, $offsetX, $offsetY);
  34.         $img->setImagePage(0, 0, 0, 0);
  35.  
  36.         if(!$savePath) echo '<img src="data:image/jpg;base64,'.base64_encode($img->getImageBlob()).'" alt="" />';
  37.         else $img->writeImage($savePath);
  38.     }
  39. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement