you are in: codestackercodes [RSS]resize image with crop

resize image with crop Delicious Email

show/hide lines
   1  ////////////////////////////////////////////////////////////////////////////////////
   2  // generate a croped-image from a picture
   3  // source: http://www.seaton-online.com/forum/index.php?showtopic=545
   4  // usage: cropImage("300", "225", "test/5.jpg", "jpg", "test/output.jpg");
   5  ////////////////////////////////////////////////////////////////////////////////////
   6  
   7  function cropImage($nw, $nh, $source, $stype, $dest) {
   8           $size = getimagesize($source);
   9           $w = $size[0];
  10           $h = $size[1];
  11  
  12            switch($stype) {
  13                case 'gif':
  14                $simg = imagecreatefromgif($source);
  15                break;
  16                case 'jpg':
  17                $simg = imagecreatefromjpeg($source);
  18                break;
  19                case 'png':
  20                $simg = imagecreatefrompng($source);
  21                break;
  22            }
  23  
  24            $dimg = imagecreatetruecolor($nw, $nh);
  25            $wm = $w/$nw;
  26            $hm = $h/$nh;
  27            $h_height = $nh/2;
  28            $w_height = $nw/2;
  29  
  30            if($w> $h) {
  31                $adjusted_width = $w / $hm;
  32                $half_width = $adjusted_width / 2;
  33                $int_width = $half_width - $w_height;
  34                imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
  35            } elseif(($w <$h) || ($w == $h)) {
  36                $adjusted_height = $h / $wm;
  37                $half_height = $adjusted_height / 2;
  38                $int_height = $half_height - $h_height;
  39                imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
  40            } else {
  41                imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
  42            }
  43  
  44            imagejpeg($dimg,$dest,80);
  45  }
created by anonymous — 05 July 2008 — get a short url — tags: gd php resize embed

Comments



We dont send spam :)

If checked, it's highlights your comment
simple_captcha.jpg
Are you a human? Type the code from the image