I have a PNG file with quite decent quality and not at all small size. I convert it to PDF like this(in php):
$imagick = new Imagick();
$imagick->readImageBlob($image);
$imagick->setImageFormat('pdf');
echo $imagick->getImageBlob();
And I get a PDF file with my image in it. But the quality gets very poor. I thought I might fix with trying different density setting it with setResolution method. But this lead to either the image becoming very small or getting even worse quality. What am I doing wrong?
Related
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'm trying to convert a pdf to an image file (png, jpg, gif does not matter).
But using the following code:
<?php
$im = new imagick('helloworld.pdf[0]');
$im->setImageFormat('png');
header('Content-Type: image/png');
echo $im;
?>
Picture quality is degraded significantly.
Is there any way to convert it with no or very little quality loss ?
you have to play with Imagick::setCompression, Imagick::setCompressionQuality and Imagick::setImageCompression
for this guy helped
$im->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$im->setImageCompressionQuality(0);
anyway, you have to put some of this strings before you format the image to png
I am using Imagick to product images from massive pdf files. I also want those images with RGB or sRGB color mode so Internet Explorer can display the images correctly.
I have tried
$im = new imagick($fileName.'[0]');
//$im->setImageColorspace(Imagick::COLORSPACE_SRGB); //try this already
// $im->setImageColorSpace(1); //try this already
$im->setResolution(300,300);
$im->setImageFormat('jpeg');
$im->writeImage($imageFile);
$im->clear();
$im->destroy();
I did get images but the color is way off with setImageColorspace and setImageColorSpace methods. (ex: color is inverted.)
If I comment out those methods, the images look right but some of them are not RGB mode and create problems in Internet Explorer.
I really need the RGB color mode on the images. Are there anyways to do it? Thanks so much!
You seem to encounter a problem with CMYK pdfs. Have you tried converting them to PNG? PNG -contrary to jpeg - only encodes RGB so the images will in any case be in the correct colorspace.
You might also want to have a look at ghostscript (the engine behind imagemagicks PDF conversion) and it's --UseCIE switch.
I wrote a php-wrapper to ghostscript which you can find at github that you might find usefull when you want to use ghostscript.
I am making an avatar script from scratch and am having some problems. I got transparency working, and multi-image support for heads, bodies, shirts, etc.
Anyhow, I want to be able to generate specific sizes of the avatar within the PHP script. At this time, I have the variable $baseImage, which is an image generated using the GD script below:
$baseImage = imagecreatefrompng($startAsset);
imagealphablending($baseImage, true);
imagesavealpha($baseImage, true);
... combine all images into $base here
header("Content-type: image/png");
imagepng($baseImage);
The size of the image this generates is 350x550 (pixels) and I want to be able to get a smaller size.
I've done research but cannot find a working solution. What built-in PHP GD functions can resize this, retain transparency, and keep the great quality/colors?
There is no way to change the size of an image resource directly. Instead, you need to create a new image of the desired size and use imagecopyresampled to copy from the fullsize image to the resized one.
I was doing some image editing with PHP, since GD provides less functionalities, I switched to Imagick.
One of the processes is to greyscale images. Everything went fine (locally on Windows 7, Imagick 2.2.1-dev 6.5.8-7 Q16) till I uploaded the script to my web hosting server (Linux, Imagick 3.0.1, 6.2.8, 2010-10-20, Q16).
I'v tried to change the quality, but it didn't improve anything.
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(100);
Here is the results from GD, Imagick and Photoshop
I believe something's wrong with version 3.0.1. Can someone please confirm that?
Q1: Is there an alternative way to convert an image to greyscale with Imagick?
Q2: Is it possible to convert a GD resource to Imagick? So I can use imagefilter($img, IMG_FILTER_GRAYSCALE); to get the correct result and then output with Imagick.
ps: For Q2, you might suggest me to just use GD to process the image. But the problem is that imagejpeg() cannot save images with resolution preserved. and that is actually the reason I switched to Imagick.
This is my preferred way to make a B&W photo in php/imagick: $im = $im->fxImage('intensity');
That applies a function to the image, where intensity is equal to 0.299*red+0.587*green+0.114*blue.
That formula is based on how our eyes are more sensitive to different colours, and as such the difference between that and a "flat" grayscale image really is night and day.
More details here:
http://php.net/manual/en/imagick.fximage.php
http://www.imagemagick.org/script/fx.php
function ImagickToGD($imagick){
$tmpfile = tmpfile();
$imagick->writeImage($tmpfile);
return imagecreatefromstring(file_get_contents($tmpfile));
}
Note that this function does not do any cleanup (except the temp file, which PHP cleans automatically).
So, for example, your code should look like:
$img = new Imagick();
// ...
$gd = ImagickToGD($img);
unset($img); // destroy imagick
imagefilter($gd, IMG_FILTER_GRAYSCALE);
imagejpeg($gd, $target_name, 100);
imagedestroy($gd);
Also, I did not understand the part about "preserving resolution". There is nothing in these operations relating to resolution. My guess is you meant compression? If you want full quality (ie, no compression) simply use 100 as compression value (as I did).
This results in maintaining the existing quality, since opening an image of 70% quality and saving it back with 70% quality actually decreases the final quality by 49% (70% of 70%).
function GDToImagickTo($gd){
$tmpfile = tmpfile();
imagepng($tmpfile); // Png is our best image deal:
// lossless compression, transparency etc..
$imagick = new Imagick()
$imagick->readImage($tmpfile);
return $imagick;
}
Refer this website and check out the image Magick operators found here www.rubblewebs.co.uk/imagemagick/
Also go with www.fmwconcepts.com/imagemagick/ you will find some examples out here...
You can use the image class what you prefer and then use the method readImageBlob to send it to the imagick http://www.php.net/manual/en/imagick.readimageblob.php