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?
source share