How to define DPi for a new JPG image? - php

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?

Related

How to change the shape of an image using PHP and imagemagick?

I need to manipulate images on the server-side via PHP. I use ImageMagick for that purpose, and there are a lot of great functions, but no documentation...
The issue is the next:
I have two images a png with a transparent background, and a jpg one;
I need to change the JPG image that has the same shape as the visible part of the PNG.
I don't want to crop the image, but to adjust to the PNG's visible part. Is that possible to do?
Example:

Clean solution to set DPI on created PNG images

I would like to create PNG images using PHP on a website. These shall be printed at a defined scale. So I would like to set the DPI value of the images using PHP directly. Unfortunately I did not find any function call for this.
Is there any function that can set/update metadata of PNG files?
Maybe an other solution is more reasonable as using a HTML-Wrapper with CSS style sheet for printing which externally defines the resolution. But I would prefer the "directly on the image" approach...
PNGs can contain arbitrary headers. If you look at the PNG specification, you can add tEXt blocks (which are called chunks) to a given PNG. See section 4.2.3 of the specification for more information on tEXT chunks.
As an example, Adobe Photoshop adds meta XML to its PNGs. I'm not sure if GD supports this, but I'd look there to start. It's definitely possible.
Here is some PHP code that deals with parsing PNG chunks. It might steer you in the right direction. http://code.svn.wordpress.org/imagelibs/libpng.php
Here's an screenshot for a text editor of a PNG, showing the XML that was generated by Photoshop. https://stackoverflow.com/a/14356339/278976
THe pHYs chunk (Physical resolution) lets you set a DPI (well, actually pixels by meter, but it's just a unit conversion). Of course, the PNG reader might ignore it.
PHP does not include (AFAIK) support for reading/writing full PNG metadata, you must do it yourself, see eg
The easiest way is to use ImageMagick, as suggested in this answer. If You want to set PNG resolution in pure PHP, you may look at my answer to the similar question.

Image print quality check

Usual basic criteria for images in digital print are
CMYK
300 dpi
right scale
Is there a possibility to compare user uploaded image to placed print standard?
(Using PHP and perhaps some extension e.g. ImageMagic)
Idea is if user uploads bad print quality image e.g. jpg 100x100px # 72dpi notice user in proper way. But after all research I have made, couldn't define some model how to extract uploaded image data and do compare/check.
Any idea how to aproach this problem would be appreciated
You have Imagick::getImageResolution to get the resolution in dpi and you have getimagesize, there you check for the channels, 4 is cmyk, 3 rgb.

phpThumb - change watermark size

I'm using phpThumb in a script to clean my images and add a watermark to them. We have images of very different sizes (from 100px width to 800px), so no matter what watermark image I use, it's either going to look too small or too big on the image.
Do you know of a way to tell phpThumb to resize the watermark? Or is there a way to resize the watermark image (depending on size of image being watermarked)?
Thanks a lot guys!
Ali
I am facing the same issue.
Maybe you could put some if statements in the phpthumb.config file in the DEFAULT PARAMETER SECTION
then depending on the height and width parameters you can decide to use different files with varying sizes for the watermark.
make sure you change $PHPTHUMB_DEFAULTS_GETSTRINGOVERRIDE to false
This is not perfect cause you could not know the source image size to start with.
but it would be a better fit based on the parameters passed to phpthumb.
I guess the only other solution would be to use phpthumb as an object but that would require a lot of work.

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

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.

Categories