/**
  * 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;
 }
 /**
  * Get thumbnail for document with given ID using Ghostscript. Imagick could
  * also handle this, but is *much* slower.
  *
  * @param int $ID The attachment ID to retrieve thumbnail from.
  * @param int $pg The page number to make thumbnail of -- index starts at 1.
  *
  * @return bool|string  False on failure, URL to thumb on success.
  */
 public function getThumbnail($ID, $pg = 1)
 {
     static $gs = null;
     if (is_null($gs)) {
         $options = DG_Thumber::getOptions();
         $gs = $options['gs'];
         if (false !== $gs) {
             $gs = escapeshellarg($gs) . ' -sDEVICE=png16m -dFirstPage=%1$d' . ' -dLastPage=%1$d -dBATCH -dNOPAUSE -dPDFFitPage -sOutputFile=%2$s %3$s 2>&1';
         }
     }
     if (false === $gs) {
         return false;
     }
     $doc_path = get_attached_file($ID);
     $temp_path = DG_Util::getTempFile();
     exec(sprintf($gs, $pg, $temp_path, $doc_path), $out, $ret);
     if ($ret != 0) {
         DG_Logger::writeLog(DG_LogLevel::Error, __('Ghostscript failed: ', 'document-gallery') . print_r($out));
         @unlink($temp_path);
         return false;
     }
     return $temp_path;
 }
 /**
  * Uses wp_read_video_metadata() and wp_read_audio_metadata() to retrieve
  * an embedded image to use as a thumbnail.
  *
  * @param string $ID The attachment ID to retrieve thumbnail from.
  * @param int $pg Unused.
  *
  * @return bool|string  False on failure, URL to thumb on success.
  */
 public function getThumbnail($ID, $pg = 1)
 {
     include_once DG_WPADMIN_PATH . 'includes/media.php';
     $doc_path = get_attached_file($ID);
     $mime_type = get_post_mime_type($ID);
     if (DG_Util::startsWith($mime_type, 'video/')) {
         $metadata = wp_read_video_metadata($doc_path);
     } elseif (DG_Util::startsWith($mime_type, 'audio/')) {
         $metadata = wp_read_audio_metadata($doc_path);
     }
     // unsupported mime type || no embedded image present
     if (!isset($metadata) || empty($metadata['image']['data'])) {
         return false;
     }
     $ext = 'jpg';
     switch ($metadata['image']['mime']) {
         case 'image/gif':
             $ext = 'gif';
             break;
         case 'image/png':
             $ext = 'png';
             break;
     }
     $temp_file = DG_Util::getTempFile($ext);
     if (!($fp = @fopen($temp_file, 'wb'))) {
         DG_Logger::writeLog(DG_LogLevel::Error, __('Could not open file: ', 'document-gallery') . $temp_file);
         return false;
     }
     if (!@fwrite($fp, $metadata['image']['data'])) {
         DG_Logger::writeLog(DG_LogLevel::Error, __('Could not write file: ', 'document-gallery') . $temp_file);
         fclose($fp);
         return false;
     }
     fclose($fp);
     return $temp_file;
 }
 /**
  * Processes the POST request, generating a ThumberResponse, validating, and passing the result to $callback.
  * If not using client.php as the webhook, whoever receives webhook response should first invoke this method to
  * validate response.
  */
 public function receiveThumbResponse()
 {
     $resp = parent::receiveThumbResponse();
     if (is_null($resp)) {
         return;
     }
     $nonce = $resp->getNonce();
     $split = explode(DG_ThumberCoThumber::NonceSeparator, $nonce);
     if ($resp->getSuccess() && count($split) === 2) {
         $ID = absint($split[0]);
         $tmpfile = DG_Util::getTempFile();
         file_put_contents($tmpfile, $resp->getDecodedData());
         DG_Thumber::setThumbnail($ID, $tmpfile, array(__CLASS__, 'getThumberThumbnail'));
         DG_Logger::writeLog(DG_LogLevel::Detail, "Received thumbnail from Thumber for attachment #{$split[0]}.");
     } else {
         $ID = count($split) > 0 ? $split[0] : $nonce;
         DG_Logger::writeLog(DG_LogLevel::Warning, "Thumber was unable to process attachment #{$ID}: " . $resp->getError());
     }
 }