Image Processing & Creation in PHP - How To Create 300DPI Images - php

When creating images using the GD library in PHP ie) imagecreatetruecolor() what is the DPI of the resulting image? I haven't been able to find anyone specifying where the dpi can be set or what it defaults to.
I require a 300DPI tiff or jpeg to be created and then saved to the file system from the program.
If this isn't possible using the GD library, is there another that would work for this situation?
Thanks for your help
Edit: Yes this is creating an image - I would like to have a 300dpi file saved from the script not have to open up each file in photoshop to specify the dpi.

I don't think there's a way of setting DPI with GD. The DPI of an image is specified in the leading bytes of the image data - I believe for JPEG images that's bytes 15-18. Bytes 15-16 are horizontal DPI, 17-18 vertical. The values are stored as octals.
I'm a bit ropey with byte-level editing, but you could resize the image in GD to the target pixel size and then edit the file to adjust the DPI.

I believe its always 72dpi. So you should multiply your pixel dimensions acoordingly to produce the desired resolution image.

Related

How to resize an image to a smaller size in codeigniter for a single Image upload?

I want to resize the images during upload to a small size so that they take less time while loading on the website.
And I also want to maintain the aspect ratio so that a portrait image doesn't get stretched and a landscape image doesn't get squeezed.That is without changing the actual dimensions of the image.
An Image Manipulation class exists in CodeIgniter and can solve Your problem. Just read the official guide to learn more.
First convert your images to Jpeg, gif and png images as these are compressed images.
In addition you can use ImageMagick to compress the image: Recommendation for compressing JPG files with ImageMagick

print quality images with php and GD

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.

How to define DPi for a new JPG image?

I'm going to create a new JPG image with PHP. How can I set the DBi value of a new JPG image.
Dirk
JPG is a Pixel format that doesn't really know physical dimensions like Dots Per Inch.
All you can do is write a dpi value into the file's Metadata as assistance for further processing, but it's just a recommendation and it can't be done using the GD library.
You would have to use an third-party library for this. See e.g. the second answer to this question: How do I add exif data to an image?

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).

PHP GD issues with ImageCreateTrueColor and PNGs

I am resizing PNG images using the GD image library function ImageCopyResampled(). It all works fine, I can even keep alpha blending transparency with the use of ImageCreateTrueColor() rather than using ImageCreate() to create the resized image.
The problem is, that if I use ImageCreateTrueColor() rather than ImageCreate() the file size of PNG files increases from something like 80kb to 150kb. If I use ImageCreate() the file size stays around the same size, but colors screw!
So my question is, how can I retain alpha blending when resizing PNG images without increasing the file size?
Oh and I am reducing the dimensions of the PNGs.
With imagecreate() you're creating an indexed-color PNG file and with imagecreatetruecolor() you're creating a 24-bit color PNG file. Of course the resampling quality is going to appear much better with the true color image, since it has a much larger range of colors to use when resampling. With imagecreate(), the system can only use the much smaller palette.
You can try this out using Photoshop or Gimp, scaling images in the different color modes (indexed and RGB). Unfortunately it's the nature of the game-- the file size will be larger when there are more colors to store.
I'm not sure if it would make a difference, but you could try using imagecopyresampled() to copy to a true-color resource (from imagecreatetruecolor()), then copy (but not resample) that to a palette image resource. This way the palette is determined based on the resampled result. I'm not sure that you'd be able to retain the alpha channel, though.

Categories