Extend Image class. Separate to ImageVariant. Remove _is_new_record.
This commit is contained in:
53
File.class.php
Normal file
53
File.class.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
abstract class File
|
||||||
|
{
|
||||||
|
public $path;
|
||||||
|
public $filename;
|
||||||
|
/**
|
||||||
|
* @param array|string $data
|
||||||
|
* @return Image
|
||||||
|
* @throws ErrorException
|
||||||
|
*/
|
||||||
|
public static function getInstance($data)
|
||||||
|
{
|
||||||
|
$instance = new static($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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getWebName()
|
||||||
|
{
|
||||||
|
return $this->path . '/' . $this->filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return self::toString($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function toString($class)
|
||||||
|
{
|
||||||
|
$data = array();
|
||||||
|
foreach ($class as $attribute_name => $attribute_value) {
|
||||||
|
$data[$attribute_name] = $attribute_value;
|
||||||
|
}
|
||||||
|
return json_encode($data);
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class Image
|
class Image extends File
|
||||||
{
|
{
|
||||||
public $width;
|
public $width;
|
||||||
public $height;
|
public $height;
|
||||||
public $type;
|
public $type;
|
||||||
public $size;
|
public $size;
|
||||||
public $filename;
|
|
||||||
public $original_filename;
|
|
||||||
public $path;
|
|
||||||
public $variants = array();
|
|
||||||
|
|
||||||
private $_is_empty = true;
|
public $original_filename;
|
||||||
|
public $variants = array();
|
||||||
|
|
||||||
const SIZE_100_80 = '100x80';
|
const SIZE_100_80 = '100x80';
|
||||||
const SIZE_200_160 = '200x160';
|
const SIZE_200_160 = '200x160';
|
||||||
@ -27,72 +24,18 @@ class Image
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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
|
* @param $size string
|
||||||
* @return Image
|
* @return Image
|
||||||
*/
|
*/
|
||||||
public function getVariant($size)
|
public function getVariant($size)
|
||||||
{
|
{
|
||||||
if (!array_key_exists($size, $this->variants)) {
|
if (!array_key_exists($size, $this->variants)) {
|
||||||
$this->variants[$size] = new Image;
|
$this->variants[$size] = new ImageVariant;
|
||||||
} else {
|
} else {
|
||||||
if (!is_object($this->variants[$size])) {
|
if (!is_object($this->variants[$size])) {
|
||||||
$this->variants[$size] = self::getInstance($this->variants[$size]);
|
$this->variants[$size] = parent::getInstance($this->variants[$size]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
9
ImageVariant.class.php
Normal file
9
ImageVariant.class.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class ImageVariant extends File
|
||||||
|
{
|
||||||
|
public $width;
|
||||||
|
public $height;
|
||||||
|
public $type;
|
||||||
|
public $size;
|
||||||
|
}
|
@ -19,7 +19,6 @@ abstract class Upload
|
|||||||
{
|
{
|
||||||
$image = new Image;
|
$image = new Image;
|
||||||
$image->original_filename = $file['name'];
|
$image->original_filename = $file['name'];
|
||||||
$image->type = $file['type'];
|
|
||||||
$greagwar_image = self::getGreagwarImage($file['tmp_name']);
|
$greagwar_image = self::getGreagwarImage($file['tmp_name']);
|
||||||
self::saveImage($image, $greagwar_image);
|
self::saveImage($image, $greagwar_image);
|
||||||
if ($force_create_variants) {
|
if ($force_create_variants) {
|
||||||
@ -40,16 +39,17 @@ abstract class Upload
|
|||||||
*/
|
*/
|
||||||
public static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true)
|
public static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true)
|
||||||
{
|
{
|
||||||
|
$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, false, false, $crop);
|
||||||
}
|
}
|
||||||
$file_path = $greagwar_image->cacheFile($greagwar_image->guessType(), 87, true);
|
$image->type = $greagwar_image->guessType();
|
||||||
|
$file_path = $greagwar_image->cacheFile($image->type, 87, true);
|
||||||
$image->size = filesize($file_path);
|
$image->size = filesize($file_path);
|
||||||
$path_parts = pathinfo($file_path);
|
$path_parts = pathinfo($file_path);
|
||||||
$image->path = preg_replace('#^' . self::PATH_ROOT . '/#', '', $path_parts['dirname']);
|
$image->path = preg_replace('#^' . self::PATH_ROOT . '/#', '', $path_parts['dirname']);
|
||||||
$image->filename = $path_parts['basename'];
|
$image->filename = $path_parts['basename'];
|
||||||
$image->width = $greagwar_image->width();
|
$image->width = $greagwar_image->width();
|
||||||
$image->height = $greagwar_image->height();
|
$image->height = $greagwar_image->height();
|
||||||
$image->setIsNotEmpty();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,28 +2,43 @@
|
|||||||
|
|
||||||
abstract class UploadHelper
|
abstract class UploadHelper
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param $proportion
|
||||||
|
* @param $file_path
|
||||||
|
* @return bool
|
||||||
|
* @throws ErrorException
|
||||||
|
*/
|
||||||
public static function imageCompareAspectRatio($proportion, $file_path)
|
public static function imageCompareAspectRatio($proportion, $file_path)
|
||||||
{
|
{
|
||||||
if (file_exists($file_path) && is_readable($file_path) && is_file($file_path)) {
|
if (!(file_exists($file_path) && is_readable($file_path) && is_file($file_path))) {
|
||||||
|
throw new ErrorException('Unable to read file "' . $file_path . '".');
|
||||||
|
}
|
||||||
if ($imagesize = getimagesize($file_path)) {
|
if ($imagesize = getimagesize($file_path)) {
|
||||||
$proprtion_parts = explode('x', $proportion);
|
$proprtion_parts = explode('x', $proportion);
|
||||||
if ($proprtion_parts[0] / $proprtion_parts[1] === $imagesize[0] / $imagesize[1]) {
|
if ($proprtion_parts[0] / $proprtion_parts[1] === $imagesize[0] / $imagesize[1]) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $size
|
||||||
|
* @param $file_path
|
||||||
|
* @return bool
|
||||||
|
* @throws ErrorException
|
||||||
|
*/
|
||||||
public static function imageCompareSize($size, $file_path)
|
public static function imageCompareSize($size, $file_path)
|
||||||
{
|
{
|
||||||
if (file_exists($file_path) && is_readable($file_path) && is_file($file_path)) {
|
if (!(file_exists($file_path) && is_readable($file_path) && is_file($file_path))) {
|
||||||
|
throw new ErrorException('Unable to read file "' . $file_path . '".');
|
||||||
|
}
|
||||||
if ($imagesize = getimagesize($file_path)) {
|
if ($imagesize = getimagesize($file_path)) {
|
||||||
$size_parts = explode('x', $size);
|
$size_parts = explode('x', $size);
|
||||||
if ($imagesize[0] == $size_parts[0] && $imagesize[1] == $size_parts[1]) {
|
if ($imagesize[0] == $size_parts[0] && $imagesize[1] == $size_parts[1]) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user