A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearResize/crop/pad a picture to a fixed size - page 1
User InfoPosts
Resize/crop/pad a picture to a fixed size#1
I need to resize a picture to a fixed size. But it has to keep the factors between the width and height.

Say I want to resize a picture from 238 (w) X 182 (h) to 210 / 150

What I do now is:

Original width / target width = 1.333333
Original Height / target Height = 1.213333


Now I take the smallest factor.

Now I always have the right width since 238 / 1.333333 = 210.
But the height is still 160.

How do I get the height down to 160 without ruining the pic?

Do I need to crop? If so how?

posted date: 2009-04-14 03:15:00


Re: Resize/crop/pad a picture to a fixed size#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-14 03:15:01


Re: Resize/crop/pad a picture to a fixed size#3
Do you have Imagick? If so you can load the image with it and do something like thumbnailimage()There you can skip either of the parameters (height or width) and it will resize correctly.

posted date: 2009-04-14 03:22:00


Re: Resize/crop/pad a picture to a fixed size#4
well i prefer to use gd since that's what I am using for everything. don't know if i have imagemagick

posted date: 2009-04-14 03:24:00


Re: Resize/crop/pad a picture to a fixed size#5
Maybe take look at PHPThumb (it works with GD and ImageMagick)

posted date: 2009-04-14 03:27:00


Re: Resize/crop/pad a picture to a fixed size#6
You(ll have to crop 5 px off the top and bottom to get to your target size, however this could ruin the picture.Really you should have a target width or height, then adjust the other dimension by the same proportion.

posted date: 2009-04-14 03:30:00


Re: Resize/crop/pad a picture to a fixed size#7
yes but then the height or the width might not corrspond to its required value

posted date: 2009-04-14 03:33:00


Re: Resize/crop/pad a picture to a fixed size#8
This doesn(t crop the picture, but leaves space around the new image if necessary, which I think is a better approach (than cropping) when creating thumbnails.$w = 210;$h = 150;$orig_w = imagesx($original);$orig_h = imagesy($original);$w_ratio = $orig_w / $w;$h_ratio = $orig_h / $h;$ratio = $w_ratio > $h_ratio ? $w_ratio : $h_ratio;$dst_w = $orig_w / $ratio;$dst_h = $orig_h / $ratio;$dst_x = ($w - $dst_w) / 2;$dst_y = ($h - $dst_h) / 2;$thumbnail = imagecreatetruecolor($w, $h);imagecopyresampled($thumbnail, $original, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $orig_w, $orig_h);

posted date: 2009-04-14 03:35:00


Re: Resize/crop/pad a picture to a fixed size#9
I(d much rather resize so that the image is contained within your limit and then fill out the blank parts. So in the above example you would resize so that the height is OK, then fill up (7 pixels on each end I think) to the left and right with a background color.

posted date: 2009-04-14 03:36:00


Re: Resize/crop/pad a picture to a fixed size#10
Resizing images from within a PHP-sourced webpage can be problematic. Larger images (approaching 2+MB on disk) can be so large that they need more than 32MB of memory to process.For that reason, I tend to either do it from a CLI-based script, with up-to 128MB of memory available to it, or a standard command line, which also uses as much as it needs.# where to put the original file/image. It gets resized back # it was originally found (current directory)SAFE=/home/website/PHOTOS/originals# no more than 640x640 when finished, and always proportionalMAXSIZE=640# the larger image is in /home/website/PHOTOS/, moved to .../originals# and the resized image back to the parent dir.cd $SAFE/.. && mv "$1" "$SAFE/$1" && \ convert "$SAFE/$1" -resize $MAXSIZE\x$MAXSIZE\> "$1"(convert( is part of the ImageMagick command line tools.

posted date: 2009-04-14 03:38:00


Re: Resize/crop/pad a picture to a fixed size#11
Have you tested this since in my example the heigth is still 160 px and not 150 as it should be

posted date: 2009-04-14 03:43:00


Re: Resize/crop/pad a picture to a fixed size#12
Having specific image sizes is not a good idea when you don't have complete control over the source of the images. You can only scale them to fit.

posted date: 2009-04-14 03:48:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.