Alexander Demidov
12 years ago
3 changed files with 180 additions and 0 deletions
@ -0,0 +1,98 @@ |
|||
<?php |
|||
|
|||
class Image |
|||
{ |
|||
public $width; |
|||
public $height; |
|||
public $type; |
|||
public $size; |
|||
public $filename; |
|||
public $original_filename; |
|||
public $path; |
|||
public $variants = array(); |
|||
|
|||
private $_is_empty = true; |
|||
|
|||
const SIZE_100_80 = '100x80'; |
|||
const SIZE_200_160 = '200x160'; |
|||
const SIZE_300_240 = '300x240'; |
|||
const SIZE_550_440 = '550x440'; |
|||
const SIZE_SKI = '1000x'; |
|||
|
|||
public static $sizes = array( |
|||
self::SIZE_100_80, |
|||
self::SIZE_200_160, |
|||
self::SIZE_300_240, |
|||
self::SIZE_550_440 |
|||
); |
|||
|
|||
/** |
|||
* @param array|string $data |
|||
* @return Image |
|||
* @throws ErrorException |
|||
*/ |
|||
public static function getInstance($data) |
|||
{ |
|||
$instance = new self($data); |
|||
if (is_null($data)) { |
|||
throw new ErrorException('Empty data for Image::getInstance().'); |
|||
} |
|||
if (!is_array($data)) { |
|||
$data = json_decode($data, true); |
|||
if (json_last_error() != JSON_ERROR_NONE) { |
|||
throw new ErrorException('Unable to convert json-string to array.'); |
|||
} |
|||
} |
|||
foreach ($data as $attribute_name => $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); |
|||
} |
|||
} |
@ -0,0 +1,57 @@ |
|||
<?php |
|||
|
|||
abstract class Upload |
|||
{ |
|||
/** |
|||
* @param $file array (ex. ['tmp_name' => '...', '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; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,25 @@ |
|||
<?php |
|||
|
|||
abstract class UploadHelper |
|||
{ |
|||
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; |
|||
} |
|||
} |
|||
public static function imageCheckMinSize($size, $file_path) |
|||
{ |
|||
$imagesize = getimagesize($file_path); |
|||
$size_parts = explode('x', $size); |
|||
if ($imagesize[0] >= $size_parts[0] && $imagesize[1] >= $size_parts[1]) { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue