I'm working on a script that has to work on a large set of images and I need a graceful way to handle corrupt/truncated images in the set.
Currently my script works for all valid images but crashes when it runs into a truncated file. It must've sneaked into the folders but I want to handle this case nevertheless.
Its a *.png.tmp file and when feeding it to imagecreatefrompng(), it causes a fatal error and stops my script.
Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data...
It is recognized as PNG by both getimagesize() and exif_imagetype().
Since I can't handle it as an exception, is there a way to check if the file is valid without crashing my script and without relying on extensions in the filename?
I can live with skipping the image, but I don't know how to recognize it as a problem image before hitting the fatal error.
Did you try to suppress error by using # operator, and check if image is actually created?
foreach ($imagePaths as $imagePath)
{
$image = #imagecreatefrompng($imagePath);
if (!$image)
{
//maybe delete bad image?
unlink($imagePath);
continue;//do nothing in this iteration anymore
}
//do your magic here
}
Related
On my site, i allow users to download videos, images (in chunks though), however, i want to make it in such a way that if a url parameter is not met, my download script should halt and cause download failed.
I did this:
<?php if(!$condition){
trigger_error('Fatal error occured', E_USER_ERROR);
}?>
The problem is that, the file downloaded( although, currupted), when i checked into the downloaded file in a notepad, i saw my fatal error ("Fatal error occured").
How do i stop the file from downloading at all?
Just put
exit();
inside the if, after you've triggered the error. This will halt the script execution so that any headers, content etc associated with the file does not get output.
Optionally you could also set an appropriate HTTP status code for the response.
Converting a PDF to an image using Gmagick in PHP renders a very poor quality image.
The solution in Imagick was to call setResolution(x,y) before loading the PDF file. This would change the -density option.
There is no setResolution(x,y) in Gmagick, and unfortunately, calling setimageresolution(x,y) just throws an error:
PHP Fatal error: Uncaught exception 'GmagickException' with message 'Can not process empty Gmagick object'
Calling setimageresolution(x,y) after loading the PDF has no effect, and I can't find a way to set the -density option before loading the file.
EDIT: I would be happy for a way to set the default density system-wide. I do have root access.
I bumped into a similar problem and solved it with the following code:
$image = new \Gmagick();
$image->setresolution(300, 300);
$image->readimage('sample.pdf[0]');
Note that the setresolution method is not documented in PHP anywhere, but it seems to work – at least for me.
I want to convert the PDF to JPG by using imagick, but after I press submit it keeps showing a fatal error: uncaught imagick error. Do you guys know if this is code problem or an imagick extension problem?
Your $_FILES['fileToUpload']['type'] stores the MIME type and you are assigning it to the variable $pdf_file and you are trying to read this with imagick. This can't work. You could var_dump($_FILES) and see if you can find the actual file in there.
Every now and then an image resizing script on our site will fail with the following error:
PHP Fatal error: Class '\xa0L\xdaor\x7f' not found ... on line 4
The actual line 4 of the script in question is:
$photo = new Photo($photo_id);
I have no idea where the hex code \xa0L\xdaor\x7f in the Error log comes from. The script will run fine and it runs relatively frequently for a day or two, then it starts failing every time, with that error.
If I run opcache_reset(), the errors stop.
Anyone have any idea what might be causing this issue?
UPDATE: I got no response - so I've simply excluded this file from the opcode cache using opcache.blacklist_filename.
Sometimes my whole PHP response was a bunch of hex codes like this.
I had that when opcache.fast_shutdown="1" was set.
i built an upload page that lets me upload images and i came onto this error when uploaded a file with an extension of jpg.
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]:
gd-jpeg, libjpeg: recoverable error: Premature end of JPEG
after playing with it i noticed that not even photoshop can open it. the file is corrupt. I then tried to upload it into facebook and facebook was able to accept it. it is weird how imaagecreatefromjpeg() in php wont accept this jpeg but facebook does. wonder what their using...
but is it possible to force this image as a jpeg?
You'll notice that this is a recoverable error. You can ignore this error and GD should continue by setting:
ini_set('gd.jpeg_ignore_warning', 1);
at the top of your script.
http://php.net/manual/en/image.configuration.php
As an aside, you should always ensure that scripts dealing with image manipulation have a high enough memory limit.