Gregwar / Image

A PHP library to handle images
MIT License
1k stars 191 forks source link

Error during generate non-existing file #2

Closed piotrchludzinski closed 12 years ago

piotrchludzinski commented 12 years ago

This is part of my template.

{{ image('image_uploads/' ~ program.id ~ '.jpg').resize(118,88) }}

When file don't exist occurs error.

<img src="/ Fatal error: Method Gregwar\ImageBundle\ImageHandler::__toString() must not throw an exception in /home/pchludzinski/www/d3/Symfony/app/cache/dev/classes.php on line 7063

Gregwar commented 12 years ago

That's true, but what behaviour would you except ? The exception ca not be thrown farther because php won't let you throw it from the __toString method

Maybe a cleaner error message would do the trick

piotrchludzinski commented 12 years ago

If the twig gave the possibility to check weather file exists this problem would not exists.

something like that:

{% if file_exists('image_uploads/' ~ program.id ~ '.jpg') %}

{% endif %}

Gregwar commented 12 years ago

This logics should not be written in a template, you should write something like :

{% if program.has_picture %}
    {{ image(program.picture).resize(118,88) }}
{% endif %}
piotrchludzinski commented 12 years ago

Yes. You have right but what if this file won't be in this dictionary physically? I would get a 404 error and not fatal error.

Gregwar commented 12 years ago

It's up to you to write your own behaviour regarding the existence of the image, the Image class is just supposed to help you in handling and managing it

dao commented 11 years ago

How about:

public function __toString()
{
    try {
        $result = $this->guess();
    } catch (\Exception $e) {
        $result = '';
    }
    return $result;
}
Gregwar commented 11 years ago

I think i would prefer facing an ugly issue than having a problem sneaking in my app silently

An empty string will lead to broken images and unclear behaviour

If you want to handle the exception, call guess() yourself and handle it in your app, because this is exactly what you'd be doing if you'd call imagefromjpeg or so

However, we could add this as an option if the user gives a fallback "error" image, this could be interesting

dao commented 11 years ago

@Gregwar that would be great!

Gregwar commented 11 years ago

I've just commited a refactorization of this repository, we can now use a fallback image (see the README)

stevegroom commented 9 years ago

@Gregwar I'm just starting to use this class in a symfony2 project and generally it is a great help. I do have an issue with the fallback image though.

My image archive has about 100,000 images in it and there are some that are indeed missing from the filesystem - the fall back is working, however there are others that are corrupted jpegs. These generate warnings, but I'm not sure how that can be forced to show the broken image icon.

WARNING - unlink(/Users/steve/dev/sites/virtual/newgt/app/../public//images/cache/f/b/d/b/5/fbdb5c7df0b46993503174a19a184e0514feab77.jpeg): No such file or directory Context: {"type":2,"file":"/Users/steve/dev/sites/virtual/newgt/vendor/gregwar/cache/Gregwar/Cache/Cache.php","line":270,"scream":0} WARNING - imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file Context: {"type":2,"file":"/Users/steve/dev/sites/virtual/newgt/vendor/gregwar/image/Gregwar/Image/Adapter/GD.php","line":476,"scream":0} WARNING - imagecreatefromjpeg(): '../media/FIAGT/imagesorg/2/k2480w2000h1333ncj_worldgt_gall.jpg' is not a valid JPEG file Context: {"type":2,"file":"/Users/steve/dev/sites/virtual/newgt/vendor/gregwar/image/Gregwar/Image/Adapter/GD.php","line":476,"scream":0} EMERGENCY - Method Gregwar\ImageBundle\ImageHandler::toString() must not throw an exception Context: {"type":1,"file":"/Users/steve/dev/sites/virtual/newgt/app/cache/dev/classes.php","line":0} CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Method Gregwar\ImageBundle\ImageHandler::toString() must not throw an exception in /Users/steve/dev/sites/virtual/newgt/app/cache/dev/classes.php line 0" at /Users/steve/dev/sites/virtual/newgt/app/cache/dev/classes.php line 0 Context: {"exception":"Object(Symfony\Component\Debug\Exception\FatalErrorException)"}

regards Steve

Gregwar commented 9 years ago

Hello @stevegroom , indeed that's not good Do you have examples of such broken jpegs? So I could have a look if it could be detected

EnchanterIO commented 9 years ago

@Gregwar any news about this?

My page was just down for several hours because of this. It just trowed a white page.

I am checking if the source file exists before re sizing but then somehow in the middle it crashes.

When I switch the environment to dev I see at least this error:

[2015-02-25 14:10:02] scream.WARNING: unlink(/var/www/deploys/.com/prod/releases/20150217214148/app/../web/cached_images/3/8/d/3/5/38d35c6935221b7765b7eeda0f2b739b84d04edc.png): No such file or directory {"type":2,"file":"/var/www/deploys/.com/prod/shared/vendor/gregwar/cache/Gregwar/Cache/Cache.php","line":270,"scream":0} []

The directory exists but the file is not there! What would be the best solution for this??

EnchanterIO commented 9 years ago

Found a problem. From some reason I am not able to resize this image and it ends up in a fatal crush of the whole request: 54ed49c714f7d

The image description suits the problem :(

EnchanterIO commented 9 years ago

On the local the script runs without problems which means the size of 712kB is too much as it's a transparent image for the server.

What's the best solution now?

  1. increase some (which?) settings to handle it
  2. prevent transparent big .png?

Hmm let me please know asap @Gregwar

Thank you

Adambean commented 8 years ago

Out of interest, was the "throw_exception" configuration option expanded to work with toString()? I've set this configuration property to false and supplied a fallback image, but still get an exception if an image couldn't be found.

Error: Method Gregwar\ImageBundle\ImageHandler::__toString() must not throw an exception, caught Error: Call to a member function log() on null