April 16, 2012

PHP detect if ImageCreateFromString fail

Question by Jerome Ansia

I would like to be able to detect when i launch this function if it’s fail or not due to the memory size limit

ImageCreateFromString();

http://php.net/manual/en/function.imagecreatefromstring.php

Answer by rauchmelder

In the PHP-Manual which you’ve linked there is the solution already written:

Return Values

An image resource will be returned on success. FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.

Answer by Starx

If you had read the manual you linked in your question, there is an answer already.

An image resource will be returned on success. FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.

There is an example for that

$im = imagecreatefromstring($data);
if ($im !== false) {
    // OK
}
else {
    echo 'An error occurred.';
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!