示例#1
0
 /**
  * Constructor for the cache entry.
  *
  * @param FileCache $cache The cache object.
  * @param string $destination The file to create.
  * @param array $userdata An array with the path of the original image and the options to use
  * to create the thumbnail.
  * @throws Exception
  */
 public function get_construct(FileCache $cache, $destination, $userdata)
 {
     list($path, $options) = $userdata;
     $callback = null;
     if ($options['background'] != 'transparent') {
         self::$background = self::decode_background($options['background']);
         $callback = __CLASS__ . '::fill_callback';
     }
     $image = Image::load($path, $info);
     if (!$image) {
         throw new Exception('Unable to load image from file %path', array('%path' => $path));
     }
     #
     # resize image
     #
     $w = $options['width'];
     $h = $options['height'];
     list($ow, $oh) = $info;
     $method = $options['method'];
     if ($options['no-upscale']) {
         if ($method == Image::RESIZE_SURFACE) {
             if ($w * $h > $ow * $oh) {
                 $w = $ow;
                 $h = $oh;
             }
         } else {
             if ($w > $ow) {
                 $w = $ow;
             }
             if ($h > $oh) {
                 $h = $ow;
             }
         }
     }
     $image = Image::resize($image, $w, $h, $method, $callback);
     if (!$image) {
         throw new Exception('Unable to resize image for file %path with options: !options', array('%path' => $path, '!options' => $options));
     }
     #
     # apply filters
     #
     $filter = $options['filter'];
     if ($filter) {
         $this->apply_filter($image, $filter);
     }
     #
     # apply the overlay
     #
     if ($options['overlay']) {
         $overlay_file = \ICanBoogie\DOCUMENT_ROOT . $options['overlay'];
         list($o_w, $o_h) = getimagesize($overlay_file);
         $overlay_source = imagecreatefrompng($overlay_file);
         imagecopyresampled($image, $overlay_source, 0, 0, 0, 0, $w, $h, $o_w, $o_h);
     }
     #
     # interlace
     #
     if (!$options['no-interlace']) {
         imageinterlace($image, true);
     }
     #
     # choose export format
     #
     $format = $options['format'];
     static $functions = array('gif' => 'imagegif', 'jpeg' => 'imagejpeg', 'png' => 'imagepng');
     $function = $functions[$format];
     $args = array($image, $destination);
     if ($format == 'jpeg') {
         #
         # add quality option for the 'jpeg' format
         #
         $args[] = $options['quality'];
     } else {
         if ($format == 'png' && !$callback) {
             #
             # If there is no background callback defined, the image is defined as transparent in
             # order to obtain a transparent thumbnail when the resulting image is centered.
             #
             imagealphablending($image, false);
             imagesavealpha($image, true);
         }
     }
     $rc = call_user_func_array($function, $args);
     imagedestroy($image);
     if (!$rc) {
         throw new Exception('Unable to save thumbnail');
     }
     return $destination;
 }