Send this to a friend
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 }
////////////////////////////////////////////////////////////////////////////////////
// generate a croped-image from a picture
// source: http://www.seaton-online.com/forum/index.php?showtopic=545
// usage: cropImage("300", "225", "test/5.jpg", "jpg", "test/output.jpg");
////////////////////////////////////////////////////////////////////////////////////
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w> $h) {
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif(($w <$h) || ($w == $h)) {
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,80);
}