Resize uploaded image to max width, max height? - php

i have a php image upload upload form and what i want to do is before the image is saved on my server, it needs to be resized to make sure the height doesnt exceed the max height ive set, and the width doesnt exceed the max width i set. I was thinking the way to do it would first be a) check if the width is greater than max width. If so, PROPORTIONALLY resize image so the width equals the max width. Then, check the height. If the height exceeds max height, proportionally resize the image so the height equals the max height.
Any suggestions? GD is installed on my server..
Assume that the image is echo $_FILES["file"]["name"];

Using gd is as lightweight as it's going to get. The idea that you're going to resize images without using a library is basically just humorous.

I've answered a similar question to what you are asking:
Resize images with PHP
It uses GD b/c like chaos says, it doesn't get any more lightweight.
This script resizes an image on demand, but caches each size of each image that has ever been called so that it doesn't have to be resized again.

Related

Resize an image setting min width and max height

I am using ImageMagick with PHP to resize images (using its commandline tool "convert").
I am able to resize images and everything works.
What I want to do is I want to resize an image (of any dimension) to an image setting the minimum width and maximum height and at the same time preserve the image resolution/aspect ratio. How do I do this?
Why I want this: I am putting these images in a news feed in my website where I require images to be of width:100% but the problem is height becomes too large for certain images and occupies an entire page. I want to avoid it.
This is what I am using for resize:
$cmd="convert -thumbnail ".$width."x".$height." \"".$source."\" \"".$dest."\";";
You can find the resize options on the Imagemagick website: http://www.imagemagick.org/script/command-line-processing.php#geometry
I do not think these will achieve what you want so I would check the dimensions of your image and calculate the height to go with your width which you can use in your convert code.

To understand math behind resizing an image while keeping the ratio

i don't know if this question fits here but i want to understand the formula for resizing an image while keeping the ratio in PHP GD library or in anything else.
For example here is an example:
http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html
In this example if "target_aspect_ratio" is bigger than "original_aspect_ratio" height is targe_height and width is calculated by target_height * original_aspect_ratio.
If "original_aspect_ratio" is bigger than "target_aspect_ratio" target width is target_width and height is calculated by target_width / original_aspect_ratio
Why is that?
The way that I always resize images when maintaining the ratio is to use an algorith like the following:
$imgHeight=600; // Or the actual image height.
$imgWidth=300; // Or again the width of the image
$imgRatio=$imgHeight/$imgWidth;
Then to resize the image you can use the following:
$newHeight=1000;
resize($newHeight, ($newHeight/$imgRatio));
// assumes Height x Width in the resize command.
With this method, you get the ratio of the original image, then apply it to whatever size you need.
Edit:
If you are doing thumbnails, you often will want to keep the image size of all the thumbnails the same exact size - so they line up nicely on a page. I would suggest resizing the image so that the resized image fits INSIDE the actual thumbnail - basically giving it space on either the top or bottom, and then fill that in with a background color or leave it transparent so that it works with the rest of the site.

How to resize the image in better way using PHP?

I want to resize the image without affecting its quality using PHP.When I trying to do with imagick or GD library in PHP if I give the fixed width or height its going to be affect the quality of image.So when I try with some other sites they are doing like depend upon the image size they dynamically resize the images.ie if I upload image in two different size when re-sizing both the images contains different width and height .But in my application when I give the thumbnail width 640(both imagic and GD library) then I will get the images with 640 image size.I want to know how do they dynamically calculate the image width and height for resizing?And also when resizing very low size image how can I achieve the quality?
If you want to keep the aspect ratio of the image while resizing it, you can keep only one of the width or height as constant while other one you need to calculate based on the aspect ratio.
For example if you want to make the image width as 640, you can find the targetHeight using following formulta
targetHeight = sourceHeight/sourceWidth * 640;

php resize thumbnail to fixed dimensions - no crop

I want to resize an image to a fixed size of 400x300.
I do not want to zoom crop it, I simply want the image constrained to either the width 400 or height 300 (depending on orientation), the image centered, and the rest of the image filled with black.
Does anyone know of a library/code that does this? (for GD)
Thanks,
Wesley
GD or ImageMagick can easily do this for you.
Create a new image with a black background of the desired height, then copy from the original, and place in the new.
Perhaps another answer will come along with the full code, but this will get you started. If not, I will post code for you later.

image resize but without any compromise in DPI?

I want to resize a image with DPI 300 or greater..
I want it's DPI to remain intact...
I have used GD library function to resize image cropped but it brings down DPI to 90!
please give a solution as my requirement involves no downsizing of DPI
Take image, say it's 1000 x 1000 Pixels large
Crop / resize image to a portio, say, 100 x 100 Pixels large
Enlarge 100 x 100 portion to 1000 x 1000 Pixels - use imagecopyresampled() for best results - manual
Done!
This comes at the price of lower quality, obviously.
It's going to be impossible to enlarge an image with no quality loss. You won't be able to retain the original image quality when enlarging because the pixel information simply isn't there. There are resizing algorithms that employ antialiasing and other techniques (like resampling in imagecopyresampled() to help the quality, but it will never be perfect.
If you want to display a large image smaller without losing any image data, you would put it into a img tag and then scale it down using the css width property. Note: This is not going to give you any better quality than resizing the image. In addition you are transferring more data than necessary, and in some browsers the result of the resizing may look bad due to the use of low-quality (but fast) algorithms - so the image may end up looking worse.
DPI means dots per inch. If you resize the image, the amount of inches stays the same (you show the same image), but the amount of dots goes down (less pixels).
So if you lower the size of the image, you always lower the DPI.
So if you lower the size of the image,
you always lower the DPI.
thats not true
the DPI is just a "conversion" used when you print something, it says nothing about the quality of the picture onscreen.
if you resize a picture (pixelsize that is) the printed versions shrinks the same way if you don't touch the DPI.
you could keep the printed size the same, but that would mean lowering the DPI
1200*1200px image with 300dpi is 4"*4" printed
600*600px image with 300dpi is 2"*2"
600*600px image with 150dpi is 4"*4"
if you use imagecopyresampled() the DPI should stay the same in the image
If you want to cropp an image and retain the source image PPI on the cropped area (which will be a new image) you can use Imagick following these steps:
// copy the original source to a new image, on which you'll be working
copy('example_source.jpg', 'example_cropped.jpg');
// set the resource path to work on
$resource = new Imagick('example_cropped.jpg');
// cropp the image
$resource->cropImage($cropp_width, $cropp_height, $left_offset_in_px, $top_offset_in_px);
// save the image (in this case, the same path as the one we're working on)
$resource->writeImage('example_cropped.jpg');
This is the most reasonable method I've found, since imagecopyresampled and imagejpeg seem to change the PPI of an image to the default 96 ppi (even for sources, with 300 ppi).

Categories