PHP - convert all images to jpg using Imagick - bad quality - php

I found more topics from this web site about quality with Imagick but nothing help me...
I have to save all images as JPG. I created this script:
$image_url = 'http://limuzynamercedes.pl/wp-content/uploads/2014/06/3.png';
$image_code = file_get_contents($image_url);
$img = new Imagick();
$img -> readImageBlob($image_code);
$img->setResolution(300, 300);
$d = $img->getImageGeometry();
$img->cropImage($d['width'],($d['height']-120), 0,0);
$img->setImageFormat('jpeg');
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setCompressionQuality(100);
$img->writeImage('read.jpg');
$img->clear();
echo '<img src="read.jpg?'.time().'">';exit;
Here is original image: http://limuzynamercedes.pl/wp-content/uploads/2014/06/3.png
and here is image which was converted by my script: http://s5.ifotos.pl/img/demo1jpg_saeaxqx.jpg
Where is a problem? Why this image is always convert in bad quality?
Thanks.

The image is not in "bad quality" (there is no blurry areas found), but the difference between 2 images is caused by transparent PNG to JPG conversion.
Before you crop the image, add these two lines:
// set background to white (Imagick doesn't know how to deal with transparent background if you don't instruct it)
$img->setImageBackgroundColor(new ImagickPixel('white'));
// flattens multiple layers
$img = $img->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

Related

Imagick php class not working when I try to set image resolution and color space for a base64 image then saving it in jpg format

I'm trying to generate a high resolution jpg image from base24 image string using imagick php class. the code runs with no errors.
However, the image saved does not show any resolution or color space in photo viewers or browsers. I don't know whether my code is working or not?!
Any help on this is highly appreciated.
$image_data = base64_decode($data);
$imagick = new Imagick();
$imagick->readImageBlob($image_data);
$jpegimagequality = 95;
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality($jpegimagequality);
$imagick->setImageResolution(300,300);
$imagick->setImageColorspace(Imagick::COLORSPACE_SRGB);
$imageResampled = $imagick->resampleImage(300, 300, Imagick::FILTER_UNDEFINED, 1);
$imagick->setImageFormat("jpg");
$devimageSaved = $imagick->writeImage('jpg:'.$hiResPosterImagePathDev);

PHP - convert EPS to PNG using Imagick

I am trying to convert eps to png image using imagick.This is the code i am using.
$path = getcwd().'/uploads/1488/791/586/imprint_option_1A.eps';
$save_path = getcwd().'/uploads/1488/791/586/imprint_option_2E_c.png';
$image = new Imagick();
$image->readimage($path);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->setResolution(300,300);
$image->scaleImage(600, 270);
$image->setImageFormat("png");
$image->writeImage($save_path);
but the transparency is not working i got image with white background ( Result image ). and when we scale image it loses clarity..
Any idea ?
Here is my eps file https://drive.google.com/open?id=0Bwq4DvGGbHVfT0FYTE94WW5GTnc
The function setResolution should be called before reading the image. Thus
$image = new Imagick();
$image->setResolution(1200, 1200);
$image->readImage($path);
should do it. As for the transparency, can you try to get the input as sRGB instead of CMYK? If I convert first the input file to pdf with epstopdf and then use this converted file in the PHP script, it produces a transparent PNG file.

Converting svg to png generates blank image

I have went through a lot of similar questions.i couldn't find an answer to my problem.
I have an svg image. I'm trying to convert it into a png image.
I have been using a 300dpi image as background of svg image.Now i have changed it into 600dpi. After that imagick returns an empty png image.
$svg=path to svg;
$im = new Imagick();
$im->readImageBlob($svg);
$im->setImageFormat("png32");
$im->setImageCompressionQuality(100);
$im->resizeImage(720, 445, imagick::FILTER_LANCZOS, 1);
$base64=base64_encode($im);
$im->clear();
$im->destroy();
return 'data:image/jpg;base64,' . $base64; //returns blank png
What am i missing here? Do i have to include any libraries??
PHP VERSION: 7
IMAGICK VERSION: ImageMagick 6.8.9-9
Looking at the documentation of Imagick::readImageBlob
Reads image from a binary string
this seems not the correct method for reading from a file path.
You should either use Imagick::readImage
Reads image from filename
$im = new Imagick();
$im->readImage($svg);
or the constructor Imagick::__construct
Imagick::__construct ( mixed $files )
Creates an Imagick instance for a specified image or set of images.
$im = new Imagick($svg);

Imagick JPEG to PNG conversion

I need to convert a JPEG Imagick image to PNG while maintaining the imageCompressionQuality of the JPEG image. I've tried using compositeimage as well as clone $image in order to achieve this, but both take the original quality (before the image compression of the JPEG file).
$image = new Imagick($image_name);
$image->resizeImage($imageWidth, $imageHeight, Imagick::FILTER_LANCZOS, 1);
$image->setImageFormat("jpeg");
$image->setImageCompression(imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(00);
$image->stripImage();
METHOD 1:
$finalImage = new Imagick();
$finalImage->newImage($imageWidth, $imageHeight, "none");
$finalImage->compositeimage($image, Imagick::COMPOSITE_OVER, 0, 0);
$finalImage->setImageFormat("png");
echo $finalImage;
METHOD 2:
$finalImage = clone $image;
etc.
Is there any way to do this?
"Quality" settings are not part of JPEG. It is merely a shorthand some encoders use for selecting quantization tables.
There is no equivalent to quantization table selection in PNG. PNG is a lossless compression. What goes in is what comes out for all settings.
Figured out a way to make this work - I used writeImage to write a temp file onto the server, then created a new Imagick object from the temp object. This preserved the compression of the jpg file.
$image->writeImage($image_name . "temp.jpg");
$finalImage = new Imagick($image_name . "temp.jpg");
$finalImage->setImageFormat("png");
echo $finalImage;
unlink($image_name . "temp.jpg");

transform a jpg into png and make a color transparent in it

In PHP I'm trying to process an image, that is, I'm trying to make the surrounding color transparent in a jpg file. I'm using the GD library by the way.
I can directly output the image by converting it into a png using imagecreatefromjpeg and imagepng functions. But I can't find a way to make the specified color transparent. Also, some images have lighter gray artifacts around black graphics, created during saving. Is there any way I can include those as well?
I'm kind of lost. I found some answers to make a color transparent on an image but I don't know how to first convert the image without saving it into the server and then process it.
Any ideas?
EDIT: Here's my code so far. I managed to make a specified color transparent but I can't make it detect the surrounding one yet.
Most of the time images will be closed because they'l be logos or texts, saved in the allowed image formats. So I don't think I will have a major issue with gradients but it would be great if I could manage to manipulate transparency in the surrounding gradients, if any, such as drop shadows.
Is there also any way to detect if the png/gif image is already transparent? My code paints the transparent parts into black for those files now.
$file = 'images/18.jpg';
$specs = getimagesize($file);
if($specs[2] == 1) $img = imagecreatefromgif($file); //gif
elseif($specs[2] == 2) $img = imagecreatefromjpeg($file); //jpg
elseif($specs[2] == 3) $img = imagecreatefrompng($file); //png
else exit('unsupported file type!');
$newimg = imagecreatetruecolor(imagesx($img), imagesy($img));
// create a new image with the size of the old one
imagecopy($newimg,$img,0,0,0,0,imagesx($img),imagesy($img));
// copy the old one
imagedestroy($img);
// free the memory
$white = imagecolorallocate($newimg,255,255,255);
imagecolortransparent($newimg,$white);
// make white pixels transparent
header('Content-Type: image/png');
imagepng($newimg);
imagedestroy($newimg);
// and finally output the new image
You can set the transparent color with the imagecolortransparent function.

Categories