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.
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.
Given a large image (let's say at least 1200x800px), how can I generate smaller centered square and scaled thumbnails?
I need to generate several thumbnails for this image:
300x300 square (centered, preferably)
100x100 square (centered, preferably)
600 max (width or height, scaled proportionally)
100 max (width or height, scaled proportionally)
Is there a linux or php library that will help accomplish this?
Sample Image: http://placehold.it/1200x800
Check out PHP Thumbnailer. Simply create one thumbnail for each size you want.
Example Usage:
$thumb = PhpThumbFactory::create('/path/to/image.jpg');
$thumb->resize(100, 100); // resize to 100x100
$thumb->save('/path/to/100x100.jpg');
Simply do that for each size you wish to save.
Check out the docs for more examples.
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 need to take a photo someone has uploaded through a form, resize, merge with a high-res frame # 300dpi and keep it all # 300dpi for the best quality for print.
is it possible to handle high-res images through GD and if so, could you provide some information on how? I have code that already does the resizing and merging, but i'm not sure if its going to work at the correct dpi.
Thanks
It's basically possible: Just use the proper amount of pixels. (The dpi unit has no meaning in digital imaging, it serves only to convert a digital [pixel] image into a physical format).
Example:
11 x 8 inch canvas #300 dpi = 3300 x 2400 pixels
However, you'll need plenty of memory to deal with images this large. The rule of thumb is
needed bytes = width x height x 3 (or 4 if alpha-channels are used)
this requirement increases if you do copying or resizing, because the script needs to keep multiple copies in memory.
You will need a very generous memory_limit setting for this to work.
Also note that GD can only deal with RGB images.
For large images and CMYK data, ImageMagick may be a better option if you can use it on your server.
DPI is a conversion factor for print purposes. It has absolutely no bearing on how GD will see the image. An image that is 200 pixels by 150 pixels will always be 200 pixels by 150 pixels, whether it's 10dpi or 5000dpi.
however, when you print out the image, 200x150 # 10dpi would make for 20 inch by 15 inch image (300 square inches), while the 5000dpi version would make for 0.04x0.03 (0.0012 square inches).
The only limitation is that you have to have enough memory available for PHP to hold the DECODED image contents in memory. a 24bit 1000x1000 pixel image requires 1000x1000x3 = roughly 3meg bytes of memory, at mininum (plus internal overhead). Then extra memory to hold the new images which will have the results of your image manipulations.
The default image dpi on a web page is 72 or 96 ( Mac or Windows/Linux ). You can specify the width and heigth of a image using the width and height attibutes of the img tag and generate the image with the desired dpi within.
<img src="/images/image.png" width="300" height="200">
Let's say that your default screen resolution is 72, than make a $resdpi variable:
$resdpi = $resolution / 72;
then, make the image on GD multiplying the width and height by that variable and you'll get a large image that will appear like a default 72dpi on screen, but will print with much more resolution.
GD is all about pixels, DPI does not come in to it, as that requires some sort of device output. If you know your final print size, then just ensure that the dimensions in pixels scale correctly at the DPI you require.
Generally, if you are using large images, your main problem is going to be efficiency, both in speed and memory usage. I would recommend using ImageMagick for more complicated jobs.
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).