kevin_newbie Posted October 2, 2009 Share Posted October 2, 2009 Hello, I have a thumbnail script that is stretching the image. I do not want that, I want to be able to set the width and height of the image, then it crops what ever is left. I left an attachment of what the images are doing so you know what I am talking about. This is what I have now; header("Content-Type: image/jpeg"); $source = $_GET['pic']; $size = getimagesize($source); $setWidth = 238; $setHeight = 143; $width = $size[0]; $height = $size[1]; if($width> $height) { $x = ceil(($width - $height) / 2 ); $width = $height; } else if($height> $width) { $y = ceil(($height - $width) / 2); $height = $width; } $new_im = ImageCreatetruecolor($setWidth,$setHeight); $im = imagecreatefromjpeg($source); imagecopyresampled($new_im,$im,0,0,$x,$y,$setWidth,$setHeight,$width,$height); imagejpeg($new_im,$dest,100); Then for my php page I am doing: <img src="filename.php?pic=images/1.jpg\" /> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/176327-thumbnail-stretches/ Share on other sites More sharing options...
.josh Posted October 3, 2009 Share Posted October 3, 2009 you can do this with the gd library Quote Link to comment https://forums.phpfreaks.com/topic/176327-thumbnail-stretches/#findComment-929422 Share on other sites More sharing options...
kevin_newbie Posted October 3, 2009 Author Share Posted October 3, 2009 What do you mean use the gd library? I am using the gd library. Quote Link to comment https://forums.phpfreaks.com/topic/176327-thumbnail-stretches/#findComment-929554 Share on other sites More sharing options...
.josh Posted October 3, 2009 Share Posted October 3, 2009 right. but if you go to that link, you will see that there are a lot of other functions in that library than just the ones you used. You can use them to achieve what you want. Or you know, you could just google for a php image cropping script... Quote Link to comment https://forums.phpfreaks.com/topic/176327-thumbnail-stretches/#findComment-929656 Share on other sites More sharing options...
ProXy_ Posted October 3, 2009 Share Posted October 3, 2009 this is somewhat what your looking for. But this should give you the idea.. <?php /* * This script demonstrates the mathematics of resizing an image to a different * width and height without stretching or squeezing the original image. If the * source image has a different aspect ratio than the destination image, the * source image will be cropped. * * Consider a source image with a width of 320 pixels and a height of 240 * pixels. The destination image is to be 80 pixels by 80 pixels. If these * dimensions were passed directly to imagecopyresampled(), the resized image * would appear to have a squeezed width. To prevent this, the width of the * source image should be cropped to 240 pixels so that the ratio of its width * to its height is 1:1, which is the same as the destination image's ratio. * When the source image is resized, it will not appear squeezed. */ $src = imagecreatefromjpeg('sample.jpg'); // Source image $dw = 80; // Destination image Width $dh = 80; // Destination image Height $sw = imagesx($src); // Source image Width $sh = imagesy($src); // Source image Height $wr = $sw / $dw; // Width Ratio (source:destination) $hr = $sh / $dh; // Height Ratio (source:destination) $cx = 0; // Crop X (source offset left) $cy = 0; // Crop Y (source offset top) if ($hr < $wr) // Height is the limiting dimension; adjust Width { $ow = $sw; // Old Source Width (temp) $sw = $dw * $hr; // New virtual Source Width $cx = ($ow - $sw) / 2; // Crops source width; focus remains centered } if ($wr < $hr) // Width is the limiting dimension; adjust Height { $oh = $sh; // Old Source Height (temp) $sh = $dh * $wr; // New virtual Source Height $cy = ($oh - $sh) / 2; // Crops source height; focus remains centered } // If the width ratio equals the height ratio, the dimensions stay the same. $dst = imagecreatetruecolor($dw, $dh); // Destination image imagecopyresampled($dst, $src, 0, 0, $cx, $cy, $dw, $dh, $sw, $sh); // Previews the resized image (not saved) header('Content-type: image/jpeg'); imagejpeg($dst); ?> Hopefully this Helps Quote Link to comment https://forums.phpfreaks.com/topic/176327-thumbnail-stretches/#findComment-929826 Share on other sites More sharing options...
kevin_newbie Posted October 3, 2009 Author Share Posted October 3, 2009 Crayon Violent, Thanks for the link and let me check it out. Hi ProXy_ I tried the script you sent me and what it does is crops the picture but then distorts it, I don't want that. I like what I have now I just don't like the whole stretching out the image, as you can see in the attached on my first post. Thanks though. Quote Link to comment https://forums.phpfreaks.com/topic/176327-thumbnail-stretches/#findComment-929915 Share on other sites More sharing options...
designrandom Posted October 3, 2009 Share Posted October 3, 2009 I use something similar to the code below. It won't crop the image, or make it an exact size - it will resize to either the max-width or max-height depending on whether the image is landscape or portrait. Hope it an be of some use... define('MAX_WIDTH', 500); define('MAX_HEIGHT', 500); // process the uploaded image $original = ($_FILES['filelogo']['tmp_name']); // begin by getting the details of the original list($width, $height, $type) = getimagesize($original); // calculate the scaling ratio if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) { $ratio = 1; } elseif ($width > $height) { $ratio = MAX_WIDTH/$width; } else { $ratio = MAX_HEIGHT/$height; } Quote Link to comment https://forums.phpfreaks.com/topic/176327-thumbnail-stretches/#findComment-929922 Share on other sites More sharing options...
kevin_newbie Posted October 3, 2009 Author Share Posted October 3, 2009 Hi designrandom, Thanks for this snippet but I already have something like this, I want to be able to have my exact size like I have on my first post. Thanks again though Quote Link to comment https://forums.phpfreaks.com/topic/176327-thumbnail-stretches/#findComment-929930 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.