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.
Related
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.
i have image with various size. and i also have various container to place those image.
example : i have 680 x 1024 image that will placed on 500x500 container without cropping.
so i thought that i will need to build image with container size, than put resized image on top of it.
result that i expected is something like this
or this
how the best way to achieve this on PHP or wordpress?
I wrote a plugin, currently at WordPress plugin repository:
JResizr
This plugin have ability to override default worpdress behavior and disable croping image but try to resize image to fit rectangle container size and fill space with background color. You also able to choose background color to use.
There are several ways to accomplish this. One alternative to javascript/php, if the images are roughly the right size, is just use CSS, specifically background-size: contain
contain
This keyword specifies that the background image should be scaled to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area.
You can experiment with it at http://jsfiddle.net/RxTLS/
Keep in mind this is mostly supported, although IE8 and below do not. I believe there are some polyfills our there though if you're worried about IE users.
This solution is only ideal if the images you are uploading are close to the size they will be displayed however. Trying to do this with images that are over 3000 pixels wide/tall is not advised for several different reasons.
In that case, you'd want to shrink them in PHP first. If you're uploading the images through Wordpress then you can use add_image_size in your functions.php and the images will be automatically resized when you upload them. Alternatively, you can do it manually in PHP as well and there are plenty of tutorials on how to do that out there. I'd just google PHP image resize.
In your html where you have <img> tag, add a width attribute, and set it to 100% that should auto resize your image to its containers size
<img src="imageNamw.jpg" alt="imageAlt" width="100%" />
You could write a simple Js file that would accomplish that.
I have done such thing before; So basically you compare the height and the width of the image in its container. then if the height is longer than the width you set the height to 500px and set the width to auto;
else if the width is longer than the height you set the width to 500px and the height to auto;
else you set the width and the height to 500px;
set the container vertical align to middle and text-align to center in css and that should do the trick;
You can also try a simple timthumb library to resize images in any size.
Please check the timthumb library code here :
http://timthumb.googlecode.com/svn/trunk/timthumb.php
In WordPress it would be simply by using their image_resize function
image_resize( $file, $max_w, $max_h, $crop, $suffix, $dest_path, $jpeg_quality );
where you would set the file, width of the container, height of the container and false (as you want to resize, not crop). The others are optional and you should fill them if you have special needs for the new file destination or name or quality. If you entered everything correctly, the function should return path to your newly resized image.
Note that with WordPress you can actually do this automatically when uploading pics so then later you just retrive the already resized picture - take a look at add_image_size function.
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.
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;
I have an image that I want to be watermarked to the bottom right section of other images.
The dimensions of the watermark image are 179 width, 39 height.
Now what if I have another image whose dimensions are 150 width, 20 height? If we tried to watermark it using the original image, it would obviously be too large and the image itself would be masked completely by the watermark image, right?
So how can I determine a smaller width and height to which I will resize the watermark image, something much less than 150 width, 20 height, so that it will appear still as a watermark and wont mask the image completely?
You need to pick a maximum percentage of height and width of the smaller image that you can allow the watermark to be, then scale watermark image to the smaller of these two maximum values. Your results will vary based on whether the target image is taller than it is wide, or vice versa.
For example, if you want the watermark to be no more than 25% of the height and no more than 50% of the width of the target image, you can see how big the watermark should be if you scaled it to either dimension.
Scaling to 50% of the width would mean the watermark would be 75 x 16 pixels, which is too tall (based on the percentages I selected arbitrarily).
(75 / 179) * 39 = 16
Scaling to 25% of the height would mean a watermark of 22 x 5.
(5 / 39) * 150 = 19
If the dimensions end up being larger than the original watermark, it's up to you whether or not to scale the watermark up. Image quality degrades much faster when increasing the size of an image, compared to decreasing its size by the same factor.
First out, I recommend you to generate a set of watermark images of different sizes instead of resizing 'on the fly'.
Below I have outlined a workflow of how to watermark images dynamically on a website:
Design a good watermark
First of all, try to design the watermark with a transparent background. This will greatly reduce the risk of cover up essential parts of the target image. This can be done by using the gif or (preferably) the png imagefile format. Just make sure that the transparent watermark works well both on light and dark backgrounds.
Also take into account how to best design the watermark that works both for portrait and landscape style images and images with awkward aspect ratios. You should consider to make two versions -- one for wide images and one for tall images. For the latter type, you could rotate the watermark 90 degrees or, if the watermark consists of text, you might want to split the text into two or more lines.
Pre-render the watermark in several different sizes
So, don't dynamically resize the watermark, instead I recommend you to render a set of watermark images with different sizes. This only has to be done once and it will greatly enhance the clarity and/or legibility of the watermark (especially for smaller target images).
Depending of how different the images will be on the web page, you might need a larger or smaller number of sizes. This is a design choice that you will have to make, but I think you could get away with only two or three different sizes.
Apply the watermark
This will happen dynamically on the server side (in your php-file). First find out the dimensions of the target image by using the function getimagesize. With this information at hand, you'll have to decide which version of the watermark to use based on the size, aspect ratio and orientation. E.g.
if ( $width > $height ) {$useLandscapeWatermark=true;}
if ( $width > 100 && $width < 400 ) {$watermarkSize=2;}
etc.
Finally, to apply the watermark I recommend you to look at the gd library. This is a powerful library that can do many neat trix, among others merging two images. An alternative is ImageMagic.
Good luck!
I would probably scale the watermark to 20% of the image being watermarked, or 179x39, whichever is smallest.