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);
Related
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.
I am converting PDF files to PNG files using imagick in PHP.
For this example I have a PDF file with only 5 lines of text on the first (and only) page. This PDF is generated from a Word file (it's not scanned in).
My code to convert the file looks like this:
$im = new Imagick();
$im->setResolution(200,200);
$im->readImage($_SERVER['DOCUMENT_ROOT'] . '/codeigniter/vrm/documents/uploaded_documents/2020/04/buddy_voor_lennert-01042020130414.pdf[0]');
$im->scaleImage(500,500);
$im->setImageFormat('png');
$im->writeImage($_SERVER['DOCUMENT_ROOT'] . '/codeigniter/vrm/documents/images/thumbnails/lennert.png');
$im->clear();
$im->destroy();
After this, the png is created, but the quality of the text is horrible, it becomes unreadable, see the screenshot i attached below.
How do I retain the same quality? I want to generate a thumbnail and a normal size image. So the thumbnail can be of lower quality, but the normal size image should be at least of a readable quality.
I've tried putting the resolution down to 50x50 or tried using the following methods, to no avail:
$im->setImageCompressionQuality(0); (also to 100)
$im->resizeImage(500, 500, Imagick::FILTER_LANCZOS, 0, true);
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?
When I use Imagick in PHP to resize PNG images, the output images are black. This is not the case when I resize JPGs; the PHP procedure (provided below) works perfectly for JPGs, so I suspect there's a missing step when processing PNG files.
I've first tested the conversion using ImageMagick on my local computer using this command:
convert x.png -resize 528 -filter Lanczos x-resized.png
...and it works; the resulting PNG file displays and is resized appropriately.
However, when I attempt to do the same on my server (both localhost and live), the result is a black image.
The PHP code I'm using is:
$source = "x.png" // I provide the complete path in my routine, etc.
$destination = "x-resized.png" // likewise.
$im = new Imagick();
$im->readImage($source);
$im->setImageFormat("png");
$im->resizeImage(528, null, Imagick::FILTER_LANCZOS, 1);
$im->writeImage($destination);
$im->clear();
What am I doing incorrectly?
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.