ImageMagick drupal 7 module fix of getimagesize() for WebP

The clean fix

The module has:


 
function image_imagemagick_get_info(stdClass $image) {
$details = FALSE;
$data = getimagesize(drupal_realpath($image->source));
 
if (isset($data) && is_array($data)) {
...
}
 
return $details;
}
 

We should add a WebP fallback using the GD functionality that we just proved works.

1. Back up the module


 
cp sites/all/modules/imagemagick/imagemagick.module \
sites/all/modules/imagemagick/imagemagick.module.bak
 

2. Edit this function


 
nano sites/all/modules/imagemagick/imagemagick.module
 

Replace the existing

image_imagemagick_get_info()

with:


 
function image_imagemagick_get_info(stdClass $image) {
$details = FALSE;
$path = drupal_realpath($image->source);
 
$data = getimagesize($path);
 
if (isset($data) && is_array($data)) {
$extensions = array(
'1' => 'gif',
'2' => 'jpg',
'3' => 'png',
'18' => 'webp',
);
 
$extension = isset($extensions[$data[2]]) ? $extensions[$data[2]] : '';
 
$details = array(
'width' => $data[0],
'height' => $data[1],
'extension' => $extension,
'mime_type' => $data['mime'],
);
}
elseif (function_exists('imagecreatefromwebp')) {
// PHP 5.6/GD can decode WebP even when getimagesize()
// cannot identify the file.
$resource = @imagecreatefromwebp($path);
 
if ($resource !== FALSE) {
$details = array(
'width' => imagesx($resource),
'height' => imagesy($resource),
'extension' => 'webp',
'mime_type' => 'image/webp',
);
 
imagedestroy($resource);
}
}
 
return $details;
}
 

3. Syntax-check it


 
php5.6 -l sites/all/modules/imagemagick/imagemagick.module
 

You want:


No syntax errors detected