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',
     $img_content);
}

echo $url . '<br>';
echo $img_documentroot . $img_subpath . $img_filename . '_tmp.jpg<br>';
ob_flush();
flush();

// 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 );

Output

I also echoed out the full path of the image being resized.

http://www.ApartmentsInAllstonMA.com/Images/Apts/132847_kn1.jpg
/home/photos/public_html/2012/0917/2516539_7_tmp.jpg
resource(127) of type (gd)
resource(130) of type (gd)
http://www.ApartmentsInMedford.com/Images/Apts/132847_lv2.jpg
/home/photos/public_html/2012/0917/2516539_11_tmp.jpg
resource(163) of type (gd)
resource(166) of type (gd)
http://www.AllstonApartmentX.com/images/agents/61.jpg
/home/photos/public_html/2012/0917/2516539_12_tmp.jpg
bool(false)

UPDATE

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
}
finfo_close($finfo);

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

...

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