silasl Posted March 2, 2008 Share Posted March 2, 2008 Hi all, I'm having a probblem with a GD resize / crop script and i'm wondering if anyone can help me. The code is supposed to take an uploaded image, resize it to the correct width, retaining its aspect ration, and then crop it. Currently it crops it fine and it's doing some resizing but with it's ignoring the dimensions i'm asking for and just shrinking it a little bit, it's also distorting the image... Can anyone see what's wrong? $rWidth = 532; $rHeight = 100; $name = ( $_FILES['userfile']['name'] ); if ($name){ $target_path = "../../images/page_images/"; $target_path = $target_path . basename( $_FILES['userfile']['name']); move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path); $dimensions = getimagesize($target_path); if ($dimensions[0] != $rWidth && $dimensions[1] != $rHeight){ $canvas = imagecreatetruecolor($rWidth,$rHeight); $piece = imagecreatefromjpeg($target_path); $width = $dimensions[0]; $height = $dimensions[1]; $newWidth = $rWidth; $newHeight = round($rWidth/($width/$height)); imagecopyresized($canvas, $piece, 0, 0, 0, 0, $rWidth, $rHeight, $newWidth, $newHeight); imagejpeg($canvas,$target_path,90); } } Thanks in advance for your help. Silas Quote Link to comment https://forums.phpfreaks.com/topic/94041-php-and-gd-problems/ Share on other sites More sharing options...
blackwinter Posted March 2, 2008 Share Posted March 2, 2008 GD is great at distorting images have you thought of using ImageMagick? Quote Link to comment https://forums.phpfreaks.com/topic/94041-php-and-gd-problems/#findComment-481782 Share on other sites More sharing options...
wildteen88 Posted March 2, 2008 Share Posted March 2, 2008 GD is great at distorting images have you thought of using ImageMagick? Really! GD works fine for me. Try the following code: <?php if(isset($_POST['Submit'])) { // define the max width and height of uploaded images $max_width = 532; $max_height = 100; // define folder inwhich the images get uploaded to $upload_dir = '../../images/page_images/'; // get uploaded image data from form $file_name = $_FILES['image']['name']; $file_tmp = $_FILES['image']['tmp_name']; $file_size = $_FILES['image']['size']; $file_type = $_FILES['image']['type']; // get info about uploaded image list($width, $height, $type, $attr) = getimagesize($file_tmp); // set the full upload path for where to upload the image to. $img_path = $upload_dir . $file_name; // check to see if the uploaded image is bigger tha max width an height // if it is we'll crop the image if($width > $max_width && $height > $max_height) { // image is too big so we'll crop it // get the aspect ratio of the image // we do this so we can keep the aspect ratio when the image gets cropped. $aspect_ratio = $height / $width; $new_width = $max_height / $aspect_ratio; // create a blank canvas for the size of the image // this is what the image will get cropped to $img_rs = imagecreatetruecolor($new_width, $max_height); // setup a image resource to the uploaded file $img = imageCreateFromJPEG($file_tmp); // crop the image imagecopyresampled($img_rs, $img, 0, 0, 0, 0, $new_width, $max_height, ImageSX($img), ImageSY($img)); // create the image ImageJPEG($img_rs, $img_path); // destroy the temporary image imagedestroy($img_rs); } else { // uploaded image is under the max width and hieght // we'll just upload it straight away move_uploaded_file($file_tmp, $img_path); } // display the image echo "<h1>Upload Image:</h1>\n\n<img src=\"$img_path\">"; } else { echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '" enctype="multipart/form-data"> <input type="file" name="image"><p> <input type="Submit" name="Submit" value="Submit"> </form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94041-php-and-gd-problems/#findComment-481877 Share on other sites More sharing options...
silasl Posted March 2, 2008 Author Share Posted March 2, 2008 Thanks for your reply, that script seems to work, as in it's resizing the image without distorting, but its shrinking it down to the height of 100 and resizing the width accordingly so it's ignoring the 532 width, I need it to retain the width and then crop whatever's left to 100 height. So i'm left with a thumbnail that's 532 x 100. Did I explain that OK...? Cheers, Silas Quote Link to comment https://forums.phpfreaks.com/topic/94041-php-and-gd-problems/#findComment-481970 Share on other sites More sharing options...
wildteen88 Posted March 3, 2008 Share Posted March 3, 2008 My code maintains the aspect ratio of the uploaded image, it wont resize the image exactly to 532 x 100. If it did all images would get distorted. Quote Link to comment https://forums.phpfreaks.com/topic/94041-php-and-gd-problems/#findComment-482368 Share on other sites More sharing options...
silasl Posted March 4, 2008 Author Share Posted March 4, 2008 Hi wildteen88, I don't want to just resize any image to 532 x 100. I want to resize to 532 x whatever (so it retains the correct aspect ratio) and then crop it to 532 x 100. Cheers, Quote Link to comment https://forums.phpfreaks.com/topic/94041-php-and-gd-problems/#findComment-483184 Share on other sites More sharing options...
wildteen88 Posted March 4, 2008 Share Posted March 4, 2008 The following code will crop it width ways, so any image that is greater than 532 pixels wide will get to resized to 532px wide by what ever the height based on the aspect ratio. It wont resize it to 532 x 100 <?php if(isset($_POST['Submit'])) { // define the max width and height of uploaded images $max_width = 532; $max_height = 100; // define folder inwhich the images get uploaded to $upload_dir = './pics/'; // get uploaded image data from form $file_name = $_FILES['image']['name']; $file_tmp = $_FILES['image']['tmp_name']; $file_size = $_FILES['image']['size']; $file_type = $_FILES['image']['type']; // get info about uploaded image list($width, $height, $type, $attr) = getimagesize($file_tmp); // set the full upload path for where to upload the image to. $img_path = $upload_dir . $file_name; // check to see if the uploaded image is bigger tha max width an height // if it is we'll crop the image //if($width > $max_width && $height > $max_height) if($width > $max_width) { // image is too big so we'll crop it // get the aspect ratio of the image // we do this so we can keep the aspect ratio when the image gets cropped. $aspect_ratio = $width / $height; $new_height = $max_width / $aspect_ratio; // create a blank canvas for the size of the image // this is what the image will get cropped to $img_rs = imagecreatetruecolor($max_width, $new_height); // setup a image resource to the uploaded file $img = imageCreateFromJPEG($file_tmp); // crop the image imagecopyresampled($img_rs, $img, 0, 0, 0, 0, $max_width, $new_height, ImageSX($img), ImageSY($img)); // create the image ImageJPEG($img_rs, $img_path); // destroy the temporary image imagedestroy($img_rs); } else { // uploaded image is under the max width and hieght // we'll just upload it straight away move_uploaded_file($file_tmp, $img_path); } // display the image echo "<h1>Upload Image:</h1>\n\n<img src=\"$img_path\">"; } else { echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '" enctype="multipart/form-data"> <input type="file" name="image"><p> <input type="Submit" name="Submit" value="Submit"> </form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94041-php-and-gd-problems/#findComment-483332 Share on other sites More sharing options...
silasl Posted March 5, 2008 Author Share Posted March 5, 2008 Than's for your help wildteen88, that script with a slight tweek does exactly what i want. Cheers, Silas Quote Link to comment https://forums.phpfreaks.com/topic/94041-php-and-gd-problems/#findComment-483388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.