Add feature - save original image. Allow create empty instance of ImageCollection.
This commit is contained in:
@ -10,7 +10,7 @@ class Image extends File
|
||||
public $original_filename;
|
||||
public $variants = array();
|
||||
|
||||
protected $sizes;
|
||||
protected static $sizes;
|
||||
|
||||
/**
|
||||
* @param $class string
|
||||
@ -31,7 +31,7 @@ class Image extends File
|
||||
|
||||
public function getSizes()
|
||||
{
|
||||
return $this->sizes;
|
||||
return self::$sizes;
|
||||
}
|
||||
|
||||
public function getVariant($size)
|
||||
|
@ -8,27 +8,49 @@ abstract class ImageCollection extends ArrayIterator
|
||||
protected $class;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array|null $data
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function __construct($data)
|
||||
public function __construct($data = null)
|
||||
{
|
||||
/**
|
||||
* @var $class Image
|
||||
*/
|
||||
$class = $this->class;
|
||||
Image::checkSubClass($class);
|
||||
if (is_null($data) || $data == '') {
|
||||
throw new ErrorException('Empty data.');
|
||||
}
|
||||
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.');
|
||||
if (!is_null($data) && $data != '') {
|
||||
if (!is_array($data)) {
|
||||
$data = json_decode($data, true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
ErrorMessage::log('Unable to convert json-string to array.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!isset($data['order']) || !isset($data['files'])) {
|
||||
ErrorMessage::log('Incorrect format for create ImageCollection.');
|
||||
return;
|
||||
}
|
||||
foreach ($data['order'] as $original_filename) {
|
||||
if (!isset($data['files'][$original_filename])) {
|
||||
ErrorMessage::log('Unable to fetch image "' . $original_filename . '" from ImageCollection.');
|
||||
continue;
|
||||
}
|
||||
$this->append($class::getInstance($data['files'][$original_filename]));
|
||||
}
|
||||
}
|
||||
foreach ($data as $item_data) {
|
||||
$this->append($class::getInstance($item_data));
|
||||
return;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
$image_collection = array(
|
||||
'order' => array(),
|
||||
'files' => array()
|
||||
);
|
||||
foreach ((array)$this as $image) {
|
||||
$image_collection['order'][] = $image->original_filename;
|
||||
$image_collection['files'][$image->original_filename] = (string)$image;
|
||||
}
|
||||
return json_encode($image_collection);
|
||||
}
|
||||
}
|
@ -2,12 +2,19 @@
|
||||
|
||||
abstract class Upload
|
||||
{
|
||||
const PATH_ROOT = '/var/www/10ballov/face/htdocs';
|
||||
const PATH_WEB_ROOT = '/var/www/10ballov/face/htdocs';
|
||||
|
||||
protected static $dir_image_cache = '/media/cache/images';
|
||||
|
||||
protected static $dir_image_originals = '/media/original';
|
||||
|
||||
const SAVE_ORIGINAL_IMAGE = 1;
|
||||
|
||||
public static function getGreagwarImage($file_path)
|
||||
{
|
||||
require_once PATH . '/lib/GreagwarImage/GreagwarImage.php';
|
||||
$greagwar_image = new GreagwarImage($file_path);
|
||||
$greagwar_image->setCacheDir(self::PATH_ROOT . '/media/cache/images');
|
||||
$greagwar_image->setCacheDir(self::PATH_WEB_ROOT . self::$dir_image_cache);
|
||||
return $greagwar_image;
|
||||
}
|
||||
/**
|
||||
@ -25,7 +32,19 @@ abstract class Upload
|
||||
Image::checkSubClass($class);
|
||||
$image = new $class;
|
||||
$image->original_filename = $file['name'];
|
||||
|
||||
$greagwar_image = self::getGreagwarImage($file['tmp_name']);
|
||||
if (self::SAVE_ORIGINAL_IMAGE) {
|
||||
$path_for_save_original = self::PATH_WEB_ROOT . self::$dir_image_originals . DIRECTORY_SEPARATOR . $class;
|
||||
if (!file_exists($path_for_save_original)) {
|
||||
mkdir($path_for_save_original);
|
||||
chmod($path_for_save_original, 0777);
|
||||
}
|
||||
$basename = $image->original_filename . '_' . md5(uniqid($image->original_filename, true));
|
||||
$extension = mb_substr($image->original_filename, mb_strrpos($image->original_filename, '.') + 1);
|
||||
$extension = mb_strtolower($extension);
|
||||
copy($file['tmp_name'], $path_for_save_original . DIRECTORY_SEPARATOR . $basename . '.' . $extension);
|
||||
}
|
||||
self::saveImage($image, $greagwar_image);
|
||||
if ($force_create_variants) {
|
||||
$sizes = $image->getSizes();
|
||||
@ -38,7 +57,7 @@ abstract class Upload
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $image ImageVariant
|
||||
* @param $image ImageVariant|Image
|
||||
* @param $greagwar_image GreagwarImage
|
||||
* @param null $width
|
||||
* @param null $height
|
||||
@ -54,7 +73,7 @@ abstract class Upload
|
||||
$file_path = $greagwar_image->cacheFile($image->type, 87, true);
|
||||
$image->size = filesize($file_path);
|
||||
$path_parts = pathinfo($file_path);
|
||||
$image->path = preg_replace('#^' . self::PATH_ROOT . '/#', '', $path_parts['dirname']);
|
||||
$image->path = preg_replace('#^' . self::PATH_WEB_ROOT . '/#', '', $path_parts['dirname']);
|
||||
$image->filename = $path_parts['basename'];
|
||||
$image->width = $greagwar_image->width();
|
||||
$image->height = $greagwar_image->height();
|
||||
|
Reference in New Issue
Block a user