简体   繁体   中英

App Engine PHP generate image thumb

I want to generate a thumbnail image for a PDF file stored on google bucket using imagick and PHP
I deploy my application on google app engine(GAE) standard environment
the problem is that I keep getting this error

Fatal error: Uncaught exception 'ImagickException' with message  
'UnableToWriteBlob `magick--1noB3XBwJhgfn': Read-only file system

I know that the file system the application is deployed to is not writable, but I need a way to achieve this...

this is my code

<?php
putenv('MAGICK_TEMPORARY_PATH='.sys_get_temp_dir());

$imagick = new Imagick();

// get the content of pdf url
$imagenblob=file_get_contents('http://www.pdf995.com/samples/pdf.pdf');      

// read the content and load it inn imagick instance
$imagick->readimageblob($imagenblob);                                        

// point to the first page, because I want thumbnail for first page
$imagick->setIteratorIndex(0);        

// set image format
$imagick->setImageFormat('png');       

// resize image
$imagick->resizeImage(320,320,Imagick::FILTER_LANCZOS,1,1);

// return the result
header("Content-Type: image/png");          
$base64 = 'data:image/png;base64,' . base64_encode($imagick);
exit($base64);

Maybe if I can change the directory which imagick use to write, but I was not able to achieve this !!

ImageMagick requires a temp directory to write artifacts during the delegate decoding/encoding process. I'm not familiar with , but the documents suggest tempnam() & sys_get_temp_dir() should be used.

putenv('MAGICK_TEMPORARY_PATH='.sys_get_temp_dir());
$imagick = new Imagick(); 
// ...

Also note, Ghostscript is used by ImageMagick for PDF decoding. Verify that the gs binary is installed & available if working w/ PDF files.

You're hitting one of the restrictions of the standard environment sandbox :

An App Engine application cannot:

  • write to the filesystem. PHP applications can use Google Cloud Storage for storing persistent files. Reading from the filesystem is allowed, and all application files uploaded with the application are available.

One option (more costly) would be to use the flexible environment, which doesn't have such restriction.

Another approach would be to try to configure imagick to not use intermediate/temporary files (no clue if it supports that) or to use in-memory file emulation (donno if PHP supports that, I'm a python user) and specify that via Imagick::setFilename .

Another thing to try would be using setFormat instead of setImageFormat , the notes on the setImageFormat suggest that it might be possible to make the transformations before/without writing to the file:

To set the format of the entire object, use the Imagick::setFormat method. Eg load TIFF files, then use setFormat('pdf') on the Imagick object, then writeImagesFile('foo.pdf') or getImagesBlob().

There's no real way to make this work due to the two opposing forces at play:

1) Google App Engine Standard's sandbox limitations

2) Imagick apparently needing local file system access (at least temporarily) in order to work.

So if you can't change how Imagick work, then the only remaining solution is to not use GAE Standard. You can either use GAE Flex or Google Compute Engine. I don't know why Flex is not an option for you; you don't need to move the whole project over. You can just port this part over as a microservice on GAE Flex in the same project. The sole function of the service would be to process the image and get the thumbnail. You can then place the thumbnail in a Google Cloud Storage bucket for the rest of your app (in GAE Standard) to use.

to anyone facing the same problem , I solved mine by created a new Google Compute Engine , with PHP installed on it, I created a function to convert the pdf to image using Imagick and it returns the response as base64 stream.

Imagick does not work well with pdf files on GAE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM