PHP Download large image and resize proportionally to Photoshop

Brief summary. Currently, we make small, medium and large images based on the images of products that we have on our site. Not being a PHP professional, I thought I would try to automate this nightmare anyway.

Download image and thumb size below

800 width x 1400 width LARGE 300 width x 525 width THUMB 

The problem with my PHP script is that it just resizes with scaling to proportions. I want it to scale like in Photoshop, you just click shift and scale the image. I am trying to use imagecopyresized , but with little luck.

 <?php // PHP UPLOAD SCRIPT // VALIDATION OF FORM if($_FILES['user_image']['type'] == "image/jpeg" && $_FILES['user_image']['size'] < 3000000) { // VARS $target_folder = 'images/'; $upload_image = $target_folder.basename($_FILES['user_image']['name']); // UPLOAD FUNCTION if(move_uploaded_file($_FILES['user_image']['tmp_name'], $upload_image)) { // VARS FOR FILE NAMES $thumbnail = $target_folder."medium_x.jpg"; $actual = $target_folder."large_x.jpg"; // THUMBNAIL SIZE list($width, $height) = getimagesize($upload_image); $newwidth = "300"; $newheight = "525"; // VARS FOR CALL BACK $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($upload_image); // RESIZE WITH PROPORTION LIKE PHOTOSHOP HOLDING SHIFT imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // MAKE NEW FILES imagejpeg($thumb, $thumbnail, 100); // FILE RENAMES rename($upload_image, $actual); { // SUCCESS MESSAGES ?> <p> Image Successfully uploaded!<br /> Actual image: <?=$actual;?><br /> Thumbnail image: <?=$thumbnail;?><br /> <h1>Large</h1> <img src="<?=$actual;?>"/><br /><br /> <h1>Medium</h1> <img src="<?=$thumbnail;?>"/><br /><br /> </p> <? } // FAILED MESSAGES } else { echo 'Upload failed.'; } } else { // NOT A JPEG echo 'Not a JPEG'; } // END OF SCRIPT ?> 

How to resize an image without stretching the image? Is it possible?

+4
source share
2 answers

The easiest way to achieve this without PHP code codes is with Image Magick. See Examples and command line examples here: http://www.imagemagick.org/Usage/resize/#fill

(can call PHP using exec () or similar)

Edit: The implementation is as follows:

 if($_FILES['user_image']['type'] == "image/jpeg" && $_FILES['user_image']['size'] < 3000000) { // VARS $target_folder = 'images/'; $upload_image = $target_folder.basename($_FILES['user_image']['name']); $thumbnail = $target_folder."medium_x.jpg"; $actual = $target_folder."large_x.jpg"; $newwidth = "300"; $newheight = "525"; if(move_uploaded_file($_FILES['user_image']['tmp_name'], $upload_image)) { exec('convert '.$upload_image.' -resize '.$newwidth.'x'.$newheight.'^ '.$thumbnail); rename($upload_image, $actual); } } ... etc 

You can optimize the way you handle loading, but I didn’t want to change the structure of your code too much, because I don’t know what surrounds it. But basically you push the image towards ImageMagick convert , and it will work with magic.

If you want to crop as well as scale, the exec command will be:

exec('convert '.$upload_image.' -resize '.$newwidth.'x'.$newheight.'^ -gravity center -extent '.$newwidth.'x'.$newheight.' '.$thumbnail);

+8
source

Take a look at this piece of code. Does what you need. It is mainly used to maintain a relationship when creating the thumb. http://snipplr.com/view/753/

+1
source

All Articles