How to generate images from PDF files in php? - php

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.

Related

imagecopyresampled results to black square image, but imagescale is fine

I have a straightforward PHP script (php version 7+) that I run to resize images to a smaller dimension around 1200px wide ...
I use imagecreatefromjpeg and imagecopyresampled method and the save new file with imagejpeg # 88% compression.
I and ran over 100 images through it and most images worked out great, except for a few stubborn images that results to BLACK square image.
I tried imagecopyresized and played with different new dimensions but it just didnt work. I even tried outputting the ob stream from these tests at the point of resizing instead of saving to file (no compressions applied) and i can confirm that the moment the image was resized, it is already black square.
On the images that produces black square the result is false.
I did verify mime type IMAGETYPE_JPEG of file, regardless of file extension, before running the code.
Now, I tried running the same files thru imagescale instead
$new_img = imagescale($img, $new_width, $new_height, IMG_SINC );
and that worked.
Now I prefer to use the previous method for better more crispier output so I've been trying to get it to work :(
Why is this happening? I searched stack for similar issues but they are describing a issues that completely didnt work on any images, not my case, where only a select few images had issues.
I am dealing with JPG only, but is it possible that the jpg file is just named with extension ".jpg" but actually a png, bmp, or something else? Will that cause issues?
I'm stuck, and i hope you guys can help.
Thank you
Most images resized nicely

Quality issue with converting a PNG to PDF with ImageMagick in PHP

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?

PHP imagick converting image from CMYK to RGB inverts image

I have an image being rejected by the eBay API because of this:
<ShortMessage>Source picture uses an unsupported colorspace.</ShortMessage>
<LongMessage>Pictures with a CMYK colorspace are not supported; source pictures must use a RGB colorspace compatible with all web browsers supported by eBay. Submit pictures created using a camera or scanner set to save images with RGB color.</LongMessage>
Well, I had no idea it was a CMYK nor am I even sure how to tell. I then used the following code to attempt a convert:
$image= "$img.jpg";
$i = new Imagick($image);
$i->setImageColorspace(Imagick::COLORSPACE_SRGB);
$i->writeImage($image);
$i->destroy();
It converts (and is now accepted by eBay), but it also inverts the colors of the picture. Why would it do this and is there another COLORSPACE I should be using?
Thanks.
The setImageColorSpace method is not meant to be used for existing images - it's only for new images (e.g. $imagick->newPseudoImage(100, 100, "xc:gray");)
The transformImageColorSpace method is the right one to use for changing an existing images colorspace.
$image= "$img.jpg";
$i = new Imagick($image);
$i->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$i->writeImage($image);
$i->destroy();

PHP Imagick - convert image to greyscale (very bad result)

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

PHP: how to create an image from another PNG image

I have a small Minecraft server where people can upload their skins. Minecraft skins are small png images. Is it possible to convert this png image to another png image via PHP (e.g. GD library)?
I have made this image to help me explain what I am trying to do:
Yes, it's possible. You'd need multiple imagecopy commands to pull out sections of the skin image and paste it into the proper spots in the "output" image.
Basic order of operations would be:
$input = imagecreatefrompng('skin.png');
$output = imagecreatetruecolor(800, 600); // whatever the dimensions should be.
imagecopy($output, $input, 0,0, 10,20, 50,60);
imagecopy(...);
...
...
The first copy command is saying "take a 50x60 section of the input image, starting at coordinates 10x20, and paste it into the destination image in the top left corner".
The actual sequence/coordinates/sizes will be up to you to figure out.
If you're not doing a 1:1 copy of the image and are doing resizing, then you'll want imagecopyresampled() instead.
Here is the PHP manual for creating images from png :
http://php.net/manual/en/function.imagecreatefrompng.php
Here is a simple tutorial :
http://www.phptutorial.info/?imagecreatefrompng
You can do this with CSS
Here is a tutorial: http://www.w3schools.com/css/css_image_sprites.asp

Categories