From 7ff2859390563ef434a5c76c6ab2f564b67f7f1b Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Mon, 19 Aug 2013 12:11:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5?= =?UTF-8?q?=D1=80=D0=B6=D0=BA=D0=B8=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=B2,=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=8E?= =?UTF-8?q?=D1=89=D0=B8=D1=85=20=D0=BC=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=20=D0=B4=D0=BE=D0=BF=D1=83=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D0=BC=D1=8B=D0=B5=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80?= =?UTF-8?q?=D1=8B=20=D0=B8=D0=B7=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F.=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D1=8B?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B5=D1=81=D0=B0=D0=B9=D0=B7?= =?UTF-8?q?=D0=B8=D0=BD=D0=B3=D0=B0=20=D0=B8=D0=B7=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9=20(=D0=BD=D0=B0=20=D1=8D=D1=82?= =?UTF-8?q?=D0=BE=D1=82=20=D1=81=D1=87=D0=B5=D1=82=20=D0=B4=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=A2=D0=9E=D0=94=D0=9E?= =?UTF-8?q?).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Image.class.php | 35 ++++++++++++++++++++++++++--------- ImageVariant.class.php | 13 +++++++++++++ Upload.class.php | 27 ++++++++++++++++++++------- 3 files changed, 59 insertions(+), 16 deletions(-) diff --git a/Image.class.php b/Image.class.php index 306d753..e527a07 100644 --- a/Image.class.php +++ b/Image.class.php @@ -10,30 +10,47 @@ class Image extends File public $original_filename; public $variants = array(); + protected static $max_width; + + protected static $max_height; + protected static $sizes = array(); /** - * @param $class string + * @param $class string|StdClass * @throws ErrorException */ public static function checkSubClass($class) { - if (is_null($class) || $class == '') { - throw new ErrorException('Class not defined.'); - } - if (!class_exists($class)) { - throw new ErrorException('Class "' . $class . '" not exists.'); - } - if (get_parent_class($class) != 'Image') { + if (!self::getIsSubClass($class)) { 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; } + public static function getMaxWidth() + { + return static::$max_width; + } + + public static function getMaxHeight() + { + return static::$max_height; + } + public function getVariant($size) { return parent::getImageVariant($size); diff --git a/ImageVariant.class.php b/ImageVariant.class.php index d1f5023..9282b69 100644 --- a/ImageVariant.class.php +++ b/ImageVariant.class.php @@ -6,4 +6,17 @@ class ImageVariant extends File public $height; public $type; 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__); + } + } } \ No newline at end of file diff --git a/Upload.class.php b/Upload.class.php index 7f08ca8..14dc9e3 100644 --- a/Upload.class.php +++ b/Upload.class.php @@ -13,7 +13,7 @@ abstract class Upload } /** - * @param $class string + * @param $class string|Image * @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...']) * @param $force_create_variants bool * @param int $quality @@ -26,19 +26,26 @@ abstract class Upload /** * @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 = new $class; + $image = is_object($class) ? $class : new $class; $image->original_filename = $file['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) { $sizes = $image->getSizes(); foreach ($sizes as $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; } @@ -47,14 +54,20 @@ abstract class Upload * @param $greagwar_image GreagwarImage * @param null $width * @param null $height - * @param bool $crop TODO: разъяснить за что отвечает этот "магический" параметер * @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); 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(); $file_path = $greagwar_image->cacheFile($image->type, $quality, true);