Imagick PHP only displaying last page - php

I am using imagick with PHP/Windows IIS. I have a simple script that converts a TIF file to PDF an presents it to the browser. It works flawlessly with single page TIF files, but with multiple pages, its only showing the last page.
I understand that it shows the last page by default because the $im variable is an array. Any attempt I make to fix it makes it an invalid PDF. Below is my code. I am new to imagick and any help is appreciated!
$im = new imagick("tmp/tmp.tif");
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im;
ImageMagick version ImageMagick 7.0.7-11 Q16 x64 2017-11-23
ImageMagick library version ImageMagick 7.0.7-11 Q16 x64 2017-11-23
(this is very rough testing code, I will clean it up later)

The internal image iterator is pointing at the last page read. You just need to reset it to the first page with Imagick::setFirstIterator.
$im = new imagick("tmp/tmp.tif");
$im->setFirstIterator();
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im->getImage();
Or even
$im->setIteratorIndex(0);
Edit based on comments
If you are attempt to output the entire PDF document, you would use Imagick::getImagesBlob.
$im = new imagick("tmp/tmp.tif");
$im->setFirstIterator();
$im->setImageFormat('pdf');
$blob = $im->getImagesBlob();
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($blob));
echo $blob;

Related

How to trim or remove black part from image in php imagick?

I have created an image using Imagick in PHP. But there is a black part in the background. I want to remove it. For more clarification, i have attached the image.
`
header('Content-type: image/jpeg');
$image = new Imagick('image.png');
$image->trimImage(.2);
echo $image;`
Code works fine for me. Your Imgagick Version has to be 6.2.9 or higher: https://www.php.net/manual/en/imagick.trimimage.php
Have you tried using $image->trimImage(0.2) ?

ECC200 Datamatrix Generation in PHP

Has anybody had any experience with generating 2D Barcodes for Royal Mail via PHP? I've spent a while attempting to get my own routines to write a valid datamatrix sadly to no avail.
I do have working conversion routines for ASCII to C40 and Luhn 16 checksum makers but just can't get anywhere with the graphical representation, or the ECC200 byte creation for that matter.
Are there any pre-written libraries out there with documentation that would help take away a lot of further legwork?
I do need to be able to generate this within the server environment, without using external sites ofr image generation ideally.
We use Zint Barcode Generator Unix packages for QR and PDF417 code generation. Royal Mail is supported as well.
(on CentOS dnf install zint, Ubuntu takes more work).
Zint documentation: http://www.zint.org.uk/
In PHP use the system method, example:
$targetFilePath = dirname(__FILE__).'/test.png';
$contents = 'ABC123';
system('zint ...params... -o"' . $targetFilePath . '" -d"' . $contents . '"');
var_dump(file_exists($targetFilePath));
It will generate an image on the requested $targetFilePath.
For ECC200 Datamatrix Generation in PHP we successfully used:
sudo apt install dmtx-utils
To output a PNG file from the server, with normal apache2 settings you would get
the barcode in PNG when you enter in the browser: http://yourserver.com/datamatrix/?in=yourbarcodetext
<?php
ob_start();
$old_path = getcwd();
$infile = "/var/www/html/datamatrix/message2.txt";
$image = "/var/www/html/datamatrix/image.png";
file_put_contents($infile,$_GET["in"]);
$ex = "export HOME=/tmp && /usr/bin/dmtxwrite {$infile} -o {$image}";
echo "<b>$ex</b>";
$output = shell_exec($ex);
echo var_export($output, TRUE);
echo "done";
chdir($old_path);
$im = imagecreatefrompng($image);
ob_end_clean();
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

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.

Use Imagick on XAMPP1.7.7 for save image

I have the following code block:
<?php
$im = new imagick('test.jpg');
echo "a";
?>
if $im is omitted, the result is "a" but if $im isn't omitted then the result is "". Is this because imagick isn't installed well? If that's the answer, how can I install it well for XAMPP 1.7.7? On the net there's little information and they talk about using old versions of imagick but I don't like that idea.
Another question I have about imagick is that I want to reprocess an image and save it in my server. I have read about the use of move_uploaded_file but if I use writeimage I won't use that. Do I need to use it in a different way?
Last question: I pass an image encoded in 64 base and I decode in a php file. How can I assign the decode image to Imagick without saving it before?
To display.
<?
$im = new imagick('test.jpg');
$im->setImageFormat('jpg');
header('Content-type: image/jpg');
echo $im;
?>
To save
<?
$im = new imagick('test.jpg');
$im->setImageFormat('jpg');
$saveToDir = "/home/";
$imageName = "image.jpg";
$im->writeImage($saveToDir . $imageName);
?>

GD: php imagepng creates whitespace in image

I have some problem with GD when i creates images with php.
The strange thing is that it works on one server with php version 5.3.1 but not on php version 5.2.14. (I'm not sure if it's the php version or the GD lib that is doing this.)
This file is created with convert and saved in a directory in captcha::get_file().
And this file is generated with imagecreatefrompng() and imagepng()
I made some small changes to the script and made a gif. But there is still a problem with the png
What causes this, and how can I fix it?
Here is the phpcode:
<?php
session_start();
require_once("./captcha.php"));
// creates the image with convert and returns the location of the picture
// document_root/picture/picture.png
$picloc = captcha::get_file();
$image = #imagecreatefrompng($picloc);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
unlink($picloc);
?>
not positive, but the problem may be in your content length header.
header('Content-Length: ' . strlen($image));
at this point in your code, $image is a resource data type, not a string. try simply removing the content-length header line, and see what happens.

Categories