/**
  * Draw thumbnail result to resource.
  *
  * @param  mixed $input Destination image, a filename or an image string data or a GD image resource
  * @param  array $options Thumbnail options
  *
  * @return boolean TRUE on success or FALSE on failure.
  * @access public
  * @see    Thumbnail::output()
  */
 function render($input, $options = array())
 {
     // Create the source image
     $sourceImage = Thumbnail::imageCreate($input);
     if (!is_resource($sourceImage)) {
         user_error('Invalid image resource', E_USER_NOTICE);
         return false;
     }
     $sourceWidth = imagesx($sourceImage);
     $sourceHeight = imagesy($sourceImage);
     // Set default options
     static $defOptions = array('width' => 150, 'height' => 150, 'method' => THUMBNAIL_METHOD_SCALE_MAX, 'percent' => 0, 'halign' => THUMBNAIL_ALIGN_CENTER, 'valign' => THUMBNAIL_ALIGN_CENTER);
     foreach ($defOptions as $k => $v) {
         if (!isset($options[$k])) {
             $options[$k] = $v;
         }
     }
     // Estimate a rectangular portion of the source image and a size of the target image
     if ($options['method'] == THUMBNAIL_METHOD_CROP) {
         if ($options['percent']) {
             $W = floor($options['percent'] * $sourceWidth);
             $H = floor($options['percent'] * $sourceHeight);
         } else {
             $W = $options['width'];
             $H = $options['height'];
         }
         $width = $W;
         $height = $H;
         $Y = Thumbnail::_coord($options['valign'], $sourceHeight, $H);
         $X = Thumbnail::_coord($options['halign'], $sourceWidth, $W);
     } else {
         $X = 0;
         $Y = 0;
         $W = $sourceWidth;
         $H = $sourceHeight;
         if ($options['percent']) {
             $width = floor($options['percent'] * $W);
             $height = floor($options['percent'] * $H);
         } else {
             $width = $options['width'];
             $height = $options['height'];
             if ($options['method'] == THUMBNAIL_METHOD_SCALE_MIN) {
                 $Ww = $W / $width;
                 $Hh = $H / $height;
                 if ($Ww > $Hh) {
                     $W = floor($width * $Hh);
                     $X = Thumbnail::_coord($options['halign'], $sourceWidth, $W);
                 } else {
                     $H = floor($height * $Ww);
                     $Y = Thumbnail::_coord($options['valign'], $sourceHeight, $H);
                 }
             } else {
                 if ($H > $W) {
                     $width = floor($height / $H * $W);
                 } else {
                     $height = floor($width / $W * $H);
                 }
             }
         }
     }
     // Create the target image
     if (function_exists('imagecreatetruecolor')) {
         $targetImage = imagecreatetruecolor($width, $height);
     } else {
         $targetImage = imagecreate($width, $height);
     }
     if (!is_resource($targetImage)) {
         user_error('Cannot initialize new GD image stream', E_USER_NOTICE);
         return false;
     }
     // Copy the source image to the target image
     if ($options['method'] == THUMBNAIL_METHOD_CROP) {
         $result = imagecopy($targetImage, $sourceImage, 0, 0, $X, $Y, $W, $H);
     } elseif (function_exists('imagecopyresampled')) {
         $result = imagecopyresampled($targetImage, $sourceImage, 0, 0, $X, $Y, $width, $height, $W, $H);
     } else {
         $result = imagecopyresized($targetImage, $sourceImage, 0, 0, $X, $Y, $width, $height, $W, $H);
     }
     if (!$result) {
         user_error('Cannot resize image', E_USER_NOTICE);
         return false;
     }
     // Free a memory from the source image
     imagedestroy($sourceImage);
     // Save the resulting thumbnail
     return $targetImage;
 }
示例#2
0
 /**
  * Процесс создания копии изображения с заданными параметрами
  *
  * @param  mixed   $input    Имя файла, изображение-строка или GD-resource
  * @param  array   $options  Массив настроек
  *
  * @return resource|boolean TRUE или FALSE.
  * @access public
  * @see    Thumbnail::output()
  */
 public static function render($input, $options = array())
 {
     // Создаем ресурс
     $sourceImage = Thumbnail::imageCreate($input);
     if (!is_resource($sourceImage)) {
         throw new joosImageLibrariesException('Invalid image resource');
     }
     $sourceWidth = imagesx($sourceImage);
     $sourceHeight = imagesy($sourceImage);
     // Устанавливаем настройки по-умолчанию
     static $defOptions = array('width' => 150, 'height' => 150, 'method' => THUMBNAIL_METHOD_SCALE_MAX, 'percent' => 0, 'halign' => THUMBNAIL_ALIGN_CENTER, 'valign' => THUMBNAIL_ALIGN_CENTER, 'check_size' => 0, 'resize' => 1);
     foreach ($defOptions as $k => $v) {
         if (!isset($options[$k])) {
             $options[$k] = $v;
         }
     }
     $resize = 1;
     if ($options['check_size'] == 1 && $sourceWidth <= $options['width'] && $sourceHeight <= $options['height'] || $options['resize'] == 0) {
         $resize = 0;
     }
     if ($resize) {
         // Estimate a rectangular portion of the source image and a size of the target image
         if ($options['method'] == THUMBNAIL_METHOD_CROP) {
             if ($options['percent']) {
                 $W = floor($options['percent'] * $sourceWidth);
                 $H = floor($options['percent'] * $sourceHeight);
             } else {
                 $W = $options['width'];
                 $H = $options['height'];
             }
             $width = $W;
             $height = $H;
             $Y = Thumbnail::_coord($options['valign'], $sourceHeight, $H);
             $X = Thumbnail::_coord($options['halign'], $sourceWidth, $W);
         } else {
             $X = 0;
             $Y = 0;
             $W = $sourceWidth;
             $H = $sourceHeight;
             if ($options['percent']) {
                 $width = floor($options['percent'] * $W);
                 $height = floor($options['percent'] * $H);
             } else {
                 $width = $options['width'];
                 $height = $options['height'];
                 if ($options['method'] == THUMBNAIL_METHOD_SCALE_MIN) {
                     $Ww = $W / $width;
                     $Hh = $H / $height;
                     if ($Ww > $Hh) {
                         $W = floor($width * $Hh);
                         $X = Thumbnail::_coord($options['halign'], $sourceWidth, $W);
                     } else {
                         $H = floor($height * $Ww);
                         $Y = Thumbnail::_coord($options['valign'], $sourceHeight, $H);
                     }
                 } else {
                     if ($H > $W) {
                         $width = floor($height / $H * $W);
                     } else {
                         $height = floor($width / $W * $H);
                     }
                 }
             }
         }
     } else {
         $W = $sourceWidth;
         $H = $sourceHeight;
         $width = $sourceWidth;
         $height = $sourceHeight;
         $X = 0;
         $Y = 0;
     }
     // Create the target image
     if (function_exists('imagecreatetruecolor')) {
         $targetImage = imagecreatetruecolor($width, $height);
     } else {
         $targetImage = imagecreate($width, $height);
     }
     if (!is_resource($targetImage)) {
         throw new joosImageLibrariesException('Cannot initialize new GD image stream', E_USER_NOTICE);
         return false;
     }
     if ($options['method'] == THUMBNAIL_METHOD_CROP && isset($options['x']) && isset($options['y'])) {
         $X = $options['x'];
         $Y = $options['y'];
     }
     // Copy the source image to the target image
     if ($options['method'] == THUMBNAIL_METHOD_CROP) {
         $result = imagecopy($targetImage, $sourceImage, 0, 0, $X, $Y, $W, $H);
     } elseif (function_exists('imagecopyresampled')) {
         $result = imagecopyresampled($targetImage, $sourceImage, 0, 0, $X, $Y, $width, $height, $W, $H);
     } else {
         $result = imagecopyresized($targetImage, $sourceImage, 0, 0, $X, $Y, $width, $height, $W, $H);
     }
     if (!$result) {
         throw new joosImageLibrariesException('Cannot resize image');
     }
     // освобождаем память, выделенную для изображения
     imagedestroy($sourceImage);
     return $targetImage;
 }