0

I have a simple script:

<?php

$source = '../path/img.jpg';
$image = imagecreatefromjpeg($source);

?>

Finally I got:

Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted

But when I try do something like that:

<?php

$source = '../path/img.jpg';

$image = @imagecreatefromjpeg($source); // Mute Fatal Error
if ( !$image )
{
    die('Image file is too large');
}

?>

but it doesn't work. Fatal error is stopping my script. Is any way to change this situation? I would like to know when fatal error is exist (then i got FALSE as $image) and then I would like to stop script, but first I want to print an information about it.

Thanks.

Pavel K
  • 235
  • 1
  • 10
  • 2
    Have you considered checking the file size before trying to process it? – Don't Panic Oct 17 '17 at 19:08
  • Checking the file size doesn't do much good with (compressed) images. The thing is that a tiny (highly compressed) jpg may take up insane amounts of memory (primarily determined by the pixel-size of the image) when using the `imagecreate*` functions. – Mikk3lRo Oct 17 '17 at 19:12
  • @Mikk3lRo Hm, I didn't realize that. I don't work with images much. Makes sense, though. – Don't Panic Oct 17 '17 at 19:15
  • I think your only way out is to separate the image processing from the main logic - depending on where you're using this you might start a separate process using a shell-command, send an ajax-request or something. But somehow you need to let the processing script itself error out without affecting the main app. The main app can then check the output of the "external" processing script and report back to the user. I'd post an answer with a suggestion, but I think more details about your environment are needed to keep it below several pages ;) – Mikk3lRo Oct 17 '17 at 19:18
  • Where is the rest of the error message, typically it will tell you how many bytes it still needed to allocate. This would be helpful to know the full scope of the issue you face. – ArtisticPhoenix Oct 17 '17 at 19:47
  • 1
    Ok, posted as a new (self-answered) question: https://stackoverflow.com/questions/46798454/how-can-i-check-if-a-jpeg-will-fit-in-memory/46798455#46798455 – Mikk3lRo Oct 17 '17 at 20:14

1 Answers1

-1

Fatal errors can't be muted or catched. You need to raise amount of memory allowed for PHP script by using memory_limit setting in php.ini

Flying
  • 4,104
  • 2
  • 14
  • 24
  • That's a bandage. If the file he's trying to open is, say, 200MB, it will still fail – Machavity Oct 17 '17 at 19:07
  • 1
    See also [this answer](https://stackoverflow.com/a/18660082) – Machavity Oct 17 '17 at 19:12
  • @Machavity you're right, it was too fast answer to be posted here – Flying Oct 17 '17 at 19:16
  • 1
    perhaps its a bandaid, perhaps they have the default memory set. Sometime you just need more memory. Most the stuff I do I need at least 512M sometimes up to 3GB of memory, its not a bandaid to use 3GB when you process statistical reports on 100million rows of data from mysql, it just is what it is. ( my server has 56GB ram :-) That said I would never set it at run time to `-1` it's reasonable to increase it when there is a good reason to do so... not just because. – ArtisticPhoenix Oct 17 '17 at 19:38
  • They are using 135mb, or that's when PHP bombed, that's not really a whole lot. Now depending what they need ( and their servers resources ) increasing this to 256M or even 512M would not be out of the question. ie. it's reasonable. – ArtisticPhoenix Oct 17 '17 at 19:44
  • It is, that's why I've posted this answer. However it is, of course, not a general solution as @Machavity pointed out. Also I would need to mention that correct solution should involve e.g. `getimagesize()` usage to measure possible memory usage for image creation. – Flying Oct 17 '17 at 19:49
  • it's a sticky wicket, lol, that's what I call memory issues. We just added a horizontal search server to our process, went through a few iterations to up the memory finally settled on 512M. ( we run 185k searches a minute ) To me its a process that has to be undertaken with definitive goals and limits – ArtisticPhoenix Oct 17 '17 at 19:52