Jump to content

Make image smaller so it looks good


webbhelp

Recommended Posts

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! =)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :P

 

Thanks

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.