Determine DPI for an Image with PHP - php

What is the fastest and easiest way to determine the DPI of an image in PHP using either ImageMagick or GD? Although it would be nice to determine the DPI of a wide variety of formats, at the very least, JPEG and PNG should be supported.
Similar questions:
How to Check Photo DPI with PHP
JPEG only solution

Related

PHP GD imagescale quality

I have a script scaling down high resolution photos. I use GD with the imagescale function with IMG_BICUBIC_LINEAR algorithm. After downscaling, I sharpen the images with a convolution matrix.
But I am not happy with the quality. Images look worse than resized with other tools. Is it impossible to get good quality with GD functions or do I do something wrong. Should I just use imagick?
The easiest solution was to start using Imagick. PHP GD functions cannot be customized enough to produce better quality.

PHP: How to compress images without losing visible quality (automatically)?

I'm wondering how to figure out the best compress rate (small filesize + no quality loss) automatically.
At the moment I'm using imagejpeg() with $quality = 85 for each .jpg.
PageSpeed (Chrome Plugin) suggests, to lower the quality of a few images to save some kb. The percentage of reduction is different.
I'd like to write a cronjob that crawls a specific directory and optimizes every image.
How does PageSpeed or TinyPNG figure out the best optimized quality and is this possible with PHP or another serverside-language?
TinyPNG uses pngquant.
Pngquant has option to set desired quality, similar to JPEG. You can run something like:
<?php system('pngquant --quality=85 image.png'); ?>
Pngquant website has example code showing how to use pngquant from PHP.
For JPEG you can apply lossless jpegcrush.
JpegMini (commercial) and jpeg-archive (free) are lossy and can can automatically find a minimal good quality for a JPEG.
In PHP you can roughly estimate how much JPEG was compressed by observing how much file size changes after re-compression. File size of JPEG recompressed at same or higher quality will not change much (but will lose visual quality).
If you recompress JPEG and see file size halved, then keep the recompressed version. If you see only 10-20% drop in file size, then keep the original.
If you're compressing yourself, use MozJPEG (here's an online version).

Php - clear PNG metadata but keep similar quality

I want to convert every uploaded image to png. I think gd is a good approach for that, because it drops the metadata of the images, and does not try to parse it. I read that ImageMagick maybe has malware vulnerability on some linux servers...
I have 2 questions:
Does gd drop the PNG metadata too if the original file was PNG, or should I use pngcrush after converting?
Do I have the same quality loss as by saving JPEG files, or is the PNG format much better?
If you would open the png in PHP, copy it to a new resource, then save it at full quality (in PNG, JPEG will lose quality) it will do what you want.

Where can I find comprehensive image manipulation library information?

I've spent a lot of time Googling and searching SO for information on image manipulation libraries with little success. Please direct me if this has been answered before and I just can't find it.
Basically I'm trying to resize an arbitrarily sized image to a couple of smaller thumbnail images, say 400px wide and 200px wide while maintaining the original aspect ratio. The original image is being uploaded via php (linux) and I've found that I can use Cairo, GD, GMagick, or ImageMagick but I've been unable to find comprehensive data on which is better suited for image manipulation. I have found comparisons for image creation, but that's functionality I won't be using.
I also have the option of uploading via php then performing the image manipulation via another method (perl/python/etc, for example) if that proves better suited.
Any pointers in the right direction are appreciated. Quality is my primary motivation followed by output image file size then library performance.
GD and ImageMagick are your best bets, so get some representative images and run some tests using:
imagecopyresampled() for GD
Imagick::thumbnailImage() for ImageMagick
Imagick::resizeImage() if you want more control and to try a few filter constants.
Resizing images in PHP with GD and Imagick has some code samples to get you going.
Compare them visually and by filesize. If there's no obvious winner, then run some benchmarks based on your expected needs. How to benchmark efficiency of PHP script is a good reference point.
Also take a look at:
Choosing between Imagemagick & GD for thumbnail creation
Should I use ImageMagick or GD2 with ImageAPI in Drupal?

layered png image using a PHP library

Does anyone know about a PHP library which can produce layered PNG images? (I'm not sure, but I think that tiff image standard supports layers also)
AFAIR, PNG does not support layers. MNG does and you can produce these with ImageMagick. For PHP extension see: http://www.php.net/imagick
From the ImageMagic API site:
PHP
MagickWand for PHP a native PHP-extension to the ImageMagick MagickWand API.
IMagick is a native PHP extension to create and modify images using the ImageMagick API. Documentation for the extension is available here.
phMagick is a wrapper class for ImageMagick, wrapping the most common web image manipulation actions in easy to use functions, but allowing full access to ImageMagick's power by issuing system calls to it's command-line programs.
ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.
Assuming you mean the layered PNG files as per fireworks, I don't think you can do it. I've not seen anything except fireworks that supports these files properly.

Categories