Imagemagick remove the upper content while converting pdf to image - php

My pdf first page looks like :
When I run below command:
exec("convert -density 300 $pdf_path $temp_images 2>&1",$output);
It converts its page in image that looks like:
this thing happen only when the dimensions of pdf are width- 595 and height- 842.
Any suggestion will be appreciated.

Looks like the CropBox of the PDF is being used instead of the media size, or possibly ImageMagick is sending a fixed (incorrect, Letter) media size to Ghostscript in order to render the page.
Unfortunately that's about all I know regarding ImageMagick, you need someone who can tell you how to find and alter the Ghostscript invocation.

This code solve my problem but I want this in command line
$im = new Imagick();
$im->readImage( $pdf_path );
$im->setImageFormat( "jpg" );
$im->writeImage( $temp_images );
echo 'Image Converted';

Related

How to generate images from PDF files in 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.

PHP: Displaying ImageMagick image without saving to server like GD

This question has already been asked (see link below) but none of the answers work. So I have this ImageMagick script that I am using to tint PNGs and it works great but the problem is that it actually generates files on the server. What I want instead is exactly what GD does where it does the image manipulation and then displays it without actually saving an image.
Here is my ImageMagick code that I use to tint the image. This code does the converting and generates an extra file on the server which is the final image.
<?php
$source = "src.png";
$final = "FINAL.png";
$color = "#00FF00";
exec("convert $source -threshold 100% +level-colors '$color', $final");
?>
Here is a GD example code which does an image manipulation and displays the final image directly without saving extra images to the server:
<?php
header('Content-Type: image/png');
$source = "src.png";
$im = imagecreatefrompng($source);
imagefilter($im, IMG_FILTER_GRAYSCALE);
imagepng($im);
imagedestroy($im);
?>
So essentially I want the image manipulation that is done in the first example, but without saving extra images and displaying the output in the browser.
Links searched:
None of the solutions worked:
Generate images with ImageMagick without saving to file but still display them on website
How can I convert my ImageMagick code to iMagick? PHP-Imagemagick image display
A direct example using your code for others to learn from.
I use this same method on my shared Linux server on Godaddy.
<?php
$source = "src.png";
$color = "#00FF00";
$cmd = "convert $source -threshold 100% +level-colors '$color',".
" -unsharp 0.2x0.6+1.0 -quality 50 JPG:-";
header("Content-type: image/jpeg");
passthru($cmd, $retval);
exit();
?>
Note: - Are you sure you are using "-threshold 100% +level-colors '$color'," correctly? Threshold 100% will push an image to black. Which then +level-colors '#00FF00', will just make a solid green image. I am assuming you have simplified the code for this demonstration.
Note: - "+level-colors '$color'", does not work on Godaddy's servers. It works fine on my home server though. Possibly an outdated ImageMagick version installed on Godaddy's server.

Converting transparent PDF files with Imagick

I have a problem creating thumbnails with PHP and imagick. The code is working ok and the thumbnail is generated in the correct size etc but when I try to place a PDF logo on the thumbnail it turns half transparent. I guess it has something to do with the PDF file being generated in InDesign and it probably doesn't have any background defined. Has anyone come across this problem or has an idea what to do about it? I tried to put a white canvas in the background but that didn't help. I also specified a channel for the compositeImage function but that didn't help either.
This is the PDF file I'm having issues with: https://dl.dropbox.com/u/13712643/Case_Study.pdf
The generated Thumbnail looks like this:
https://dl.dropbox.com/u/13712643/Case_Study1.jpg
The code I have produced so far: http:// pastebin.com/74CYC972
Any ideas? Thank you for your help.
I had the same issue and I solved it by using Imagick::compositeImage that was found in here: php imagick convert PNG to jpg
The code goes something like this:
$im = new Imagick();
$im->readimage($pdfFile."[$currentPage]");
$res = $im->getimageresolution();
$bg = new Imagick();
$bg->setresolution($res["x"],$res["y"]); //setting the same image resolution
//create a white background image with the same width and height
$bg->newimage($im->getimagewidth(), $im->getimageheight(), 'white');
$bg->compositeimage($im, Imagick::COMPOSITE_OVER, 0, 0); //merging both images
$bg->setimageformat("png");
//then you can write to a file
$bg->writeImage('white-background-pdf-image.png');
//or output it
header('Content-type: image/png');
echo $bg;
May be this what you are looking for :
$im->setBackgroundColor(new ImagickPixel('transparent'));
http://www.php.net/manual/en/imagick.setbackgroundcolor.php
None of the existing answers worked for me. Flatten the image just after creating a new imagick() worked instead:
$im = $im->flattenImages();
Edit: The flattenImages method has been deprecated & removed. Use
$im = $im->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );

Image Magick create thumbnail

Hi i am creating a thumbnail using imagemagick by following command
convert -define jpeg:size=".$this->info[0]."x".$this->info[1]." testi/".$this->filename." -auto-orient -thumbnail 200x200 -unsharp 0x.5 testi/".$this->filename
but this command only runs for jpack input file.
Can anyone tell me the command for any file type? like if gif then output shuld gif ?
Here is code:
<?php
// read page 1
$im = new imagick( 'test.pdf' );
// convert to jpg
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(60);
$im->setImageFormat('jpeg');
//resize
$im->resizeImage(290, 375, imagick::FILTER_LANCZOS, 1);
//write image on server
$im->writeImage('thumb1.jpg');
$im->clear();
$im->destroy();
?>
What is a jpack file?
The -define will only work for jpg files but I thought if the output file was not a jpg it would be ignored; try removing that.
The output file should be the same name as the input file.
Try writing your code like this so you can see what the command actualy is; you can comment the echo out when its working.
$cmd = "-define jpeg:size={$this->info[0]}x{$this->info[1]} testi/{$this->filename} -auto-orient -thumbnail 200x200 -unsharp 0x.5 testi/{$this->filename}";
echo $cmd;
exec("convert $cmd");
I got a bit lost with the "$this->" business and so the code may not work as written. I try and keep things simple in my commands and would have put the filenames etc. into a variable outside the Imagemagick command.

Imagemagick - PDF to Jpeg/Raster texture issue

I am using imagemagick/php to make a jpeg file from a PDF.
Input PDF file:
PDF-file
Output Jpeg file:
Jpeg-file
The textures on the output file look wrong near the bottom. This is the same result if I make a PNG also. I have tired different floor plans, other textures play up also in a similar way.
PHP code
$im = new Imagick();
$im->setResolution( 300, 300 );
$im->readImage( $input_path );
$im->setImageFileName($output_path);
$im->writeImage();
Server Config
PHP Version 5.3.5
ImageMagick 6.4.8
Thank you.
It looks like ImageMaigck handles opacity in embedded images in PDFs incorrectly. I am not familiar enough with ImageMagick to have a solution for you, but it is discussed here in their documentation -- perhaps there is an option you can change to improve things.

Categories