Link to home
Start Free TrialLog in
Avatar of Mr_Splash
Mr_Splash

asked on

GD Library- Crop and Resize

Hello

I'm trying to set up a script that will generate thumbnails for an image gallery using GD Library.

I'm using the following script, which resizes the image to 100px x 100px. It's working fine however I was hoping someone could help me customize it so that it shrinks the image by 50% then crops the thumbnail from the center.

<?php

//Input file
$file = 'image.jpg';

//Output file
$save = 'new.jpg';

//Get image dimensions
list($width, $height) = getimagesize($file) ;

//Set new Dimensions
$modwidth = 100;
$modheight = 100;

$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100);

?>
Avatar of Jason Minton
Jason Minton
Flag of United States of America image

so wouldn't you just do some math on the $width and $height to get the size reduced by 50% then use the results to resize and crop it.

?
Avatar of Mr_Splash
Mr_Splash

ASKER

That's exactly what I need to do, but I'm very new to PHP and I'm afraid I don't know the syntax.
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or this should work

<?php

//Input file
$file = 'image.jpg';

//Output file
$save = 'new.jpg';

//Get image dimensions
list($width, $height) = getimagesize($file) ;

//Set new Dimensions
$modwidth = $width/2;
$modheight = ($modwidth/$width)*$height;

$tn = imagecreatetruecolor(100, 100) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, $modwidth/2, $modheight/2, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100);

?>