convert pdf files to image using imagemagick - php

i have installed imageMagick and ghostscript from online and put it inside mamp but m not getting how to include them in my php code..
by googling i found a code
<?php
$pdf = 'serviceReport.pdf';
$save = 'output.jpg';
exec('convert "'.$pdf.'" -colorspace RGB -resize 800 "'.$save.'"', $output, $return_var);
?>
but m not getting the result..can anyone help with this

It is much effective to use the php extension ImageMagick offers (Imagick) instead of command line tools:
<?php
// ...
$img = new \Imagick();
$img->readimage($filedata['tmp_name']); // this can be a pdf file!
$img->setResolution(300,300);
$img->writeImage(sprintf('%1$s/%2$s.jpg', $basepath, $basename));
// ...
?>
Another alternative is to use the poppler tools instead. They offer much faster processing.

Related

Php api for reading any text from image wahtever we uplaod

I need help in to read text from image whatever we upload.Is there any library for this. I am using Tesseract PHP OCR.
But not getting idea to use it . I am attaching my file here.
Tesseract file is from here: https://github.com/thiagoalessio/tesseract-ocr-for-php/tree/master/src
and my php i have written attached image.enter image description here
Here is a little script I use to do ocr for pdfs on ubuntu 16.04
$inputPDF = 'path/to /your/file';
$fileToOCR = "ocr.tiff";
exec("convert -density 300 $inputPDF -depth 8 -strip -background white -alpha off $fileToOCR");
$outputOCR = "ocr";
exec("tesseract $fileToOCR -l deu+eng $outputOCR hocr");
note that you need tesseract-ocr and imagemagick installed sudo apt-get install tesseract-ocr imagemagick
also you need the language packs you want to use sudo apt-get install tesseract-ocr-[lang]
exec("convert ..."); prepares the file for better results
exec("tesseract ... "); does the actual ocr where deu+eng is the language from the text and hocr is the output format (xml with additional infos where the text was found)
hope it helps
Hello you can use this libary for that
https://www.phpclasses.org/package/3312-PHP-Hide-encrypted-data-in-images-using-steganography.html
You can use this API (it's free):
<?php
$url = 'http://server.com/image.png';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/ocr.php?url=' . $url . '&format=txt'));
if (#$data->success !== 1)
{
die('Failed');
}
$txt = file_get_contents($data->file);
file_put_contents('text.txt', $txt);
You should just replace $url with URL to your image file and the output will be save as text.txt.

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.

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.

Imagick: Failed to query the font metrics

I'm working on (what should be) a simple script that will add text to an image. After going over my script several times looking for any mistakes I finally decided to try running a sample from php.net and I encountered the same, nondescript, error: "Failed to query the font metrics". Here's the code:
/* Text to write */
$text = "Hello World!";
/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // Transparent
/* Font properties */
$draw->setFont('Arial');
$draw->setFontSize(50);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
/* Get font metrics */
$metrics = $image->queryFontMetrics($draw, $text);
/* Create text */
$draw->annotation(0, $metrics['ascender'], $text);
/* Create image */
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);
/* Save image */
file_put_contents('/tmp/file.png', $image);
I can not for the life of me find any information via google about this error. Nor can I find adequate documentation on this method or potential causes of failure. Basically, I'm stumped. If anyone could provide insight or a fix it would be greatly appreciated.
ImageMagic version: ImageMagick 6.6.5-10 2011-04-06 Q16
Imagick module version: 3.1.0b1
have you tried cli version ? is imagemagick installed on server ? if yes
then run a command like
system('convert -background lightblue -fill blue \
-font Candice -pointsize 72 label:Anthony \
label.gif ');
see if you have imagenamed label.gif in server after running script.
for your reference http://www.imagemagick.org/Usage/text/
For those having similar issues, the PHP Imagick exceptions aren't always the most descriptive. I could have saved myself a lot of time examining the output from the Image Magic application installed on the server first. Just something to keep in mind. To view a list of the currently installed delegates(modules) use the command convert -list configure and examine the line that starts with "DELEGATES" from the output. If you encounter the same error, I recommend checking here first. I found I was missing the freetype and ghostscript delegates. After installing the dependancies and a quick recompile of ImageMagick everything works like a charm.

Categories