1

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

?>
Syed Waqas Bukhary
  • 5,130
  • 5
  • 47
  • 59
  • Consider PNG. JPEG is for photos, GIF is limited to an 8-bit palette and only supports 1-bit alpha. – Brad May 13 '14 at 16:12
  • 1
    Yes, i have tried both of these. It is giving the same results. gif is much better the the other two. Looking for something sharp, like we get in Illustrator export to jpg feature. – Syed Waqas Bukhary May 13 '14 at 16:16
  • 3
    I can assure you that PNG will give you the best quality. If it isn't, you're doing something wrong elsewhere. PNG is lossless. – Brad May 13 '14 at 16:21
  • 1
    Formats EMF and WMF are sometimes not reliably converted to bitmaps - like many vector formats, it depends on the conversion software. You may need to upload some **small** illustrations of the problem you are encountering. Also, do some research as to whether there are any implementation issues with ImageMagick in relation to the object types in the image you are converting. – halfer May 13 '14 at 16:31
  • Can you post an example file please? – Danack May 13 '14 at 18:53

1 Answers1

3

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.

Community
  • 1
  • 1
Syed Waqas Bukhary
  • 5,130
  • 5
  • 47
  • 59