'ImagickException' with message 'unable to open file - php

'ImagickException' with message 'unable to open file /data/web/myweb.com/sub/7/file:/data/web/myweb.com/sub/7/sites/default/files/logo.png'
Hi,
I got this message when I want to crete PDF file from the webpage, that png files. It throws error only when content of PDF contains some PNG file. When I remove all png files, that should be in pdf, everything is correct.
When I look in php info, I see png is supported format for Imagick. When I tried to find solution on google, it returned many of sites with the same error but almost no solution. the only solution I have found is to install another library to server. But I would prefer another solution if there is any.
thnaks for any advice
Tomas

ImageMagick can indeed take png files and convert it to pdf. Are you sure the file is in fact on the server at the correct place.
it feels like the inout command to ImageMagick may be incorrect. The path below looks weird.
data/web/myweb.com/sub/7/file:/data/web/myweb.com/sub/7/sites/default/files/logo.png
^^^^^
Is this expected?
I can tell you more if you tell me the website. Otherwise, I would say its a bug in the website php/python script. It doesn't look like an ImageMagick issue to me.

The ImagickException error message indicates that Imagick (PHP class) is choking on the 'file://' prefix, which is a streamwrapper, fairly recent PHP addition. Imagick documentation is not clear on the use of stremwrappers, and it is most certain that Imagick does not support them. When it sees no '/' in the first byte of the file name, it assumes the file name is relative and pre-pends current directory to it, creating the monster string you pasted in the question.
Solution is to trim the 'file://' prefix on the file name you are sending to the Imagick. File name should be either absolute (start with /) or relative.
if (strpos('file://', $filename) === 0) {
$filename = substr($filename, 7);
}

Related

Error converting .docx file (with .emf image background) to PDF

A Laravel-based application is converting documents (.doc, .docx, .pdf, .png, .otd, html, etc) to PDF so that they can all be merged together into a master PDF document. It is using a combination of plugins like PHPWord and DOMPDF Wrapper to do the file loading and creation. Every once and awhile, the process encounters an error due to a Word file.
ERROR: PhpOffice\PhpWord\Exception\InvalidImageException: Invalid
image: zip:// ... #word/media/image2.emf
The error is caused by an image background within the document that acts like a watermark. The PHPWord part that errors out is the PhpOffice\PhpWord\Element\Image->checkImage() method, but happens when the file is trying to be loaded.
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
$pdfWord = IOFactory::load(storage_path() . '/app/uploads/randomfile.docx', 'Word2007');
How can the application convert a Word document, with an EMF image embedded, to a PDF?
For more code/info on how to recreate the error, a few issues exist in the Github PHPWord library.
Support EMF image #1480
Read docx error when contains image from remote url #1173
The environment-related information:
Server: Windows / IIS
PHP: 7.2.11
Laravel: 5.7.15
PHPWord: 0.15.0
EDIT:
I also tried to come at this from a different angle, to no avail. I tried using PHP's ZipArchive to unzip the docx file, remove the emf image from the document (ZipArchive::deleteName()), remove the reference to the emf image in the [Content_Types].xml (ZipArchive::getFromName()), then zip the docx file back up but that did not work. I can open the new docx file and see that the image is gone, but the PHPWord error still persists in the application.
It looks like PHPWord has a feature request open to solve this issue.
https://github.com/PHPOffice/PHPWord/issues/1480
I think you're on the right path with the file alteration, there is probably a reference to the image you are missing somewhere that PHPWord is still trying to access.
I would unzip the file on your local drive and grep (search the contents of the exploded file) the directory for the file you are looking for. This will show you where else you may need to remove it from being referenced in the file.

PHP - Grab PDF with URL that does not have the .pdf extension

I'm using Filepicker.io to upload PDFs to my application. I have all those URLs and now I am trying to merge some of those PDFs using the PDF Tool Kit PHP library. It was not working for me so I ran some tests using the "file_exists" on PHP and it kept returning false.
I think this has to do with the fact that the URL does not have a ".pdf" extension at the end. This is what they look like: "https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1"
Does anyone know how I can pull the PDF using PHP in order to merge those files using the PDF Toolkit Library?
Thanks!
Alain F.
file_exists doesn't work with URLs, only with local files. Instead download the file to the temp dir using the copy command.
If the file can't be downloaded, the copy command will return false.
$exists = copy('https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1', '/tmp/example.pdf');
if (!$exists) throw new Exception("PDF could not be downloaded");
Use the downloaded file in the PDF Tool Kit.
EDIT: This does not solve this particular problem but does address the theory that it didn't work because "the URL does not have a ".pdf" extension at the end."
You can add things to the end of the filepicker URL with a trailing +
The following urls are equivalent:
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1
https://www.filepicker.io/api/file/LCvbgpqEQLGwt8bfnqc1+name.pdf

read microsoft excel, word and powerpoint info using PHP

i want my PHPapp to read a file and tell me if it's a word, excel or powerpoint file. my first approach was to read the extension of the file. But microsoft has different file extension names, and i was also wondering about files uploaded by a mac (with no filename extension)
So, maybe the answer is using mimetypes. Do you know a better approach for this?
Yes, trying to determine the MimeType is your best bet short of just try/catching to load the files with PHPWord, PHPExcel and PHPowerpoint directly to see if they throw an exception (Mark Baker correct me please if they dont throw exceptions).
See my answer to
PHP how can i check if a file is mp3 or image file?
for various ways to detect the MimeType.
You can find a number of possible MimeTypes for Office documents at
http://filext.com/faq/office_mime_types.php and
http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/87b9cd73-a41b-4fd0-94c7-dfe53e92947e

getimagesize function does not working

Images in my website is located in other server. I tried to get the size of an image using getimagesize of php and it is not working. It gives an error 'getimagesize is not seekable'. I tried this same function from other server(images are in same server and I change the location of the file, which is using getimagesize function) and it worked. Can anyone solve this?
From the manual:
filename
This parameter specifies the file you wish to retrieve information about. It can reference a local file or (configuration permitting) a remote file using one of the supported streams.
The settings they're talking about (for the HTTP wrappers) is for the most part having allow_url_fopen enabled or disabled.
The error is probably could not make seekable and could mean that PHP / GD cannot recognize the image file as such.
Have you tried with different images and different image formats (GIF, JPG, PNG) ?
Maybe GIF or PNG is not supported by your PHP version.
maybe the image path is wrong
try using this instead:
dirname($_SERVER['SCRIPT_FILENAME'])
it will return the parent directory's path,
example:
getimagesize(dirname($_SERVER['SCRIPT_FILENAME']).'/images/sampleimage.jpg');

Problem with imagecreatefrompng (PHP)

in one of my PHP projects, imagecreatefrompng complains, that the image "is not a valid PNG file".
Warning: imagecreatefrompng() [function.imagecreatefrompng]:
'/.../...png' is not a valid PNG file in /.../...php on line ...
I'm pretty sure, the file is valid, because I can open it in any program. So I was wondering what else could cause this warning.
Does anybody know the details behind imagecreatefrompng or has had a similar problem before?
Thanks for your help,
Timo
Is it a PNG image? Run file against it, or try the fileinfo functions.
Check this mime type of file you are going to upload by using below function:
mime_content_type($_FILES['product_img']['tmp_name'])
Image editors can often recognize file type by its contents, php might just try to use the file based on its mime-type. If it don't match then there's an error.

Categories