Question

I am trying to convert a vector image formats .emf,.wmf to a high resolution sharp and crisp raster image .gif,jpg. (Usually this could be easily done in Illustrator). But i am unable to do this in PHP. I am trying the following code but the results are either blurry or distorted or even totally black.

<?php

$image = new Imagick("1.emf"); 

$image->resizeImage(1500,0,Imagick::FILTER_LANCZOS,1);

$image->setImageFormat('gif');

$image->setresolution(900, 900);

$image->writeImage("2.gif");

?>
Was it helpful?

Solution

We just needed to set the resolution before loading the image.

$image = new Imagick();

$image->setresolution(300, 300);

$image->readimage($filename);

$image->resizeImage(1500,0,Imagick::FILTER_LANCZOS,1);

$image->setImageFormat('jpg');

$image->writeImage("1.jpg");

This code will convert a vector to a sharp and crisp raster image. It works for all vector formats (svg, ai, emf, wmf, etc). If the jpg result is unexpectedly a black image, you need to change the image transparency to white (check this link). Another way to get around with transparency problem is by getting your PHP updated to 5.5 and by installing Imagick for that version. By doing so, it will not cause any problems with transparent images and the above code will just work fine.

For testing purposes you could change jpg to png because it supports transparency.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top