When saving a RGB image to a PNG with Imagick. The image gets saved automatically in grayscale when it contains only grey pixels. I understand this a default stetting, that can be overridden. I would like to save my image always in RGB as a default. How do I turn Imagick's auto-grayscale off with PHP/Imagick? I wrote this code.
$im = new Imagick();
$im->setResolution(288,288);
$im->setColorspace(Imagick::COLORSPACE_SRGB);
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImage($orgDataPath);
$im->setoption("colorspace:auto-grayscale", "off");
$im->setImageType(Imagick::IMGTYPE_TRUECOLOR);
$im->trimImage(0);
$im->setImageFormat("png");
$im->writeImage($pngPrint.".png");
// cleanup
$im->clear();
$im->destroy();
Related
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.
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);
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");
I want to change opacity of multiple images when i used setImageOpacity it work fine for all images but not with png images and when i used evaluateImage it is work fine for transparent images but not for other images. how can i used same method for all types of images if image is transparent or not this is the code
<?php
// Open the original image
$image = new Imagick();
$image->readImage(3.jpg);
// Open the watermark
$watermark = new Imagick();
$watermark->readImage(2.png);
$watermark->setImageOpacity(0.444);
//$watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, 0.0, Imagick::CHANNEL_ALPHA);
$watermark->rotateImage(new ImagickPixel('transparent'), 90);
// Overlay the watermark on the original image
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 20, 20);
// send the result to the browser
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
use if condition using getImageAlphaChannel() function
to detect if the image have any transparent
note :
This method is available if Imagick has been compiled against
ImageMagick version 6.4.0 or newer.
When compressing a image with imagemagick on PHP the result turns pink.
$im = new Imagick();
$imgsrc = file_get_contents( $imgFilepathIn );
$im->readImageBlob($imgsrc);
$im->setImageColorspace(255);
$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(90);
$im->setImageFormat('jpeg');
$im->writeImage( $imgFilepathOut );
The problem is the default transparency color for the jpg (which is pink).
We can overcome this by explicitly setting a new transparency/background color.
$im->setBackgroundColor(new ImagickPixel('transparent'));
and it also seems like
$im->setImageColorspace(255);
could sometimes create problems