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.
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
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\">";
?>