/**
  * Uses WP_Image_Editor_Imagick to generate thumbnails.
  *
  * @param int $ID The attachment ID to retrieve thumbnail from.
  * @param int $pg The page to get the thumbnail of.
  *
  * @return bool|string  False on failure, URL to thumb on success.
  */
 public function getThumbnail($ID, $pg = 1)
 {
     $doc_path = get_attached_file($ID);
     $img = new DG_Image_Editor_Imagick($doc_path, $pg - 1);
     $err = $img->load();
     if (is_wp_error($err)) {
         DG_Logger::writeLog(DG_LogLevel::Error, __('Failed to open file in Imagick: ', 'document-gallery') . $err->get_error_message());
         return false;
     }
     $temp_file = DG_Util::getTempFile();
     $err = $img->save($temp_file, 'image/png');
     if (is_wp_error($err)) {
         DG_Logger::writeLog(DG_LogLevel::Error, __('Failed to save image in Imagick: ', 'document-gallery') . $err->get_error_message());
         return false;
     }
     return $temp_file;
 }
Example #2
0
 /**
  * @filter dg_thumbers Allows developers to filter the Thumbers used
  * for specific filetypes. Index is the regex to match file extensions
  * supported and the value is anything that can be accepted by call_user_func().
  * The function must take two parameters, 1st is the int ID of the attachment
  * to get a thumbnail for, 2nd is the page to take a thumbnail of
  * (may not be relevant for some filetypes).
  *
  * @return array
  */
 private static function getThumbers()
 {
     static $thumbers = null;
     if (is_null($thumbers)) {
         $options = self::getOptions();
         $active = $options['active'];
         $thumbers = array();
         // Audio/Video embedded images
         if ($active['av']) {
             $exts = implode('|', self::getAudioVideoExts());
             $thumbers[$exts] = array(__CLASS__, 'getAudioVideoThumbnail');
         }
         // Ghostscript
         if ($active['gs'] && self::isGhostscriptAvailable()) {
             $exts = implode('|', self::getGhostscriptExts());
             $thumbers[$exts] = array(__CLASS__, 'getGhostscriptThumbnail');
         }
         // Imagick
         if ($active['imagick'] && self::isImagickAvailable()) {
             include_once DG_PATH . 'inc/class-image-editor-imagick.php';
             if ($exts = DG_Image_Editor_Imagick::query_formats()) {
                 $exts = implode('|', $exts);
                 $thumbers[$exts] = array(__CLASS__, 'getImagickThumbnail');
             }
         }
         // allow users to filter thumbers used
         $thumbers = apply_filters('dg_thumbers', $thumbers);
         // strip out anything that can't be called
         $thumbers = array_filter($thumbers, 'is_callable');
         // log which thumbers are being used
         if (DG_Logger::logEnabled()) {
             if (count($thumbers) > 0) {
                 $entry = __('Thumbnail Generators: ', 'document-gallery');
                 foreach ($thumbers as $k => $v) {
                     $thumber = is_array($v) ? implode('::', $v) : print_r($v, true);
                     // TODO: The following works for all internal regexes, but may have unpredictable
                     // results if developer adds additional thumbnail generators using different regexes
                     $filetypes = str_replace('|', ', ', $k);
                     $entry .= PHP_EOL . "{$thumber}: {$filetypes}";
                 }
             } else {
                 $entry = __('No thumbnail generators enabled.', 'document-gallery');
             }
             DG_Logger::writeLog(DG_LogLevel::Detail, $entry);
         }
     }
     return $thumbers;
 }