PHP GD imagescale quality - php

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.

Related

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

Determine DPI for an Image with 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

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?

What PHP compression utility and settings will mimic Photoshop's "Save for Web"?

I've tried about a half dozen compression utilities and have been unable to get anywhere near the compression level that Photoshop's "Save for Web" feature gets (5-10x smaller). I think the problem is that most of the utilities can't change bit depth of the image as part of the compression process.
I'd like to use a PHP compression utility (if possible).
My settings on Photoshop's "Save for Web" are:
png-8
selective
diffusion
no transparency
64 colors
100% dither
If it's a dramatic difference in size, then you are probably right and it's not reducing the bit-depth. ImageMagick is going to give you a good amount of control. The other thing that PS "save for web" does is strip most of the meta-data, but that savings is usually pretty minimal.
Using ImageMagick you can use the format png8:filename.png for 8-bit pngs, you'll have to look at the documentation to get the rest of the attribute settings, but note that for PNGs, the quality setting is not the same as lossy formats like jpg, each digit represents a different png setting.
To use, install the ImageMagick library and either run the commands via one of the PHP exec functions or install the PHP PECL extension, imagick.
Imagemagick is a nice PHP image utility and is used by most developers for advanced Image algorithms. I would recommend looking into this utility for further image manipulation needs. Specifically Imagick::setCompressionQuality
PHP Imagemagick
convert -thumbnail -quality '70%' from_path to_path
convert - console variant of ImageMagick...
just play with settings...

ImageMagick - what do i need to be aware of?

Even on max quality GD just wasn't good enough quality, so i'm pretty sure i'm going to have to go down the ImageMagick route... I've heard stories....!
What kind of things no i need to be careful about/aware of when using/installing ImageMagick?
Nothing. Just download and run.
Also, I hope you have tried imagecopyresampled of GD, not the other resize function.

Categories