General Image Optimisation for the Web in ImageMagick - php

I currently do a strait copy() of images that are the correct width/height to go into my site. I was wondering what sort of best practicies in Imagemagick should I be doing to ensure filesize is lowest it can be without loosing quality of the JPEG?

What you can do is that identify the current image quality & size before you do your copy and compression
Example
identify -verbose rose.jpg
Would return all information including
Compression
Quality
Resolution
Depth
File Size
etc ...
To do your own Optimization
Don't just use fixed values .. used the information too calculated possible and ideal compression and size for the image
Example
If an image quality is 70 and 60 is your bench mark .. all you need so do is reduce it by 10% and its it is 100 reduce by 40% .. at all times you would images with the same level of quality

I resize on upload as even if you read a jpg into Imagemagick and save without doing anything it recompresses it.
If you resize and use -resize all EXIF data is kept; using -thumbnail all EXIF data ( including the embeded thumbnail ) apart from the colour profile is stripped. You can also use -strip on its own which will remove all EXIF data and the colour profile.
To keep the quality in jpg use -quality 100 so you could use something like:
convert input.jpg -strip -quality 100 output.jpg

Related

PHP ImageMagick png to eps conversion with transparent background

I have a png image with transparent background and i tried to convert that to an eps file using convert command of imagemagick,and got eps image with white background.
I want eps image with transparent background.I have a php application for designing images for tshirt printing,So image transparency is must.
Also image quality of output eps image is pretty bad and image size is really big comparing to input size.
I had tried command
convert test2.png -resize 1024x1024 -density 500 -units pixelsperinch -quality 100 -compress none -alpha set -flatten -background none -depth 20 test2_eps.eps
Iam using system function of php to execute this command.
Is there any option to maintain transparency of input image? Is it white by default? How can we improve output eps image quality?
PostScript (and therefore EPS) doesn't have a concept of transparency (with some exceptions), so you can't readily convert an image with an alpha channel to an EPS.
If the 'background' is 100% transparent it is possible to create a similar effect in PostScript using masked images (that's 2 images, where the second is a mask that determines where the 1st image draws). That's a rarely used feature, and requires a level 3 PostScript interpreter. Most EPS export functionality doesn't go above level 2.
As to whether ImageMagick can create such an EPS, I have no clue, but I wouldn't be at all surprised to find it cannot. You may have to use something like Photoshop.
Regarding the image size; PNG is a compressed format, and you have specified
-compress none
So its hardly surprising that the output is larger!
Its essentially impossible to comment on your quality problem, partly because you haven't really said exactly what it is that you see as a problem. 'Quality' is rather vague, perhaps you could post an example.
If your EPS has a thumbnail preview do not be fooled into thinking this is what the EPS contains, its a low resolution bitmap preview only and its there for the benefit of EPS consumers that can't interpret the PostScript content. So that they can preview the result when you place the EPS in the final document.

ImageMagick - Smaller resized image is a much larger file size

In ImageMagick, I'm running a:
convert -trim -density 200 myfile.tif -resize 70% myconvertedfile.png
If I remove -resize 70%, the image size is only 78,527.
However, when the file is stored at 70%, the file size is 860,504
What makes the smaller image a larger file size, and is there a way to decrease the file size without quality loss?
Most likely your smaller image has a larger number of colors due to interpolation of pixels during the resizing operation. To resize without adding colors, use -sample 70% instead of -resize 70%.
Your tiff may be compressed, Imagemagick, then decompresses and outputs to png. The resulting png compression may produce a file size larger than your tiff compression. Also, the tif may be 8-bit color, but when resized, it gets new colors and so must be saved as 24-bit color into PNG. I suspect this latter.
Also you should use proper ImageMagick syntax, which reads the input image before applying settings and then operators. However, ImageMagick 6 is forgiving in this regard.
convert myfile.tif -trim -resize 70% -density 200 myconvertedfile.png
If you want an 8-bit result, then you can use PNG8:myconvertedfile.png for your output.
When specifying density, you should specify units. PNG only stores units of pixels per centimeter, but will convert density and pixels per inch into the correct pixels per centimeter.
You could replace -resize with -sample and not interpolate pixels to new values. -sample will just grab every nth pixel.

resizing images - imagemagick using - quality and jpeg:extent in one command

I am using imagemagick to resize images...
however I want to resize the images so that it uses both jpeg:extent and quality...
i.e. if I have a large image that i want to resize, and I use the following:
-resize 720 -quality 80
if the resulting image is larger than 250kb then I want the command to use jpeg:extent=250kB instead of -quality 80
Is this possible to do in one command?.. or do I have to do multiple proceedures to achieve this?... regards J
By the by, I dont want to just use jpeg:extent=250kB as if the image saved at 80% quality is alot smaller than 250kb i am creating images larger than they need be... cheers J
You can use both options at the same time. According to documentation:
Restrict the maximum JPEG file size, for example -define
jpeg:extent=400kb.
This command doesn't define the actual size, it only defines a size limit. This means that quality will be 80 if the size limit is not reached

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.

Is it possible to perform a lossless rotation on a JPEG Image in PHP?

I need to rotate some existing JPG images. They have already lost some detail, but I now want to rotate them and lose no further detail.
With a little research, it seems the only lossless Image rotation library for PHP is by using the jPegTran library.
Are there any other options when it somes to doing lossless jpg rotation?
Thanks!
Would't it be possible to call an external program say losslessrotator by exec('commandline');
Another option would be jpegtran by jpegclub
Be careful about jpegtran when rotating cw or ccw by 90 degrees, it will not rotate all of the pixels as expected, as it can do losslless rotation only within area which dimensions are the multiple of jpeg block size (8x8 pixels usually). It rotates pixels inside each of these blocks internally to avoid re-compression of the image, but the edge blocks can't be rotated like that. So with jpegtran -rotate 90 or 270 you will be left with a tiny strip of un-rotated pixels on the edge, and you need to use -trim option to get rid of them, but then the resulting image will be a few pixels smaller than the original.
So while it is a lossless rotation, you still end up loosing some pixels in the process.
JPEG is a lossy format, so the answer is no, you can't create a lossless rotate of JPEG on any application, programming language, or guru meditation.
What you can do, however, is minimize image data loss by using the $quality argument when saving the rotated JPEG, if you're saving it in JPEG format that is. If you're saving it in lossless format, then you've already minimized image data loss.
Example:
$img = imagecreatefromjpeg($file);
$rot = imagerotate($img, 90, 0);
imagejpeg($rot, $output, 100); /* set quality to 100% */

Categories