Jeg tror du kan genbruge dette eksempel:
http://www.codebreaker.dk/exp/820952exp.phpOg her er koden:
<?php
//http://www.eksperten.dk/spm/820952
function resizeCenter( $filename, $newfilename, $max_width_or_height, $quality=8 5 )
{
$ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
switch($ext)
{
case 'jpeg':
case 'jpe':
case 'jpg':
$srcim = imagecreatefromjpeg( $filename );
break;
case 'gif':
$srcim = imagecreatefromgif( $filename );
break;
case 'png':
$srcim = imagecreatefrompng( $filename );
break;
default:
return false;
}
$ow = imagesx( $srcim );
$oh = imagesy( $srcim );
$original_min_width_or_height = min($ow,$oh);
$crop_width = floor(($ow - $original_min_width_or_height)/2);
$crop_height = floor(($oh - $original_min_width_or_height)/2);
$dstim = imagecreatetruecolor( $max_width_or_height, $max_width_or_height );
imagecopyresampled( $dstim, $srcim, 0, 0, (0+$crop_width), (0+$crop_height), $ max_width_or_height, $max_width_or_height, ($ow-($crop_width*2)), ($oh-($crop_he ight*2)) );
switch($ext)
{
case 'jpeg':
case 'jpe':
case 'jpg':
imagejpeg( $dstim, $newfilename, $quality );
break;
case 'gif':
imagegif( $dstim, $newfilename );
break;
case 'png':
$png_q = floor( abs( $quality / 10 - 9.9 ) );
imagepng( $srcim, $newfilename, $png_q );
break;
default:
return false;
}
imagedestroy( $dstim );
imagedestroy( $srcim );
return file_exists($newfilename);
}
$img = '649278.jpg';
$new_img = '820952exp.jpg';
echo '<h1>Original Picture</h1>';
echo '<img src="'.$img.'" alt="'.$img.'" />';
echo '<h1>Cropped/Resized Picture</h1>';
if(resizeCenter($img,'820952exp.jpg',100)){
echo '<img src="'.$new_img.'" alt="'.$new_img.'" />';
}else{
echo 'Crop and Resize failed';
}
?>