toolman Posted February 12, 2012 Share Posted February 12, 2012 Hi, I have some images that I want to resize to a square dimention (40px 40px). However, not all image are square, so when they are resized, they lose their ration and look squashed. Is there a way I can display just a part of the original image, say 40px x 40px on the top middle of each image? I hope that makes sense Quote Link to comment https://forums.phpfreaks.com/topic/256934-show-part-of-image-without-distorting-image/ Share on other sites More sharing options...
scootstah Posted February 12, 2012 Share Posted February 12, 2012 Yes, or you can resize proportionally. So 40x40 might actually be 40x35. There's a bazillion Google results for doing so. Quote Link to comment https://forums.phpfreaks.com/topic/256934-show-part-of-image-without-distorting-image/#findComment-1317216 Share on other sites More sharing options...
litebearer Posted February 12, 2012 Share Posted February 12, 2012 Perhaps using a combination of two methods... CROP - http://www.nstoia.com/sat/crop/ RESIZE - http://www.nstoia.com/sat/resize/ Quote Link to comment https://forums.phpfreaks.com/topic/256934-show-part-of-image-without-distorting-image/#findComment-1317219 Share on other sites More sharing options...
SergeiSS Posted February 12, 2012 Share Posted February 12, 2012 I think you need GD2 or Imagick library if you like to do it inside PHP. You may save resized images at you server or resize them 'on the fly', dynamically, every time when you need it. In the first case you save a time (but need some space on a server), in the second case you save a space on your server (but need a time to resize every time when you need a smal copy). Quote Link to comment https://forums.phpfreaks.com/topic/256934-show-part-of-image-without-distorting-image/#findComment-1317271 Share on other sites More sharing options...
MMDE Posted February 13, 2012 Share Posted February 13, 2012 As everyone above has pointed out, there are many ways to do this. Do you really want to re-size the image, or do you just want it to appear re-sized? If you just want it to appear re-sized, then use html for this, the img tag has height and width parameters you can set. You may not know the size of the image and it might appear weird when re-sized to the same size every time, which means you either need to set these on server side with for example PHP, where your best choice is using the GD library or maybe store it in a database etc, or you can also use css to do this, and even javascript. The later two choices are much better than the first, because it does use any resources on the serverside, and if you need hel with this, just google, easy to find. If you absolutely want to re-size the image, then I recommend you do this with the GD library. It's not that hard to figure out, so just play around with it a bit. http://www.php.net/manual/en/ref.image.php Quote Link to comment https://forums.phpfreaks.com/topic/256934-show-part-of-image-without-distorting-image/#findComment-1317418 Share on other sites More sharing options...
Psycho Posted February 13, 2012 Share Posted February 13, 2012 Here is a script I created a long time ago that will resize an image to a specified dimension. It also takes an optional parameter to determine whetther you want the image cropped. If this parameter is set to yes, then the image will be resized (proportionally) so that one dimension is the same as the target and the other dimension will be larger than the target with the overlap cropped off. So, if you have an image that is 100 x 150 and you want it resized to 60 x 60, the result will be exactly 60 x 60 with 50% of the 150 dimension cropped off. As I said, I wrote this a long time ago and haven't used it in a long time. So, I give no warranty //This function resizes and, optionally, crops an image and returns an image resource. //If cropping is selected, the image will be resized and cropped such that it exactly fits the dimensions. ###UPADTE TO ALLOW DIFFERENT INPUT IMAGE TYPES ###UPADTE TO ALLOW DIFFERENT OUTPUT IMAGE TYPES function resizeImage($filename, $dst_w, $dst_h, $crop=false) { //Format new dimensions as integers $dst_w = intval($dst_w); $dst_h = intval($dst_h); //Get dimensions of original image $src_info = @getimagesize($filename); //Return false if any invalid input if($dst_w<1 || $dst_h<1 || !$src_info) { return false; } //Define additional variables needed list($src_w, $src_h) = $src_info; $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; //Get ratios of source and destination $src_ratio = $src_w / $src_h; $dst_ratio = $dst_w / $dst_h; if($crop!==true) { //Calculate resize dimentions if ($src_ratio < $dst_ratio) { $dst_w = round($dst_h / $src_ratio); } else { $dst_h = round($dst_w / $src_ratio); } } else { //Calculate crop dimentions if ($src_ratio < $dst_ratio) { //Determine Resize ratio on width $ratio = $dst_w / $src_w; //Detemine cropping dimensions for height $crop = round((($src_h * $ratio) - $dst_h) / $ratio); $src_y = round($crop/2); $src_h = $src_h - $crop; } else { //Detemine Resize ratio on height $ratio = $dst_h / $src_h; //Detemine cropping dimensions for width $crop = round((($src_w * $ratio) - $dst_w) / $ratio); $src_x = round($crop/2); $src_w = $src_w - $crop; } } //Create new image $dst_image = imagecreatetruecolor($dst_w, $dst_h); $src_image = imagecreatefromjpeg($filename); imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); return $dst_image; } Quote Link to comment https://forums.phpfreaks.com/topic/256934-show-part-of-image-without-distorting-image/#findComment-1317419 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.