convert pdf to transparent png - php

I'm using ImageMagick to convert a pdf file to a png (thumbnail) image, and it works well. I'm wondering whether it's possible to convert the pdf, which has a white background, to a png file with a transparent background (i.e. set all the white pixels to transparent).
This is the PHP code i'm currently using (which results in a png file with a white background):
/* Open first page of PDF file */
$im = new imagick($pdf_filepath . '[0]');
/* Scale */
$im->thumbnailImage($width, $height);
/* Convert to png */
$im->setImageFormat('png');
/* Save file */
$result = $im->writeImage($thumbnail_filepath);

Check out: http://imagemagick.org/Usage/channels/#mask_creation
I think you will have to create it to GIF first and then back to png if you wish.

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?

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.

Convert PDF (with transparency *and* CMYK) to jpg

I need to generate jpg images from PDF files (first page only). The PDF files are user generated, so they can contain anything. I'm currently using the following code:
// Load PDF.
$i = new Imagick;
// Create thumbnail of first page of PDF.
$i->setResolution(150, 150);
$i->loadImage("test.pdf[0]");
$i->thumbnailImage(640, 480, true);
// Remove transparency, fill transparent areas with white rather than black.
$i->setImageBackgroundColor("white");
$i->setImageAlphaChannel(11); // Imagick::ALPHACHANNEL_REMOVE
$i->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
// Output.
$i->writeImage("test.jpg");
This works as expected in that transparency becomes white instead of black. However, I've run into problems with some generated jpg images, so I ran jpeginfo on them:
$ jpeginfo -c test.jpg
test.jpg 960 x 480 32bit JFIF N 9481 Unsupported color conversion request [ERROR]
It turns out that some source PDFs actually use CMYK, and apparently are not converted to RGB when saved as jpg. So I changed my code to the following (addition of a single line) to explicitly convert to RGB:
// Load PDF.
$i = new Imagick;
// Create thumbnail of first page of PDF.
$i->setResolution(150, 150);
$i->loadImage("test.pdf[0]");
$i->thumbnailImage(640, 480, true);
// Remove transparency, fill transparent areas with white rather than black.
$i->setImageBackgroundColor("white");
$i->setImageAlphaChannel(11); // Imagick::ALPHACHANNEL_REMOVE
$i->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
// Convert to RGB to prevent creating a jpg with CMYK colors.
$i->setImageColorspace(Imagick::COLORSPACE_RGB);
// Output.
$i->writeImage("test.jpg");
This creates a jpeg with an RGB color profile, all right. However, for some obscure reason it results in an image with a black background again. In other words: the transparency problem is back. Why does Imagick do this, and more importantly, what's the solution to both the transparency problem and the CMYK problem?
The correct function to use is transformImageColorspace not setImageColorspace. transformImageColorspace is used for existing images, setImageColorspace is for new images e.g. svg drawing..
I've added it to the manual and it should show up soon.

Black background from transparent image

I created an image from other image with the following code,
imagepng(imagecreatefromstring(file_get_contents(destination path)),source_path);
The image was created,But when i try to create a png image from a transparent image,The background become black color.
Why this happens?How to solve this?
If you look at the GD Documentation you'll find the alpha based functions. Taken directly from imagesavealpha() is the following code that opens a png and outputs it with transparency maintained;
// Load a png image with alpha channels
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
// (So any resizing/rotating/cropping etc)
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);

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