Php api for reading any text from image wahtever we uplaod - php

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.

Related

Convert Audio files to .flac format using php code

I have audio files on my server and wanted to convert them into flac format in order to convert them into text. Please let me know how can we achieve that..
You can use FFmpeg:
https://ffmpeg.org/
ffmpeg -i input.mp3 output.flac
There is a php wrapper for the ffmpeg binary on github.
https://github.com/PHP-FFMpeg/PHP-FFMpeg
In case you can't install ffmpeg you might want to try this free API:
<?php
$url = 'http://server.com/sound.mp3';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/sound_convert.php?url=' . $url . '&format=flac'));
if (#$data->success !== 1)
{
die('Failed');
}
$flac = file_get_contents($data->file);
file_put_contents('sound.flac', $flac);

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);

convert pdf files to image using imagemagick

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.

PHP ImageMagick get image from http

I am trying to use ImageMagick for some photo manipulation (crop for the moment). I succeed previously the desired outcome by using GD library for PHP (on my localhost) but now the hosting doesn't support it and they suggest me to use ImageMagick instead. The issue is that I can crop an image that it is stored locally :
<?php
$four = '4fingers1.jpg';
exec("convert $four -crop 100x100+100+100 test.jpg");
?>
<img src="test.jpg">
with no problem but when it comes from http sources (the actual purpose of my script) I receive no image. The code is the following (I post with a form the actual src of the image):
$src = $_POST['src'];
exec("convert $src -resize 720x720 resized.jpg"); // this is specified in the documentation
exec("convert resized.jpg -crop 100x100+100+100 final.jpg");
?>
<img src="final.jpg"> <!-- no image -->
<img src="resized.jpg"> <!-- no image -->
<img src="<?php echo $src; ?>"> <!-- alright -->
The documentation specify the following:
IM can also download an image that is published on the 'world wide
web' by specifying that images URL. This basically provides a 'http:'
image coder, which is why it works.
Link: http://www.imagemagick.org/Usage/files/#read
I've tried a lots of code snippets founded online and can't figure it out why is not working? I can use maybe something else to reach my purpose?
First of all, as Prisoner comments this code is vulnerable to shell argument injection. Basically anyone can manipulate the post parameters and cause you to execute any command line they want; this is potentially disastrous and should be addressed immediately!
One way to fix the security issue is use escapeshellarg; this should also fix your "original" problem since there is no apparent reason for the command to fail. If the URL is valid and the argument is properly escaped it should just work.
Alternatively, you can solve both the security headache and your original problem by downloading the image yourself and saving it to a temporary file before calling IM:
$temp = tempnam(sys_get_temp_dir());
file_put_contents($temp, file_get_contents($url));
exec("convert ".escapeshellarg($temp)." -crop 100x100+100+100 test.jpg");
First I'm with the answer of Jon and the comment of Prisoner. You'll have to escape the $src POST var to prevent from shell command injection using escapeshellarg():
$src = escapeshellarg($_POST);
Further you should use the second param (output of command) and the third param (return value of command) and respect it.
Call exec() like this:
exec("convert $src -resize 720x720 resized.jpg 2>&1", $output, $returnval);
if($returnval !== 0) {
die('imagemagick error: ' . join("<br/>", $output));
}
Now you are able to react on errors and you can see what's going wrong. Note that I've redirected stderr of imagemagick to stdout using shell redirection ( 2>&1 )
The problem was that IM have a problem with the https protocol in my case. I've tested with a http image and everything was working fine so I made the following:
<?php
$src = $_POST['src'];
$src = preg_replace('/^https\:\/\//', 'http://', $src);
$src = escapeshellarg($src);
exec('convert ' . $src . ' resized.jpg 2>&1', $output, $returnval);
if($returnval !== 0) {
die('imagemagick error: ' . join("<br/>", $output));
}
?>
<img src="resized.jpg">
I have the image now displayed and I can start crop it and so on.

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