SVG to PNG image conversion in php - php

I want to convert SVG images to PNG files with transparent background. I am using below code to convert it using imagick in php,but it gives image with black background.
$image = new imagick();
//set transparent background
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->setFormat('svg');
$image->readImageBlob(file_get_contents("image.svg"));
$image->setImageFormat("png32");
$image->resizeImage(265,195, Imagick::FILTER_LANCZOS, 1);
$image->writeImage('result.png');
Can anyone please help me how to convert above svg image to png image using PHP.
Any help would be appreciated.

Related

ImageMagick - Convert PDF to JPG and Set Width

I am currently converting a PDF to a set of images using ImageMagick like this..
// create Imagick object
$imagick = new Imagick();
// Reads image from PDF
$imagick->readImage('book.pdf');
// Writes an image
$imagick->writeImages('converted.jpg', false);
The resulting images are quite small, is there a way to set a defined width and then let the height work itself out?

Using Imagick to convert PDF in landscape format to image

I'm using Imagick to convert PDFs to images. It works great when I convert PDFs oriented in portrait mode. When I try with PDFs in landscape mode, the result is a cropped image. Even if I try rotateImage("#fff",90), the result is also cropped.
Here's the important part of my script:
$imagick = new \Imagick();
$imagick->setResolution(144, 144);
$imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1));
$imagick->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
$imagick->setFormat($this->determineOutputFormat($pathToImage));
return $imagick;
Could anyone give me an idea?

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);

Setting background color as transparent while creating PNG image from EPS file

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;
?>

Categories