I am trying to convert svg to png image using imagick.This is the code i am using.
<?php
$usmap = 'http://yatnam.com/demo/vh/card2_1.svg';
$svg = file_get_contents($usmap);
$im = new Imagick();
//$im->setBackgroundColor(new ImagickPixel('transparent'));
$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();
?>
<img src="<?php echo 'data:image/jpg;base64,' . $base64;?>" />
My SVG images contain many other base64 encoded images..These images will not convert properly if it is not in png format.
To understand the issue exactly, please browse svg url directly.It is a image with Pink background.Now run my code. See same image in white background ..
Please help me to fix this issue..Thanks a lot in advance.
Do you have the access to the SVG file? Or are you able to download/save it locally, then change
xlink:href="data:image/jpeg;base64
in the 1st <image>
xlink:href="data:image/png;base64
and reference your local downloaded and changed copy?
The other way to show what you need to change is here:
I'm getting then the following:
UPDATE: I'd like to say once more that the SVG file you provided as example has the pink background as <image> with wrong MIME type, as I said in the comments. The problem you describe occurs because of that, no matter how reliable the image source is. You can check it by copying the base64 value of the first <image> in the SVG, decode it and save and then open it with any editor, you will see this:
which is a PNG, not JPEG signature. However, the first <image> in the SVG has image/jpeg - check it.
Now back to your claim that you can't change all the files. What I can propose is parsing SVG's XML in your script and replacing all MIME types with the correct ones. Be aware that this will require quite a lot memory as SVG's may be large. Note the new code between MODIFY THE MALFORMED SVG comments
$usmap = 'http://yatnam.com/demo/vh/card2_1.svg';
$svg = file_get_contents($usmap);
/////////////////// MODIFY THE MALFORMED SVG ///////////////////////
$dom = new DomDocument();
$dom->loadXML($svg);
foreach($dom->getElementsByTagName('image') as $image) {
$encoded = $image->attributes->getNamedItem('href')->value;
if(!empty($encoded)) {
$binary = base64_decode(substr($encoded,strpos($encoded,'base64,') + 7));
$info = getimagesizefromstring ($binary);
$image->setAttributeNS('http://www.w3.org/1999/xlink','xlink:href','data:'.$info['mime'].';base64,' . base64_encode($binary));
}
}
$svg = $dom->saveXML();
/////////////////// MODIFY THE MALFORMED SVG ///////////////////////
$im = new Imagick();
//$im->setBackgroundColor(new ImagickPixel('transparent'));
$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();
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 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);
Im using PHP Imagick to process images, I need to convert an EPS file into PNG file and next the remaining code can process the created PNG, Im able to convert EPS as PNG and can process to, but when i do this the PNG file is creating with white background, I want it to be transparent where EPS file is not having any background either.
My code is as follows
$img = new Imagick();
$img->setResolution(300,300);
$img->readImage('my_file.eps); //reading the file
$img->setBackgroundColor ('#623423'); //setting background color but not working
$img->setImageFormat("png"); //setting the format to save//converting
$img->writeImage('converted.png'); //saving the converted file
But the generated png is coming with white bg, can some one help me how to create it with out BG color(transparent)?
Thanks in advance!.
Have you looked at this example?
<?php
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImage('carte_Alain2.svg');
$im->setImageFormat("png32");
header('Content-type: image/png');
echo $im;
?>
I have a problem creating thumbnails with PHP and imagick. The code is working ok and the thumbnail is generated in the correct size etc but when I try to place a PDF logo on the thumbnail it turns half transparent. I guess it has something to do with the PDF file being generated in InDesign and it probably doesn't have any background defined. Has anyone come across this problem or has an idea what to do about it? I tried to put a white canvas in the background but that didn't help. I also specified a channel for the compositeImage function but that didn't help either.
This is the PDF file I'm having issues with: https://dl.dropbox.com/u/13712643/Case_Study.pdf
The generated Thumbnail looks like this:
https://dl.dropbox.com/u/13712643/Case_Study1.jpg
The code I have produced so far: http:// pastebin.com/74CYC972
Any ideas? Thank you for your help.
I had the same issue and I solved it by using Imagick::compositeImage that was found in here: php imagick convert PNG to jpg
The code goes something like this:
$im = new Imagick();
$im->readimage($pdfFile."[$currentPage]");
$res = $im->getimageresolution();
$bg = new Imagick();
$bg->setresolution($res["x"],$res["y"]); //setting the same image resolution
//create a white background image with the same width and height
$bg->newimage($im->getimagewidth(), $im->getimageheight(), 'white');
$bg->compositeimage($im, Imagick::COMPOSITE_OVER, 0, 0); //merging both images
$bg->setimageformat("png");
//then you can write to a file
$bg->writeImage('white-background-pdf-image.png');
//or output it
header('Content-type: image/png');
echo $bg;
May be this what you are looking for :
$im->setBackgroundColor(new ImagickPixel('transparent'));
http://www.php.net/manual/en/imagick.setbackgroundcolor.php
None of the existing answers worked for me. Flatten the image just after creating a new imagick() worked instead:
$im = $im->flattenImages();
Edit: The flattenImages method has been deprecated & removed. Use
$im = $im->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );