From da77be4fdd8665d58b3002db04de310f293181db Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 24 May 2013 13:27:47 +0400 Subject: [PATCH] Add Image, Upload, UploadHelper. --- Image.class.php | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ Upload.class.php | 57 +++++++++++++++++++++++++++++ UploadHelper.class.php | 25 +++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 Image.class.php create mode 100644 Upload.class.php create mode 100644 UploadHelper.class.php diff --git a/Image.class.php b/Image.class.php new file mode 100644 index 0000000..6a75a1a --- /dev/null +++ b/Image.class.php @@ -0,0 +1,98 @@ + $attribute_value) { + if (property_exists(__CLASS__, $attribute_name)) { + $instance->{$attribute_name} = $attribute_value; + } + } + $instance->_is_empty = false; + return $instance; + } + + /** + * @return string + */ + public function getWebName() + { + return $this->path . '/' . $this->filename; + } + + /** + * @param $size string + * @return Image + */ + public function getVariant($size) + { + if (!array_key_exists($size, $this->variants)) { + $this->variants[$size] = new Image; + } else { + if (!is_object($this->variants[$size])) { + $this->variants[$size] = self::getInstance($this->variants[$size]); + } + } + return $this->variants[$size]; + + } + + public function getIsEmpty() + { + return (bool) $this->_is_empty; + } + + public function setIsNotEmpty() + { + $this->_is_empty = false; + } + + public function __toString() + { + $data = array(); + foreach ($this as $attribute_name => $attribute_value) { + $data[$attribute_name] = $attribute_value; + } + return json_encode($data); + } +} \ No newline at end of file diff --git a/Upload.class.php b/Upload.class.php new file mode 100644 index 0000000..ab7d2d0 --- /dev/null +++ b/Upload.class.php @@ -0,0 +1,57 @@ + '...', 'name' => '...', 'type' => '...']) + * @return Image + */ + public static function image($file) + { + require_once PATH . '/lib/GreagwarImage/GreagwarImage.php'; + $greagwar_image = new GreagwarImage($file['tmp_name']); + $greagwar_image->setCacheDir('media/cache/images'); + $image = new Image; + $image->original_filename = $file['name']; + $image->type = $file['type']; + self::saveImage($image, $greagwar_image); + foreach (Image::$sizes as $size) { + $size_parts = explode('x', $size); + self::saveImage($image->getVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null); + } + return $image; + } + + /** + * @param $image Image + * @param $greagwar_image GreagwarImage + * @param null $width + * @param null $height + * @param bool $crop + */ + protected static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true) + { + if ($width || $height) { + $greagwar_image->resize($width, $height, 0xffffff, false, false, $crop); + } + $file_path = $greagwar_image->cacheFile($greagwar_image->guessType(), 87, true); + $path_parts = pathinfo($file_path); + $image->path = $path_parts['dirname']; + $image->filename = $path_parts['basename']; + $image->size = filesize($image->path . '/' . $image->filename); + $image->width = $greagwar_image->width(); + $image->height = $greagwar_image->height(); + $image->setIsNotEmpty(); + } + + public static function imageCompareAspectRatio($proportion, $file_path) + { + $imagesize = getimagesize($file_path); + $proprtion_parts = explode('x', $proportion); + if ($proprtion_parts[0] / $proprtion_parts[1] === $imagesize[0] / $imagesize[1]) { + return true; + } else { + false; + } + } +} \ No newline at end of file diff --git a/UploadHelper.class.php b/UploadHelper.class.php new file mode 100644 index 0000000..e691835 --- /dev/null +++ b/UploadHelper.class.php @@ -0,0 +1,25 @@ += $size_parts[0] && $imagesize[1] >= $size_parts[1]) { + return true; + } else { + return false; + } + } +} \ No newline at end of file