Mega Code Archive

 
Categories / Php / Code Snippets
 

A resizing example that keeps the aspect ratio of the image intact

The ratio is passed to the script and then the length and width are divided by that. <?php header("Content-type: image/jpeg"); //get the scale setting $scale = $HTTP_GET_VARS['scale']; //our test image $img = "test.jpg"; $input_image = ImageCreateFromJPEG("$img"); //scale the image $image_width = ImageSX($input_image) / $scale; $image_height = ImageSY($input_image) / $scale; //create new image $output_image = ImageCreate($image_width, $image_height); //resize new image ImageCopyResized($output_image, $input_image, 0, 0, 0, 0, $image_width, $image_height, ImageSX($input_image), ImageSY($input_image)); //output image ImageJPEG($output_image); //clean up ImageDestroy($input_image); ImageDestroy($output_image); ?>