Resize image in php if height or width is over a certain size and maintain original aspect ratio

Hello,

I have a function below to add a watermark.

I would also like to make it so that if the image is over 800px wide or high then it is resized with the largest dimension to be 800 (width or height) and the aspect ratio maintained. Is this something strightforward to do in php 5.6?

Sorry if this has already asked, I did search but the replies similar to this were quite old.

public function Gdn_Upload_SaveAs_Handler($Sender,$Args) {

$Path = $Args['Path'];
$width = $Args['Parsed']['Width'];

//if ($width && (preg_match("/editor/",$Path))) {
   if ($width && (!preg_match("/userpics/",$Path))) {
//below is a config statement to path of watermark instead of a hard-code.
$watermark = imagecreatefrompng(C('Plugins.Watermark.Image'));
$xoff = 10;
$yoff = 5;

if (preg_match("/png/",$Path)){
$TargetImage = imagecreatefrompng($Path);
imagecopy($TargetImage, $watermark, imagesx($TargetImage) - imagesx($watermark) - $xoff, imagesy($TargetImage) - imagesy($watermark) - $yoff, 0, 0, imagesx($watermark), imagesy($watermark));
imagepng($TargetImage, $Path);
imagedestroy($TargetImage);
}

if (preg_match("/jpg|jpeg/",$Path)){
$TargetImage = imagecreatefromjpeg($Path);
resizeImage('image.jpg', 'resized.jpg', 200, 200);
imagecopy($TargetImage, $watermark, imagesx($TargetImage) - imagesx($watermark) - $xoff, imagesy($TargetImage) - imagesy($watermark) - $yoff, 0, 0, imagesx($watermark), imagesy($watermark));
imagejpeg($TargetImage, $Path);
imagedestroy($TargetImage);
}

if (preg_match("/gif/",$Path)){
$TargetImage = imagecreatefromgif($Path);
imagecopy($TargetImage, $watermark, imagesx($TargetImage) - imagesx($watermark) - $xoff, imagesy($TargetImage) - imagesy($watermark) - $yoff, 0, 0, imagesx($watermark), imagesy($watermark));
imagegif($TargetImage, $Path);
imagedestroy($TargetImage);
}

imagedestroy($watermark);

  }
}

Here is some old code of mine and you would probably need to add an if statement to see if the image is over 800px

// Set the path to the image to resize
$input_image = "House.jpg";
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Set the new width of the image
$thumb_width = "200";
// Calculate the height of the new image to keep the aspect ratio
$thumb_height = ( int )(( $thumb_width/$size[0] )*$size[1] );
// Create a new true color image in the memory
$thumbnail = ImageCreateTrueColor( $thumb_width, $thumb_height );
// Create a new image from file 
$src_img = ImageCreateFromJPEG( $input_image );
// Create the resized image
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1] );
// Save the image as resized.jpg
ImageJPEG( $thumbnail, "resized.jpg" );
// Clear the memory of the tempory image 
ImageDestroy( $thumbnail );

Out of interest you could probably do all the watermark and resize code on one line with Imagemagick

Thanks for that, I’ll lookup Imagemagick. I’m new to PHP, will 5.6 have this imagemagick installed already?

You don’t happen to know sample code that intreates with the function above to do this?

The code below should do what you want and a couple of notes:
Most servers have Imagemagick but it is an external program not built into php and may be an old version.
When Imagemagick is given an image it opens it and resaves it so if your image is below 800 it will be opened and saved so in the case of a jpg recompressed which is not good.
This will work for any image so if you want to restrict it to one or two types you need to do that when validating the image.
Make sure you validate the image is what it says it is - should be doing that for GD as well.
There is an Imagemagick API that can be built into php. It is called Imagick and it will probably still have less code than using GD but I do not use it.
The method I have used is an imagemagick command line method. exec() can be exploited so as I say make sure you validate the image.

<?php

$input='Venice.jpg';
$output='result.jpg';
$watermark='boots.png';
// Get the size of the image into a variable
$size = getimagesize($input);
// If the width or the height of the image is greater do the resize and watermark. Otherwise just watermark
// The watermark will be in the bottom right corner of the image.
if (( $size[0] > 800 ) or ( $size[1] > 800 )){
$cmd = " -resize 800x800 $watermark -gravity southeast -composite";
}

else {
$cmd = " $watermark -gravity southwest -composite";
}

exec("convert $input $cmd $output");  

echo "<img src=\"$output\">";

?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.