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!
Related
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();
I have image that i want to convert from rgb to cmyk using color profile.
Here is the image that i am using
And the peace of code that i am using to convert into cmyk.
$IMagick = new IMagick();
$IMagick->clear();
$IMagick->readImage($image);
$IMagick->transformImageColorspace(12);
$icc_cmyk = file_get_contents("USWebUncoated.icc");
$IMagick->profileImage('icc', $icc_cmyk);
$IMagick->writeImage ($image2);
And here is the output image that i got.
I then checked color space and yes it is cmyk, but you can mark that green color is change though. Am i doing anything wrong here?
It is known that converting RGB to CMYK can change colors. Also browsers hae challenges rendering CMYK images properly. There is nothing you are doing wrong.
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;
I'am trying to convert an SVG Image which is created by the SVGGraph library ( http://www.goat1000.com/svggraph.php).
The SVG is colored (red, green, yellow, gray, ...) in the browser and everything is fine. But when I convert it, its just black and white.
With this code I convert it:
//new instance of imagick
$im = new Imagick();
//read the svg file/data (its not saved on the filesystem)
$im->readImageBlob($svgFile);
$im->setImageFormat("png24");
$im->writeImage('cylinder.png');
$im->clear();
$im->destroy();
I've tried it with jpeg and png as output format but the result is the same, all colors will be replaced with black
does anyone have an idea how to fix that ?
Try this way:
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImageBlob($svgFile);
$im->setImageFormat("png24");
$im->writeImage('cylinder.png');
$im->clear();
$im->destroy()
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();