You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.3 KiB

<?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);
}
}