Search code examples
phpimagemagickjquery-file-uploadblueimp

Imagemagick: can not make a static thumbnail from gif animation


I use jQuery-File-Upload scripts. The package contains a php script as well to process the files and to make thumbnails. Everything works fine but I need to create static thumbnails in case if users upload animated gif files.

Below is the function that creates thumbnails.

I have commented out everything for "Handle animated GIFs:" as I don't want animated thumbnails from animated gifs.

and added a line:

mail('my@email', 'file_path', "$file_path");

When uploading, I get emails with file path like: /var/www/images/image.gif

Then I added the following line to make thumbnails from the first frame

$file_path = $file_path[0];
mail('my@email', 'file_path', "$file_path");

Now I get empty email and thumbnails are not created. So, the variable $file_path[0] does not exist. Why?

How can I create static thumbnails from animated gifs?

protected function imagemagick_create_scaled_image($file_name, $version, $options) {

list($file_path, $new_file_path) =
$this->get_scaled_image_file_paths($file_name, $version);

$file_path = $file_path[0];

mail('my@email', 'file_path', "$file_path");

$resize = @$options['max_width']
.(empty($options['max_height']) ? '' : 'x'.$options['max_height']);
if (!$resize && empty($options['auto_orient'])) {
if ($file_path !== $new_file_path) {
return copy($file_path, $new_file_path);
}
return true;
}
$cmd = $this->options['convert_bin'];
if (!empty($this->options['convert_params'])) {
$cmd .= ' '.$this->options['convert_params'];
}
$cmd .= ' '.escapeshellarg($file_path);
if (!empty($options['auto_orient'])) {
$cmd .= ' -auto-orient';
}


//if ($resize) {
// Handle animated GIFs:
//$cmd .= ' -coalesce';
//if (empty($options['crop'])) {
//$cmd .= ' -resize '.escapeshellarg($resize.'>');
//} else {
//$cmd .= ' -resize '.escapeshellarg($resize.'^');
//$cmd .= ' -gravity center';
//$cmd .= ' -crop '.escapeshellarg($resize.'+0+0');
//}
// Make sure the page dimensions are correct (fixes offsets of animated GIFs):
//$cmd .= ' +repage';
//}


if (!empty($options['convert_params'])) {
$cmd .= ' '.$options['convert_params'];
}
$cmd .= ' '.escapeshellarg($new_file_path);

exec($cmd, $output, $error);
if ($error) {
error_log(implode('\n', $output));
return false;
}
return true;

}

Solution

  • So, the variable $file_path[0] does not exist. Why?

    I would assume that $file_path is a string, so $file_path[0] would access the character element in the string.

    php > $file_path = '/var/www/images/image.gif';
    php > print $file_path;    //=> '/var/www/images/image.gif'
    php > print $file_path[0]; //=> '/'
    

    How can I create static thumbnails from animated gifs?

    The [0] should be appended to the file path string value, not within the PHP language.

     $file_path .= '[0]';
     //=> '/var/www/images/image.gif[0]'