September 18, 2012

How to ensure JPEG image is valid and can be handled by PHP?

Question by Nyxynyx

I have this JPEG that has been giving problems to the function imagesx($this->image) from the Resizer library that I am using. I am able to view the image using the browser, and when attempting to resize, I get the error:

imagesx() expects parameter 1 to be resource, boolean given

It is OK for me to not handle this file if it is going to throw an error. How can I use PHP to check whether this image can be handled properly by PHP’s image functions?

Code that calls the Library

// Download the photo
$img_content = file_get_contents($url);
if($img_content !== FALSE) {
    file_put_contents($img_documentroot . $img_subpath . $img_filename . '_tmp.jpg',

echo $url . '<br>';
echo $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg<br>';

// Resize photo
Resizer::open( $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg' )
->resize(300, 300, 'landscape' )
->save($img_documentroot . $img_subpath . $img_filename . '.jpg' , 90 );

// Thumbnail photo
Resizer::open( $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg' )
->resize(100, 100, 'crop' )
->save($img_documentroot . $img_subpath . $img_filename . '.jpg' , 90 );


I also echoed out the full path of the image being resized.
resource(127) of type (gd)
resource(130) of type (gd)
resource(163) of type (gd)
resource(166) of type (gd)


This is the code snippet that is causing the library to return a false value.

private function open_image( $file )

    // If $file isn't an array, we'll turn it into one
    if ( !is_array($file) ) {
        $file = array(
            'type'      => File::mime( strtolower(File::extension($file)) ),
            'tmp_name'  => $file

    $mime = $file['type'];
    $file_path = $file['tmp_name'];

    switch ( $mime )
        case 'image/pjpeg': // IE6
        case File::mime('jpg'): $img = @imagecreatefromjpeg( $file_path );  break;
        case File::mime('gif'): $img = @imagecreatefromgif( $file_path );   break;
        case File::mime('png'): $img = @imagecreatefrompng( $file_path );   break;
        default:                $img = false;                               break;

    return $img;

Answer by Starx

Checking for the mime type is a very good method to ensure the validity of JPEG image. Here is how you can do this.

$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type
$type = finfo_file($finfo, $filename) . "n";  
if($type == "image/jpeg") { 
     //Valid JPEG Image

According to your errors, you are sending boolean value instead of image resource.

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!