ImageMagick php imagick loading a file from memory - imagemagick

I m trying to integrate imagick into my framework. The framework has the file in memory.
The imagick code works great
/* Read page 1 */
$im = new imagick( 'test.pdf[0]' );
/* Convert to png */
$im->setImageFormat( "png" );
/* Send out */
header( "Content-Type: image/png" );
echo $im;
Now my problem is imagick constructor takes only path name. How can i load the pdf from memory.
like $im = newImagick($file);

$im = new Imagick();
$im->readImageBlob($file_contents);
http://www.php.net/manual/en/function.imagick-readimageblob.php

Related

how can we apply threshold effect on png image using php

I have some greyscaled png image or colorful images. i want to apply the threshold effect using php language.
I am still looking for the solution.
<?php
function thresholdimage($imagePath, $threshold, $channel) {
$imagick = new \Imagick(realpath($imagePath));
$imagick->thresholdimage($threshold * \Imagick::getQuantum(), $channel);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
}
?>

Is ImageMagick the fastest way to overlay images? How do I make faster or is there a faster tech I'm unaware of?

I'm in the process of building an image manipulation Nginx CDN/cache server to overlay millions of unique SVG designs files on apparel jpegs. Similar tutorial here: http://sumitbirla.com/2011/11/how-to-build-a-scalable-caching-resizing-image-server/
I have written a test script here:
<?php
$cmd = "composite GOSHEN.svg blank-tshirt.jpg -geometry 600x700+456+335 JPG:-";
header("Content-type: image/jpeg");
passthru($cmd);
exit();
?>
Here is an example result:
My issue is that ImageMagick is so slow. Besides more CPU/Memory, are there any tricks to make it faster? Are there any alternative technologies that could be faster to overlay images?
Any help is much appreciated.
php-vips can be quite a bit quicker than imagick. I made a test program for you:
#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';
use Jcupitt\Vips;
for($i = 0; $i < 100; $i++) {
$base = Vips\Image::newFromFile($argv[1], ["access" => "sequential"]);
$overlay = Vips\Image::newFromFile($argv[2], ["access" => "sequential"]);
// centre the overlay on the image, but lift it up a bit
$left = ($base->width - $overlay->width) * 0.5;
$top = ($base->height - $overlay->height) * 0.45;
$out = $base->composite2($overlay, "over", ["x" => $left, "y" => $top]);
// write to stdout with a mime header
$out->jpegsave_mime();
}
Using the test images from your server:
http://build9.hometownapparel.com/pics/
Then running on my desktop machine (Ubuntu 17.10, a fast i7 CPU) I see:
$ time ./overlay.php blank-tshirt.jpg GOSHEN.svg > /dev/null
real 0m2.488s
user 0m13.446s
sys 0m0.328s
So about 25ms per image. I see this result (taken from the first iteration, obviously):
I tried a looping version of your imagemagick example:
#!/usr/bin/env php
<?php
header("Content-type: image/jpeg");
for($i = 0; $i < 100; $i++) {
$cmd = "composite GOSHEN.svg blank-tshirt.jpg -geometry 600x700+456+335 JPG:-";
passthru($cmd);
}
Running it against IM-6.9.7-4 (the version packaged for Ubuntu) I see:
$ time ./magick.php > /dev/null
real 0m29.084s
user 0m42.289s
sys 0m4.716s
Or 290ms per image. So on this test, php-vips is more than 10x faster. It's a bit unfair: imagick would probably be a little quicker than just shelling out to composite.
There's another benchmark here:
https://github.com/jcupitt/php-vips-bench
On that one, php-vips is about 4x faster than imagick and needs 8x less memory.
Here's the whole thing packaged as a Dockerfile you should be able to run anywhere:
https://github.com/jcupitt/docker-builds/tree/master/php-vips-ubuntu-16.04

How download files on app mobile with PHPSpreadsheet?

for my project I'm using PHPSpreadsheet for exporting data in Excel and PDF format.
If from the website, when I press on the respective download buttons, I can easily download the .xlsx and .pdf files, I have problems with the app (iOs Swift language). In fact, when I try to download the files, I receive the following messages:
for Excel files: "the file may be damaged"
for PDF files (I open it with Chrome): "Chrome does not support this link file: ///var/mobile/Containers/Data/Application/.../Documents/name_file.pdf"
Here are the fragments of the PHP code that deal with the download:
PDF:
$ output. = '... html code';
$ mpdf = new \ Mpdf \ Mpdf ();
$ mpdf-> Bookmark ('Start of the document');
$ Mpdf-> WriteHTML ($ output);
$ mpdf-> Output ($ filename.'. pdf',
\Mpdf\Output\Destination::DOWNLOAD);
EXCEL:
$spreadsheet = new Spreadsheet(); /*----Spreadsheet object-----*/
$Excel_writer = new Xlsx($spreadsheet); /*----- Excel (Xls) Object*/
/* ...riempiendo foglio excel con i dati */
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition:
attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$Excel_writer->save('php://output');
Thanks for your help!
for Excel files: "the file may be damaged"
My blind guess is that when you peek the file content you will find Headers already sent PHP message polluting your file. If that is so, just call ob_start() before you call any of your header()s.

How to view DJVU file in UIWebview

I am having iOS application in which I want to view data from .djvu file. Is there any way to read .djvu file in Swift or inside UIWebview.
I have also tried following solutions to view djvu file in uiwebview but that doesn't help.
1. Directly open djvu file in uiwebview
let urlPage : URL! = URL(string: "http://192.168.13.5:13/myData/5451890-OP.djvu")
webView.loadRequest(URLRequest(url: urlPage))
2. Secondly i have tried to convert djvu file to pdf and the showing that converted pdf file into view.
Reference link: https://github.com/MadhuriMane/ios-djvu-reader
But that gives mirror image with low quality.
3. I have tried to preview file with the help of UIDocumentInteractionController() and it's delegate method but didn't work.
Please suggest any possible ways to do so.
Keeping in mind that .djvu files are less popular then similar formats like EPUB, MOBI, PDF and other eBook file formats, I would have following approach to workaround the problem.
1) Create a Web Service to convert djvu files to pdf
ex.: http://example.com/djvuToPdf/djvuFile/outputFile
2) Read the PDF file in UIWebView
To create a Web Service I would assume that you have access to any Linux distributed Server, in my case Ubuntu 16.04.
Step one: Install djvulibre
sudo apt-get install djvulibre-bin ghostscript
Step two: test run $ djvups inputFile.djvu | ps2pdf - outputFile.pdf.
You may also use the ddjvu command. However, files converted with ddjvu command are 10x larger than djvups command. You would want to consider to use --help the explore the settings like mode, quality and so on.
Step three: Creating a Web Service (to keep things simple, I use PHP, use anything at your convenience [Python golang])
<?php
$inputFile = $_GET['input_file'];
$outputFile = $_GET['output_file'];
// use shell exec to execute the command
// keep in mind that the conversion takes quite a long time
shell_exec(sprintf("djvups %s | ps2pdf - %s", $inputFile, $outputFile));
$name = $outputFile;
//file_get_contents is standard function
$content = file_get_contents($name);
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $content ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
echo $content;
?>
Final Step: load PDF in App
As Apple advise, consider using WKWebView in place of UIWebView.
if let pdfURL = Bundle.main.url(forResource: "pdfFile", withExtension: "pdf", subdirectory: nil, localization: nil) {
do {
let data = try Data(contentsOf: pdfURL)
let webView = WKWebView(frame: CGRect(x:20,y:20,width:view.frame.size.width-40, height:view.frame.size.height-40))
webView.load(data, mimeType: "application/pdf", characterEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
view.addSubview(webView)
}
catch {
// catch errors here
}
}

imagemagick quality image issue using php

I have convert the PDF to image,I try using the exec,by excute the linux command like '/usr/bin/convert -density 300x300 file.pdf[0] -resample 160 -resize 512x700! images/sample-0.jpeg 2>&1'.But i have read some articles that it slow as compare to imagick php module.so try using php with code
$uploadfile = "file.pdf";
$im = new Imagick ( $uploadfile . "[0]" );
$im->setImageFormat ( "png" );
$im->writeImage ( "p-10.png" );
As you can see that i am not setting the desity and resample,the quality of image is not good at all.In php manual also there no function that set the density,i also try with setResolution,but does not work.
Please help if any body any idea about this.
have dream day

Resources