<?php
function cut($imgname,$x,$y,$width,$height){
list($sw,$sh,$imgType) = getimagesize($imgname);
$types = array('1' => 'gif', '2' => 'jpeg', '3' => 'png' );
$createImg = 'imagecreatefrom'.$types[$imgType];
$imgsrc = $createImg($imgname);
$ratio_orig = $sw / $sh ;
if ( $width / $height > $ratio_orig ) {
$width = $height * $ratio_orig ;
} else {
$height = $width / $ratio_orig ;
}
$dimg = imagecreatetruecolor($width, $height);
imagecopyresampled($dimg, $imgsrc, 0, 0, $x, $y, $width, $height, $width, $height);
$save = 'image'.$types[$imgType];
$save($dimg, 'new_' . $imgname);
imagedestroy($imgsrc);
imagedestroy($dimg);
}
cut('me.jpg',50,50,200,200);
?>