imagemagick quality image issue using php - imagemagick

I have convert the PDF to image,I try using the exec,by excute the linux command like '/usr/bin/convert -density 300x300 file.pdf[0] -resample 160 -resize 512x700! images/sample-0.jpeg 2>&1'.But i have read some articles that it slow as compare to imagick php module.so try using php with code
$uploadfile = "file.pdf";
$im = new Imagick ( $uploadfile . "[0]" );
$im->setImageFormat ( "png" );
$im->writeImage ( "p-10.png" );
As you can see that i am not setting the desity and resample,the quality of image is not good at all.In php manual also there no function that set the density,i also try with setResolution,but does not work.
Please help if any body any idea about this.
have dream day

Related

how can we apply threshold effect on png image using php

I have some greyscaled png image or colorful images. i want to apply the threshold effect using php language.
I am still looking for the solution.
<?php
function thresholdimage($imagePath, $threshold, $channel) {
$imagick = new \Imagick(realpath($imagePath));
$imagick->thresholdimage($threshold * \Imagick::getQuantum(), $channel);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
}
?>

Why Typo3 generates poor quality by convert PNG to PNG with imagemagick?

All my sites works fine, after some updates in the last weeks I realize that the quality of PNG are very bad. I use Typo3 4.5.27 and imagemagick 6.6.0.4, Typo3 called following command:
/usr/local/bin/convert -strip -geometry 150x120! /var/www/introductionpackage-4.5.27/typo3/sysext/install/imgs/jesus2_transp.png[0] /var/www/introductionpackage-4.5.27/typo3temp/pics/install_scale_png.png
This is the output of this command
And this is the refference Image:
Can anybody explain what is wrong? Any suggestion to solve or debug the problem?
I try to update the imagemagick to current version but I still have this issue.
This is GFX section in localconf
$TYPO3_CONF_VARS['GFX']['gdlib_2'] = '0';
$TYPO3_CONF_VARS['GFX']['im_combine_filename'] = 'composite';
$TYPO3_CONF_VARS['GFX']['im_version_5'] = 'im6';
$TYPO3_CONF_VARS['GFX']['im_path_lzw'] = '/usr/local/bin/';
$TYPO3_CONF_VARS['GFX']['im_imvMaskState'] = '1';
$TYPO3_CONF_VARS['GFX']['im_negate_mask'] = '0';
$TYPO3_CONF_VARS['GFX']['im_path'] = '/usr/local/bin/';
$TYPO3_CONF_VARS['GFX']['im_stripProfileCommand'] = '-strip';
$TYPO3_CONF_VARS['GFX']['png_truecolor'] = '1';
$TYPO3_CONF_VARS['GFX']['im_v5effects'] = '0';
$TYPO3_CONF_VARS['GFX']['gdlib_png'] = '1'
Under Basic Configuration of Typo3 is following Content under GDLIB Section:
since the branch from ImageMagick many improvements have been made. It can also be installed over ftp if there is no shell access. http://wiki.typo3.org/GraphicsMagick

Imagemagick with greyscale images and 24 bit depth

I am using the Imagick library to work with ImageMagick in PHP. I am first reading an (JPEG) image from an external server with:
$img = new Imagick();
$img->readImage($source);
And then upload it to my Amazon S3 bucket with the following code:
$s3 = new AmazonS3();
$s3->create_object(BUCKET, $destination_path, array(
'body' => $img->getImageBlob(),
'length' => $img->getImageSize(),
'acl' => AmazonS3::ACL_PUBLIC,
'contentType' => 'image/jpeg'
));
Everything seems to be working fine, the files appear in my storage bucket and I can view them in my browser. However, when handling greyscale images, ImageMagick converts the image from 24 bit depth to 8 bit depth. I would like them to keep their 24 bit depth, how could I achieve this? I've tried the following, without success:
$img->setImageType(imagick::IMGTYPE_TRUECOLOR);
For colorized images, everything works fine, images keep their 24 bit depth.
Edit:
It seems that ImageMagick changes the image type from 6 (truecolor) to 2 (greyscale). Trying to overwrite this does not work, as tested with the following code:
$img = new Imagick();
$img->readImage($source);
$img->setImageType(6);
echo $img->getImageType();
which outputs 2
Use setType() before you load the image. So:
$img = new Imagick();
$img->setType(6); //or use imagick::IMGTYPE_TRUECOLOR instead of 6
$img->loadImage($source);
This will output an image with truecolor, even if the loaded picture uses graycolors only.

ImageMagick php imagick loading a file from memory

I m trying to integrate imagick into my framework. The framework has the file in memory.
The imagick code works great
/* Read page 1 */
$im = new imagick( 'test.pdf[0]' );
/* Convert to png */
$im->setImageFormat( "png" );
/* Send out */
header( "Content-Type: image/png" );
echo $im;
Now my problem is imagick constructor takes only path name. How can i load the pdf from memory.
like $im = newImagick($file);
$im = new Imagick();
$im->readImageBlob($file_contents);
http://www.php.net/manual/en/function.imagick-readimageblob.php

Google PageSpeed & ImageMagick JPG compression

Given a user uploaded image, I need to create various thumbnails of it for display on a website. I'm using ImageMagick and trying to make Google PageSpeed happy. Unfortunately, no matter what quality value I specify in the convert command, PageSpeed is still able to suggest compressing the image even further.
Note that http://www.imagemagick.org/script/command-line-options.php?ImageMagick=2khj9jcl1gd12mmiu4lbo9p365#quality mentions:
For the JPEG ... image formats,
quality is 1 [provides the] lowest
image quality and highest compression
....
I actually even tested compressing the image using 1 (it produced an unusable image, though) and PageSpeed still suggests that I can still optimize such image by "losslessly compressing" the image. I don't know how to compress an image any more using ImageMagick. Any suggestions?
Here's a quick way to test what I am talking about:
assert_options(ASSERT_BAIL, TRUE);
// TODO: specify valid image here
$input_filename = 'Dock.jpg';
assert(file_exists($input_filename));
$qualities = array('100', '75', '50', '25', '1');
$geometries = array('100x100', '250x250', '400x400');
foreach($qualities as $quality)
{
echo("<h1>$quality</h1>");
foreach ($geometries as $geometry)
{
$output_filename = "$geometry-$quality.jpg";
$command = "convert -units PixelsPerInch -density 72x72 -quality $quality -resize $geometry $input_filename $output_filename";
$output = array();
$return = 0;
exec($command, $output, $return);
echo('<img src="' . $output_filename . '" />');
assert(file_exists($output_filename));
assert($output === array());
assert($return === 0);
}
echo ('<br/>');
}
The JPEG may contain comments, thumbnails or metadata, which can be removed.
Sometimes it is possible to compress JPEG files more, while keeping the same quality. This is possible if the program which generated the image did not use the optimal algorithm or parameters to compress the image. By recompressing the same data, an optimizer may reduce the image size. This works by using specific Huffman tables for compression.
You may run jpegtran or jpegoptim on your created file, to reduce it further in size.
To minimize the image sizes even more, you should remove all meta data. ImageMagick can do this by adding a -strip to the commandline.
Have you also considered to put your thumbnail images as inline-d base64 encoded data into your HTML?
This can make your web page load much faster (even though the size gets a bit larger), because it saves the browser from running multiple requests for all the image files (the images) which are referenced in the HTML code.
Your HTML code for such an image would look like this:
<IMG SRC="data:image/png;base64,
iVBORw0KGgoAAAANSUhEUgAAAM4AAABJAQMAAABPZIvnAAAABGdBTUEAALGPC/xh
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
And you would create the base64 encoded image data like this:
base64 -i image.jpg -o image.b64
Google performs those calculations based on it's WebP image format (https://developers.google.com/speed/webp/).
Despite giving performance gains though, it is currently supported only by chrome and opera (http://caniuse.com/webp)

Resources