Advertisement
christiansalazarh

recortar y reducir una imagen con php y GD

Nov 23rd, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. <?php
  2. /**
  3.  * ImageResizer
  4.  *  a helper class to handle image resize.
  5.  *
  6.  *  example usage:
  7.  *     
  8.  *  $fullName = '/anypath/image.jpg';
  9.  *  list($ow, $oh, $xmime) = getimagesize($fullName);
  10.  *  $imageSize = filesize($fullName);
  11.  *  $mime = '';
  12.  *  if($xmime == 2) $mime = 'image/jpg';
  13.  *  if($xmime == 3) $mime = 'image/png';
  14.  *   
  15.  *  $f = fopen($fullName,"r");
  16.  *  $imageData = fread($f, $imageSize);
  17.  *  fclose($f);
  18.  * 
  19.  *  $r = new ImageResizer();
  20.  *  $newImage = $r->resize($imageData, 160, 120, 70, 'jpg', $ow, $oh);
  21.  *  header('Content-type: '.$mime);
  22.  *  echo $newImage;
  23.  *
  24.  *
  25.  * @author Christian Salazar <christiansalazarh@gmail.com>
  26.  * @license NEW BSD.
  27.  */
  28. class ImageResizer {
  29.     /**
  30.      * resize
  31.      *  resizes an image making it to fit into a rectangle
  32.      *
  33.      * @param mixed $image  Binary raw image data.
  34.      * @param mixed $dw     destination viewport width
  35.      * @param mixed $dh     destination viewport height
  36.      * @param mixed $q  quality for jpg or png: 1 to 100.
  37.      * @param mixed $imgtype desired output image type 'jpg' or 'png'
  38.      * @param mixed $ow     original image width
  39.      * @param mixed $oh     original image height
  40.      * @return new image. you can echo it or use GD functions to handle it.
  41.      */
  42.     public function resize($image, $dw, $dh, $q, $imgtype, $ow, $oh){
  43.         $im = imagecreatetruecolor($dw, $dh);
  44.         $im_src = imagecreatefromstring($image);
  45.         $_w = 0;
  46.         $_h = 0;
  47.         $this->_scaleVector($dw, $dh, 0.95, $ow, $oh, $_w, $_h);
  48.         // displacement vector, this vector center the image.
  49.         $dx = ($dw - $_w)/2;
  50.         $dy = ($dh - $_h)/2;
  51.         $fillcolor = imagecolorallocate($im,255,255,255);
  52.         //$xcolor = imagecolorallocate($im, 200,200,200);
  53.         imagefilledrectangle($im, 0,0,$dw, $dh, $fillcolor);
  54.         //imagefilledrectangle($im, $dx,$dy, $dx + $_w, $dy + $_h, $xcolor);
  55.         imagecopyresampled(
  56.                 $im, $im_src,
  57.                 $dx, $dy, 0, 0,
  58.                 $_w, $_h,
  59.                 $ow, $oh
  60.         );
  61.         if($imgtype == 'png')
  62.             return imagepng($im, null, $q);
  63.         return imagejpeg($im, null, $q);
  64.     }
  65.  
  66.     /**
  67.      * _scaleVector
  68.      * 
  69.      *
  70.      * @param mixed $dw         |   destination viewport:
  71.      * @param mixed $dh         |       d = {w, h}
  72.      * @param mixed $delta      |   delta: is a fixture measurement. max 1.
  73.      * @param mixed $ow         |   original viewport to be scaled into "d":
  74.      * @param mixed $oh         |       o = {w, h}
  75.      * @param mixed $out_w     
  76.      * @param mixed $out_h
  77.      * @access private
  78.      * @author Christian Salazar H. <christiansalazarh@gmail.com>  
  79.      * @return void
  80.      */
  81.     private function _scaleVector($dw, $dh, $delta, $ow, $oh, &$out_w, &$out_h){
  82.         $dR = $dw / $dh;
  83.         if($dR >= 1){
  84.             $out_w = $delta * $dw;
  85.             $out_h = ($out_w * $oh) / $ow;
  86.         }else{
  87.             $out_h = $delta * $dh;
  88.             $out_w = ($out_h * $ow) / $oh;
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement