ImageMagick - Convert PDF to JPG and Set Width - php

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?

Related

Imagick - set paper size of output pdf

I have a php helper function that takes base64 png and outputs a pdf file:
public static function base64PngToPdf($b64, $filePath) {
$imagick = new Imagick();
$imagick->readImageBlob(base64_decode($b64));
// Create PDF File
$pdfFile = new Imagick();
$pdfFile->setFormat('pdf');
// Add image to pdf
$pdfFile->addImage($imagick->getImage());
file_put_contents($filePath, $pdfFile->getImagesBlob());
return $filePath;
}
The problem in my case is, the png I have is 800w x 1200h px. The code above is outputting a label that's 211 x 317mm. I want to get a label that's scaled down to either 101mm wide or 152mm tall (preferrably without scaling the pixels themselves, just changing the physical size).
I did some googling and found the imagick has a density parameter, but I'm not 100% sure if that's what I want in this case, and my library doesn't have a ->setDensity function.
I found a sort-of related solution. I can use $pdfFile->setResolution($resolution[0], $resolution[1]);, and in setResolution I can set for example a ppi of 72.

Imagick php class not working when I try to set image resolution and color space for a base64 image then saving it in jpg format

I'm trying to generate a high resolution jpg image from base24 image string using imagick php class. the code runs with no errors.
However, the image saved does not show any resolution or color space in photo viewers or browsers. I don't know whether my code is working or not?!
Any help on this is highly appreciated.
$image_data = base64_decode($data);
$imagick = new Imagick();
$imagick->readImageBlob($image_data);
$jpegimagequality = 95;
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality($jpegimagequality);
$imagick->setImageResolution(300,300);
$imagick->setImageColorspace(Imagick::COLORSPACE_SRGB);
$imageResampled = $imagick->resampleImage(300, 300, Imagick::FILTER_UNDEFINED, 1);
$imagick->setImageFormat("jpg");
$devimageSaved = $imagick->writeImage('jpg:'.$hiResPosterImagePathDev);

Imagick - When resizing, image gets blurry

I am trying to resize a pdf (which i converted from image), and I am trying to resize (increase) the image in its ratio.
$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->resizeImage(595,842,\Imagick::FILTER_CUBIC, 1, true);
// and this:
// $imagick->adaptiveResizeImage(595,842, true);
$imagick->setImageFormat('pdf');
$imagick->writeImage($endpath);
But the image is getting blurry. However, it's not a bad quality image and the image can go that size without getting disturbed. (For example, if I let Twilio to do it (via fax api), the same image can get to that scale).
I have also tried with putting blur parameter of resizeImage between '1' and '0.1'
Original pdf (you can also see it here, if you want to try):
My resized pdf (with blur 1):
My "adaptive resized" pdf:
You are starting with a very small pdf if rasterized and enlarging. So that would cause blurring. But if you increase the input density, it works fine for me in ImageMagick as
convert -density 600 input.pdf -resize 595x842 result.png
I do not know Imagick well, but try the following. Reduce the blur value in resizeImage as desired to make it sharper.
$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->Imagick::setImageResolution( 600, 600 );
$imagick->resizeImage(595,842,\Imagick::FILTER_CATROM, 1, true);
$imagick->setImageFormat('pdf');
$imagick->writeImage($endpath);

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.

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