Рефакторинг. Добавлении поддержки параметров, задающих максимально допустимые размеры изображения. Изменены параметры для ресайзинга изображений (на этот счет добавлено ТОДО).

This commit is contained in:
Alexander Demidov
2013-08-19 12:11:40 +04:00
parent e97f584ebc
commit 7ff2859390
3 changed files with 59 additions and 16 deletions

View File

@ -10,30 +10,47 @@ class Image extends File
public $original_filename; public $original_filename;
public $variants = array(); public $variants = array();
protected static $max_width;
protected static $max_height;
protected static $sizes = array(); protected static $sizes = array();
/** /**
* @param $class string * @param $class string|StdClass
* @throws ErrorException * @throws ErrorException
*/ */
public static function checkSubClass($class) public static function checkSubClass($class)
{ {
if (is_null($class) || $class == '') { if (!self::getIsSubClass($class)) {
throw new ErrorException('Class not defined.');
}
if (!class_exists($class)) {
throw new ErrorException('Class "' . $class . '" not exists.');
}
if (get_parent_class($class) != 'Image') {
throw new ErrorException('Class "' . $class . '" mast be extend of "Image".'); throw new ErrorException('Class "' . $class . '" mast be extend of "Image".');
} }
} }
public function getSizes() /**
* @param $class string|StdClass
* @return bool
*/
public static function getIsSubClass($class)
{
return (get_parent_class($class) == __CLASS__);
}
public static function getSizes()
{ {
return static::$sizes; return static::$sizes;
} }
public static function getMaxWidth()
{
return static::$max_width;
}
public static function getMaxHeight()
{
return static::$max_height;
}
public function getVariant($size) public function getVariant($size)
{ {
return parent::getImageVariant($size); return parent::getImageVariant($size);

View File

@ -6,4 +6,17 @@ class ImageVariant extends File
public $height; public $height;
public $type; public $type;
public $size; public $size;
/**
* @param $class string|StdClass
* @return mixed
*/
public static function getIsClass($class)
{
if (is_object($class)) {
return (get_class($class) == __CLASS__);
} else {
return ($class == __CLASS__);
}
}
} }

View File

@ -13,7 +13,7 @@ abstract class Upload
} }
/** /**
* @param $class string * @param $class string|Image
* @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...']) * @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
* @param $force_create_variants bool * @param $force_create_variants bool
* @param int $quality * @param int $quality
@ -26,19 +26,26 @@ abstract class Upload
/** /**
* @var $image Image * @var $image Image
*/ */
if (is_null($class) || $class == '') {
throw new ErrorException('Class not defined.');
}
if (!is_object($class) && !class_exists($class)) {
throw new ErrorException('Class "' . $class . '" not exists.');
}
Image::checkSubClass($class); Image::checkSubClass($class);
$image = new $class; $image = is_object($class) ? $class : new $class;
$image->original_filename = $file['name']; $image->original_filename = $file['name'];
$greagwar_image = self::getGreagwarImage($file['tmp_name']); $greagwar_image = self::getGreagwarImage($file['tmp_name']);
self::saveImage($image, $greagwar_image, null, null, true, $quality); self::saveImage($image, $greagwar_image, $class::getMaxWidth(), $class::getMaxHeight(), $quality);
if ($force_create_variants) { if ($force_create_variants) {
$sizes = $image->getSizes(); $sizes = $image->getSizes();
foreach ($sizes as $size) { foreach ($sizes as $size) {
$size_parts = explode('x', $size); $size_parts = explode('x', $size);
self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null, true, $quality); self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null, $quality);
} }
} }
// В случае, если был передан объект, возвращение результата не имеет значения
return $image; return $image;
} }
@ -47,14 +54,20 @@ abstract class Upload
* @param $greagwar_image GreagwarImage * @param $greagwar_image GreagwarImage
* @param null $width * @param null $width
* @param null $height * @param null $height
* @param bool $crop TODO: разъяснить за что отвечает этот "магический" параметер
* @param int $quality * @param int $quality
* TODO: Сделать возможность передавать параметры для метода _resize()
*/ */
public static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true, $quality = 87) public static function saveImage($image, $greagwar_image, $width = null, $height = null, $quality = 87)
{ {
if (!ImageVariant::getIsClass($image) && is_null($width) && is_null($height)) {
if (Image::getIsSubClass($image)) {
$width = $image::getMaxWidth();
$height = $image::getMaxHeight();
}
}
$greagwar_image = clone($greagwar_image); $greagwar_image = clone($greagwar_image);
if ($width || $height) { if ($width || $height) {
$greagwar_image->resize($width, $height, 0xffffff, false, false, $crop); $greagwar_image->resize($width, $height, 0xffffff, $force = true, $rescale = true, $crop = false);
} }
$image->type = $greagwar_image->guessType(); $image->type = $greagwar_image->guessType();
$file_path = $greagwar_image->cacheFile($image->type, $quality, true); $file_path = $greagwar_image->cacheFile($image->type, $quality, true);