Imagick PHP EPS to PNG conversion no clean results on the edges - php

When I am converting an EPS to PNG with imagick for generating a thumbnail preview. The result is very bad around the edges of a EPS without a background.
The Colorspace is CMYK but the result is the same using RGB. As I read, PNG does not support CMYK so I have to convert it first to SRGB. But It does not change anything. Normally I would set the resolution to 72dpi, but I also have tested it with 300dpi. Is there maybe a special order for the method calls to consider?
My Code:
$im = new \Imagick();
$im->setResolution(72, 72);
$im->setColorspace(imagick::COLORSPACE_SRGB );
$im->readImage($target_file);
$im->setImageAlphaChannel(imagick::ALPHACHANNEL_DEACTIVATE);
$im->setImageDepth(8);
$im->adaptiveResizeImage(800, 0);
$im->setImageFormat("png");
$im->writeImage($thumb_dir . basename($zoom_file_ne . '.png'));
$im->destroy();

Related

Imagick: EPS to JPG

I have an eps. I want to make an jpg out of it. I tried everything, but the quality is too bad.
I think imagick takes the original size of the file, makes an jpg, and then makes it bigger without any compression. The problem is, the generated image of the eps is in bad quality, and the bigger image is totally bad.
Here are some configurations I tried:
$imagick = new \Imagick();
$imagick->readImage($imagePath);
$imagick->flattenImages();
$imagick->resizeImage(1024, 0, \Imagick::FILTER_LANCZOS, 1);
$imagick->setImageResolution(300, 300);
$imagick->setImageCompressionQuality(100);
$imagick->setImageCompression(\Imagick::COMPRESSION_JPEG);
$imagick->setCompressionQuality(100);
$imagick->setImageFormat('jpeg');
$imagick->writeImage($new_imagePath);
Can anyone help me with this? I don't get it.
You'll need to set the image density/resolution before you read the Encapsulated PostScript file.
$imagick = new \Imagick();
$imagick->setResolution(100); // or 300
$imagick->readImage($imagePath);
Also note that, by nature, EPS is a vector format, and JPEG is a lossy raster format. Rendering at a low dpi, and resizing up to a given size will always result in poor quality. Either render at a high-dpi & downsample to a given size, or render at expected size & omit any resizing.

I create a cmyk image by php imagemagick, but cmyk color is different on photoshop?

NEED HELP!
I create a cmyk image by php imagemagick, but cmyk color is different on photoshop! e.g.: set ImagickPixel color cmyk(0,0,0,100)(black), but found cmyk(61,61,61,0) on photoshop.
why? and how to set the correct cmyk color?
You need to set the colorspace to CMYK, other wise your pixels will be converted to RGB.
$img->setImageColorspace(Imagick::COLORSPACE_CMYK);
http://php.net/manual/en/imagick.setimagecolorspace.php
Also all make sure that you are using file type that supports CMYK. ( eg. .jpg, .tif )
Edit
It seems Imagick has a bug.
Until it is fixed you can try and use this work around using transformImageColorspace.
$draw = new \ImagickDraw();
$fillColor = new \ImagickPixel();
$fillColor->setColor('cmyk(0%,0%,0%,100%');
$draw->setFillColor($fillColor);
$draw->rectangle(100, 100, 400, 400);
$img = new \Imagick();
$img->newImage(500, 500, 'white');
$img->drawImage($draw);
$img->transformImageColorspace(Imagick::COLORSPACE_CMYK);
$img->setImageFormat("jpg");
header('Content-Type: image/'.$img->getImageFormat());
echo $img;

Convert vector to raster in php

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

Converting an SVG to PNG and keep transparency does not work

I am trying to convert an SVG to PNG and keep the transparency, but that does not work. Instead, I get a black background.
This is my code:
$png = new Imagick();
$png->setBackgroundColor(new ImagickPixel("transparent"));
$png->readImageBlob($svg);
$png->setImageFormat("png32");
Thanks in advance!

Low resolution test when convert PDF to JPEG with PHP and ImageMagick

I have use this code to convert PDF file to JPEG images
$im = new Imagick();
$im->setResolution(90,90);
$im->readImage($pdf_file);
$im->setImageFormat('jpeg');
$im->writeImages($save_to,false);
$im->clear();
$im->destroy();
and it work but I have a problem that when there is a text with white background it will not be clear but I don't have this problem when ever I have colored BG.
this image will make every thing clear
JPEG compression generates such artefacts on edges where there is big color differences (such as between your black text and your white background). Try to to push up the compression quality
or use another image format for images containing text (such as png)
Add this functions:
$im = new Imagick();
$im->setResolution(90,90);
if ($width < 300) $im->sharpenImage(4, 1);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100); // or some alse
$im->readImage($pdf_file);
$im->setImageFormat('jpeg');
$im->writeImages($save_to,false);
$im->clear();
$im->destroy();

Categories