webbhelp Posted January 16, 2011 Share Posted January 16, 2011 Hi! I using PHP - GD to rotate an image and put a watermark to it, that works great! But I want to make the image smaller because the image the user uploads is big. I can make the image smaller by: style="width: 300px; height:300px;" but the quality is not good then. My question is how I can make the image smaller, with GD if that's necessary. I guess I want to keep the proportion so I got a normal picture and not to width or to height or something. I want the picture width a width like 300-320 px how do i know how much the height will be? And do i need to crop the image? And how do I crop the image and still beeing sure to keep the face of the person on the picture? The width and height need to be same at all images! Thanks in advance, I really hope you can help me with this, this is really important! =) Quote Link to comment https://forums.phpfreaks.com/topic/224649-make-image-smaller-so-it-looks-good/ Share on other sites More sharing options...
litebearer Posted January 17, 2011 Share Posted January 17, 2011 might try this http://www.nstoia.com/toh/imageresize.php Quote Link to comment https://forums.phpfreaks.com/topic/224649-make-image-smaller-so-it-looks-good/#findComment-1160457 Share on other sites More sharing options...
leitning_strike Posted January 17, 2011 Share Posted January 17, 2011 Just got done working on a similar problem. You can do it with GD, but GD will make it look terrible when resizing, so I used ImageMagick from the command line, but you must have it installed on your server. First, use GD to figure out the dimensions of your new image with ratios: $picName = // The name of the image file $picDir = // The directory the image file is stored in $picPath = $picDir . $picName; // Get the original size of the image $info = getimagesize($picPath); $width = $info[1]; $height = $info[2]; // Set the max sizes $maxWidth = 300; $maxHeight = 320; // Compute Max to Original Ratios $xRatio = $maxWidth / $width; $yRatio = $maxHeight / $height; // Set New Sizes // If original size is fine if(($xRatio >= 1) && ($yRatio >= 1)) { $newWidth = $width; $newHeight = $height; } // If vertically long else if($height > $width) { $newHeight = $maxHeight; $newWidth = ceil($width * $yRatio); } // If horizontally long else { $newHeight = ceil($height * $xRatio); $newWidth = $maxWidth; } Once you have your new dimensions (and are able to call ImageMagick from the command line) you can execute the command 'mogrify' to resize an image without changing it's path or 'convert' to resize and move an image. I'll show both here, resizing the original and then creating a thumbnail with dimensions 70x70. $thumbPath = $picDir . '/thumbs/' . $picName; $resizeIt = 'mogrify -resize ' . $newWidth . 'x' . $newHeight . ' ' . $picPath; exec($resizeIt); $thumbIt = 'convert ' . $picPath . ' -resize 70x70 ' . $thumbPath; exec($thumbIt); Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/224649-make-image-smaller-so-it-looks-good/#findComment-1160584 Share on other sites More sharing options...
webbhelp Posted January 17, 2011 Author Share Posted January 17, 2011 Thanks for your answers. The first answer the link, just resize the images, but the images got different sizes so they will look strange when I make the width and height less... The second link... I don't get magicquick and I can't install it :/ So is there another solution... I would be very happy Thanks Quote Link to comment https://forums.phpfreaks.com/topic/224649-make-image-smaller-so-it-looks-good/#findComment-1160589 Share on other sites More sharing options...
litebearer Posted January 17, 2011 Share Posted January 17, 2011 When you resize an image using equal h & w (ie 300 x 300); UNLESS either the original is square OR you CROP the original to a square, you will end up with a distorted image. The major difficulty in CROPPING an image automatically, is deciding what part of the image you want - ie if there is a face near the left edge but you crop from the center you may lose the face. There a numerous 'scripts' that allow your user to determine what portion of an image to crop - ie http://www.hotscripts.com/blog/javascript-image-cropping-scripts/ Hope this is of some help. Quote Link to comment https://forums.phpfreaks.com/topic/224649-make-image-smaller-so-it-looks-good/#findComment-1160738 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.