I'm trying to convert PDF to IMG (JPG) with help PHP.
I'm using imagick extension.
this is my code
$fp_pdf = fopen($pdf, 'rb');
$img = new imagick(); // [0] can be used to set page number
$img->readImageFile($fp_pdf);
$img->setImageFormat( "jpg" );
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
$img->setResolution(300,300);
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$data = $img->getImageBlob();
my source pdf file has right dimension (210x297 mm, like A4 has). And everything looks good.
But my jpg has page dimension as 842x595 px, and DPI is 72.
and img file much more smaller on paper then pdf, when i had print it.
what is a proper way to make image file from pdf and make it so big as pdf (on paper)
You could use imagemagick through exec() or similar, the shell arguments are much less verbose than the PHP extension.
$pdf_file = escapeshellarg( "mysafepdf.pdf" );
$jpg_file = escapeshellarg( "output.jpg" );
$result = 0;
exec( "convert -density 300 {$pdf_file} {$jpg_file}", null, $result );
// at this point $result should == 0 if the conversion was successful
It's the "-density" (which sets the DPI to read the source file as) option that specifically fixes your problem.
Also imagemagick by default uses a -quality setting of 92 for JPEG writing in most cases - so you probably don't need to explicitly declare it.
It looks like you missed two setters:
Imagick::setImagePage() http://www.php.net/manual/en/function.imagick-setimagepage.php
And:
Imagick::setImageExtent() http://www.php.net/manual/en/function.imagick-setimageextent.php
In order to get the correct parameters for these functions, you may try the following:
$fp_pdf = fopen($pdf, 'rb');
$params=array();
$img = new imagick();
$img->readImageFile($fp_pdf);
/*my modification: */$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
/*my modification: */$params=$img->identifyImage();
$img->setImageFormat( "jpg" );
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
/*my modification: */$img->setPage($params['geometry']['width'], $params['geometry']['height'], 0, 0)
/*my modification: */$img->setResolution($params['resolution']['x'], $params['resolution']['y']);
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$data = $img->getImageBlob();
If you find that some others attributes should be set, then let me show you the information that $params is holding. It may proof useful for you:
$params=array(
[imageName],
[format],
[geometry] => Array
(
[width]
[height]
)
[type],
[colorSpace],
[resolution],
(
[x]
[y]
)
[units],
[fileSize],
[compression],
[signature],
)
To be honest, I'm not completely sure if this will work. Is just a try in order to help you. I sincerely hope it does.
ImageMagick uses GhostScript to process JPEGs, so you'd do better to exec GhostScript directly, which would be much more efficient and give you more control. It would also be only 1 exec statement, instead of playing around with the IMagick functions.
As mentioned before, setting the resolution before reading the file does the trick:
$fp_pdf = fopen($pdf, 'rb');
$img = new imagick(); // [0] can be used to set page number
$img->setResolution(300,300);
$img->readImageFile($fp_pdf);
$img->setImageFormat( "jpg" );
$img->setImageCompression(imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$data = $img->getImageBlob();
You have to call setResolution before reading the image. Otherwise imagemagick will use the default system dpi.
Related
I get an image back from an API, and depending on the parameters, sometimes it's in landscape mode and sometimes portrait. I could hardcode a check of the parameter settings to determine what the mode is but I'd rather examine the image and figure it out. The "Orientation" field returned by identify is "Undefined" - is there another way for me to figure out what the orientation is?
This method returns just the width & height as an associative array: getImageGeometry
An example to figure out landscape or portrait using Imagick image sizes:
$filename = '/path/to/image.jpg';
$image = new imagick($filename);
$size = $image->getImageGeometry();
$orientation = ( $size['width'] > $size['height'] ) ? 'landscape' : 'portrait';
echo $orientation;
If you have a 'square' image... it will be considered 'portrait'. You can adjust if it matters.
So, I'm testing some of our images with the Imagick PHP library, to see what compression we want to use. But there doesn't seem to be any change to the output file, no matter what I do. This is my basic process:
$original_image = new \Imagick( $image_url );
foreach ( ['original', '92', '80', '60', '40'] as $compression_size )
{
$tester = clone $original_image;
// don't compress the original
if ( 'original' != $compression_size )
{
$tester->setImageCompression(Imagick::COMPRESSION_JPEG);
$tester->setCompressionQuality( (int) $compression_size);
}
$filename: <original base> . "-$compression_size.jpg";
file_put_contents($filename, $tester->getImageBlob() );
$tester = null;
}
The results show that the file sizes between the various compressions don't change, and visually, there is no difference between the original and even the compression = 40 version. What am I doing wrong here?
From the docs (http://php.net/manual/en/imagick.setcompressionquality.php):
This method only works for new images e.g. those created through Imagick::newPseudoImage. For existing images you should use Imagick::setImageCompressionQuality().
So, replace
$tester->setCompressionQuality( (int) $compression_size);
for
$tester->setImageCompressionQuality( (int) $compression_size);
I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that
Storage::disk('s3')->get(imageUrlonS3);
retrieve the image in binary and is giving me errors. This is my code:
function createSlices($imagePath) {
//create transform driver object
$im = imagecreatefromjpeg($imagePath);
$sizeArray = getimagesize($imagePath);
//Set the Image dimensions
$imageWidth = $sizeArray[0];
$imageHeight = $sizeArray[1];
//See how many zoom levels are required for the width and height
$widthLog = ceil(log($imageWidth/256,2));
$heightLog = ceil(log($imageHeight/256,2));
//more code here to slice the image
.
.
.
.
}
// ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg
$content = Storage::disk('s3')->get(imageUrlonS3);
createSlices($content);
What am I missing here ?
Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself.
If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have.
Here's the relevant documentation:
createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php
getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php
Resulting code might look something like this:
function createSlices($imageData) {
$im = imagecreatefromstring($imageData);
$sizeArray = getimagesizefromstring($imageData);
//Everything else can probably be the same
.
.
.
.
}
$contents = Storage::disk('s3')->get($imageUrlOnS3);
createSlices($contents);
Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.
I'm trying to merge an animated gif into a static jpeg as a background, this is being generated by html canvas and a file upload that the user uploads. I had it working great with imagemagick command line
exec("convert ".$_paths['images_dir'].$fname.".gif \( gif/cleared_dance".$dance.".gif -resize '80%' -repage 0x0-10+$imgHeight\! \) -coalesce -delete 0 -deconstruct -loop 0 ".$_paths['images_dir'].$fname.".gif");
this was taking about 15~ seconds to process which was acceptable, but then I found out I can't use the exec command and must use Imagick API, I was eventually able to recreate the process using the following code:
$data = $_POST["imgData"];
$dance = $_POST["dancer"]+1;
$spriteLeft = $_POST['left'];
$spriteTop = $_POST['top'];
$scale = $_POST['scale'];
$canvas = new Imagick();
$data = str_replace("data:image/png;base64,", "",$data);
$data = base64_decode($data);
$canvas->readImageBlob($data);
$fname = 'tmp_'.uniqid();
$gif = new Imagick();
$gif->readImage('gif/cleared_dance'.$dance.'.gif');
do {
$canvas->addImage($gif);
} while ($gif->nextImage());
foreach($canvas as $frame){
if($frame->getIteratorIndex() != 0){
$page = $frame->getImagePage();
$frame->setImagePage(200,200,$page['x']+$spriteLeft,$page['y']+$spriteTop);
}
}
This code takes about 5 seconds which is great but their is a blank frame in the gif when it loops that was super annoying so i added this after:
$canvas->setIteratorIndex(0);
$canvas->removeImage();
$canvas = $canvas->deconstructImages();
$canvas->writeImages('tmp/'.$fname.'.gif', true);
This takes out the blank frame, but adds about 20 - 30 seconds of processing. Am I doing something wrong, I've played around a good bit with the order of the code and trying different approaches. Is there a way to get the processing time back under 20 seconds like it was with the command line?
Your code is quite odd. Why are you setting the format of the Imagick object that is going to contain an animated gif to JPG? Why are you fiddling with the image page after you composited the image?
I'm pretty sure you want your code to look a lot more like
$static = new Imagick();
//$static->readImage("./testFile.png");
$inputGif->readImageBlob($data);
$inputGif = new Imagick();
//$inputGif->readImage('./testGif.gif');
$inputGif->readImage('gif/cleared_dance'.$dance.'.gif');
$gifFrames = $inputGif->coalesceImages();
$outputGif = new Imagick();
$outputGif->setFormat('gif');
foreach ($gifFrames as $gifFrame) {
$frame = clone $static;
$frame->compositeimage(
$gifFrame,
Imagick::COMPOSITE_OVER, // or maybe \Imagick::COMPOSITE_ATOP,
0, 0 // Set the offset here if you don't want the gif in the top left corner.
);
$outputGif->addImage($frame);
}
$outputGif->setImageIterations(0); //loop forever
$outputGif->deconstructImages();
$outputGif->writeImages("./outso.gif", true);
Loads of answers on how to do it for a command line
convert /path/to/file/file.pdf[3] output.jpg
great... but what if I am using in memory processing, I am generating PDF with PDFlib and then output its buffer to a function that I want to generate jpg preview of selected page. How? My code :
[...]
$buf = $pdf->get_buffer();
//$buff is just a PDF stored in a string now.
$im = new Imagick();
$im->readimageblob($buf);
$im->setImageFormat("jpg");
$im->setimagecompressionquality(60);
$len = strlen($im);
header("Content-type: image/jpeg");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=test.jpg");
echo $im;
This creates a jpeg but always returns last page of the PDF. I want to be able to choose which one will be converted. Is it doable without saving temporary files and using command line (exec('convert /path/to/file/file.pdf[3] output.jpg')) syntax?
Let me add that I tried
$im->readimageblob($buf[2]);
and it did not work :)
For the ones who is still searching for solution of reading specific page number from blob, please check this question Creating array populated by images from a PDF using PHP and ImageMagick
$img_array = array();
$im = new imagick();
$im->setResolution(150,150);
$im->readImageBlob($pdf_in);
$num_pages = $im->getNumberImages();
for($i = 0;$i < $num_pages; $i++)
{
$im->setIteratorIndex($i);
$im->setImageFormat('jpeg');
$img_array[$i] = $im->getImageBlob();
}
$im->destroy();
I'm loading the PDF binary into memory from Amazon S3 and then selecting the specific page I want using setIteratorIndex() followed by getImage()
function get_image_from_pdf($pdf_bytes, $page_num){
$im = new \Imagick();
$im->setResolution(150, 150);
$im->readImageBlob($pdf_bytes);
$im->setIteratorIndex($page_num);
$im = $im->getImage();
$im->setImageFormat('png');
return $im->getImageBlob();
}
version 3.0.1
being on the last image or first image in imagic object
$image_obj = new Imagick("test.pdf"); then you on last image in $image_obj
if you use
fp_pdf = fopen("test.pdf", 'rb');
$image_obj = new Imagick();
$image_obj -> readImageFile($fp_pdf);
then you on the first image in $image_obj
In second case to switch to last image you can do
fp_pdf = fopen("test.pdf", 'rb');
$image_obj = new Imagick();
$image_obj -> readImageFile($fp_pdf,2); // 2 can be any positive number?
then you on the last image in $image_obj
echo $image_obj->getNumberImages() // Returns the number of images in the object
then
if ($image_obj->hadPreviousImage)
$image_obj->previousImage() //Switch to the previous image in the object
}
or
if ($image_obj->hasNextImage()) {
$image_obj->nextImage()) //Switch to the next image in the object
}
e.g. if you have 6 images total and you need 4th then do from the end
$image_obj->previousImage()
$image_obj->previousImage()
$image_obj->setImageFormat("png");
header("Content-Type: image/png");
echo $image_obj;
EDIT: Another find is that you can
foreach($image_obj as $slides) {
echo "<br>".$Obj_img->getImageWidth();
//or wehatever you need to do.
}
EDIT 2: Very simple solution would be to use this function $image_obj->setIteratorIndex(4) count starts with zero.
It's not good news unfortunately, but I can definitively say that, as of time of writing, the ImageMagick (and PHP libraries) don't support the page notation that you're trying to use. (For people from the future finding this: I'm checking php-imagick-3.0.1 and imagemagick-6.6.0.4).
I'm trying to do the exact same thing as you, and I've just spent the last few hours trawling through the source, trying to figure out what it does and how it gets the pages, and it looks like it simply won't use it when reading from a stream (ie. the readBlob() call).
As such, I'm just going to be putting it in a temporary file and reading it from there instead. Not as elegant, but it'll work.