imagemagick pdf to jpg - font quality bad - php

I do upload PDFs to PHP and extract the pages as JPG in different resolutions in a kind of batch through JS + AJAX-Calls to work arround PHP timeout.
But the font is rendering not so pretty... what can I do?
$pdf = new Imagick();
$pdf->setresolution(225, 225);
$pdf->readimage('mypdf[0-5]');
$written = $pdf->writeimages('previewfolder/pages/hq-0.jpg', FALSE);
$pdf->clear();
$pdf->destroy();
I tryed upsetting the values of setresolution to 500and 500, then the font is a Little bit better but the Image is also much bigger in Resolution. Here an screenshot: http://imgur.com/5U88bx5
My Target: small Image (1000px*1000px) but with as max font-quality as possible.
Hopefully someone has an Idea.
Regards, lippoliv

as often: mistake 40 (the mistakes sits 40cm in front of his Monitor)...
$pdf = new Imagick();
$pdf->setresolution(350, 350);
$pdf->readimage('mypdf[0-5]');
// Because we have multiple pages, we have to process each page.
foreach ($pdf as $page) {
$page->resizeimage(1500, 1500, \Imagick::FILTER_UNDEFINED, 1.1, TRUE);
}
$written = $pdf->writeimages('previewfolder/pages/hq-0.jpg', FALSE);
$pdf->clear();
$pdf->destroy();
Thanks to Mark Setchell who brought in that Idea and made me thinking about why the resize doesn't work. And than ofter another hour of Google I found an example about resizeing Images, pointing out that you have to resize each Frame.
So I thought may I have to resize each single page of my PDF (in that example there are 6 pages) and now it works: http://imgur.com/UoP3kMK
Now I can up- / downscale the Image size as I like to and get nice Fonts :) Even as a JPG.
Thank you all.

Related

How can I enlarge the "canvas" using provided png data

We are processing nearly 20M existing images (not files), all of which have been converted to the raw PNG data. Each image is always 640x480, and we want them all to be saved as square 640x640 files, with the original image on the top of the larger canvas, leaving the additional lower 160px for descriptive text to be added.
Our PNG data being retrieved is like this:
data:image/png;base64,iVBORw0 ... kJggg==
I've seen lots of posts and examples showing work with resizing, transparencies, colors, etc., but cannot determine where to start creating a new empty 640x640 "canvas" with existing PNG data.
If this is a duplicate question, I'd be happy to delete it if I can get some direction on how to begin. Without knowing the basics to get started, googling things like "image create png canvas" hasn't been helpful. Clearly, image processing is new to me, so apologize if I'm not being clear enough. Thanks in advance.
This should work for you:
$imgData = 'data:image/png;base64,iVBORw0 ... kJggg==';
$img = imagecreatefromstring(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $imgData)));
$targetImage = imagecreatetruecolor(640, 640);
imagecopyresampled($targetImage, $img, 0, 0, 0, 0, 640, 480, 640, 480);
imagepng($targetImage, $yourPath);
Of course you might want to add some checks, background color etc. but I think you will be ready to start with this.

Imagick PHP Issue - Massive Quality loss of .png or .jpeg after "mergeImageLayers" operation

I'm currently facing an issue regarding the image quality after a "mergeImageLayers" operation with ImageMagick. I merge three seperate .png pictures each one of them has initially arround 22KB and looks fine. After the "merging-prozess" I get an 5KB picture in bad quality either as .png or .jpeg.
How can I built in a compression method to ensure higher quality? At the moment the images are sadly barely useable. Here is the function I built (it also does color-replacement):
function mergeImages($BASE,$MASK,$TEXT,$TARGET,$FILL,$FUZZ){
$layerMethodType = \Imagick::LAYERMETHOD_COMPARECLEAR;
$img1 = new Imagick(realpath($BASE));
$img1->floodFillPaintImage($FILL, $FUZZ* \Imagick::getQuantum(),$TARGET,0,0, true);
$fuzz = $FUZZ* $img1->getQuantumRange()['quantumRangeLong'];
$img1->opaquePaintImage($TARGET, $FILL, $fuzz, false, Imagick::CHANNEL_DEFAULT);
$img2 = new Imagick(realpath($MASK));
$img1->addImage($img2);
$img1->setImageFormat('png');
$img3 = new Imagick(realpath($TEXT));
$img3->floodFillPaintImage($FILL, $FUZZ* \Imagick::getQuantum(),$TARGET,0,0, true);
$fuzz = $FUZZ* $img1->getQuantumRange()['quantumRangeLong'];
$img3->opaquePaintImage($TARGET, $FILL, $fuzz, false, Imagick::CHANNEL_DEFAULT);
$img1->addImage($img3);
$img1->setImageFormat('jpeg');
$result = $img1->mergeImageLayers($layerMethodType);
header("Content-Type: image/jpeg");
$result->getImageBlob();
return $result;
}
Hopefully one can help me out with this. I allready tried the following:
$img1->setImageFormat('jpeg');
$img1->setImageCompression(\Imagick::COMPRESSION_NO );
and
$img->setCompression(Imagick::COMPRESSION_JPEG);
$img->setCompressionQuality(90);
as .jpeg or .png in all cases the new images has 5KB
Thx in advanced for your help!
Update
I have tried different images and the KB went up imideately. Simple pictures like Icons a playicon for instatance simple look not good. Fotos on the other side are quite well compressed and have a good quality. But the "compression-problem" still remains -> the above mentioned solutions simple don't work. Now I have to do the "compress_image()" workarround.

Taking care of transparency using IMagick via PHP makes some images darker

I have a script that handles/scales uploaded images. I noticed that some of the images come out significantly darkened, and through a process of elimination tracked the darkening back to this section of code:
$scaled = new IMagick();
$scaled->newPseudoImage($original->getImageWidth(), $original->getImageHeight(), 'xc:white');
$scaled->compositeImage($original, Imagick::COMPOSITE_DEFAULT, 0, 0);
$scaled->flattenImages();
What I'm doing here is trying to eliminate issues with transparent backgrounds in some images coming through as black when I convert to jpg.
Does anyone have any idea which part of this code is darkening the image and what a good way to fix it might be?
Edit: Still haven't figured out the heart of this issue, but I did find that I can avoid doing this to images that don't need it by wrapping it in:
if($original->getImageAlphaChannel()){

Overlay PNG on JPG using Imagick

I have the last few hours tried to get a PNG logo with a transparent background on top of a JPG background. I have tried several ways and with several globals as well, but I do not seem to be able to get the result I want.
"First Attempt":
$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background
$overlay->destroy();
$image->destroy();
As you can see, the Jaguar logo is all black.
"Second Attempt"
$overlay = new Imagick('overlay.png');
$image = new Imagick('background.jpg');
$image->setImageColorspace($overlay->getImageColorspace() );
$image->compositeImage($overlay, Imagick::COMPOSITE_DEFAULT, 10, 10);
$image->writeImage('background.jpg'); //replace original background
$overlay->destroy();
$image->destroy();
This one the Jaguar logo looks like it should, but the background is all messed up now.
I have tried with Imagick::setImageMatte and tried to add the overlay to a white background (thought I does need to have a transparent background) and still it won't display the image properly. I have tried many other variations of the 2 above snippets but they all seem to make the PNG completely or partial black.
What am I missing or doing wrong? Can anyone give me a nudge in the right direction?
Please note this needs to be done in PHP.
Thank you very much!
I am a huge idiot! Turns out I forgot to convert the images from CMYK to RGB.
For anyone who might encounter this in the future, learn from my incompetence!
I was trying to overlay a png with transparency over the top of another png. I used this line from the PHP docs.
$src1->compositeImage($src2, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
but I was getting the same problem. The overlay came through as black only. Changing it to this seemed to fix the colours.
$src1->compositeImage($src2, Imagick::COMPOSITE_DEFAULT, 0, 0);

php image manipulation - fade to transparency

does anyone know how to apply fade effect to an image using PHP ? what I am looking for is a way to apply gradient transparency ( i mean : at the top , the image is opaque , which gradually gets more and more transparent , and at the bottom it is completely transparent).
i have been reading up on http://php.net/manual/en/function.imagecolortransparent.php , but did not see anything about applying a gradient effect to an image.
i also read : PHP - Generate transparency (or opacity) gradient using image , but it kinda trailed off without any solution!
I am also open to any other suggestion / libraries that can do this from command line.
Obviously you'll need to work with a png for this effect, but you can convert any png into a jpg using php. The following question I believe covers what you are asking about. Part of the code will have to be removed to clear the image reflection effect.
Can You Get a Transparent Gradient using PHP ImageMagick?
The piece of code which seems to do what you are trying to accomplish is:
$im = new Imagick('image.jpg'); //Reference image location
if (!$im->getImageAlphaChannel()) {
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET);
}
$refl = $im->clone();
$refl->flipImage();
$gradient = new Imagick();
$gradient->newPseudoImage($refl->getImageWidth() + 10, $refl->getImageHeight() + 10, "gradient:transparent-black");

Categories