Files
image/Upload.class.php

92 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
abstract class Upload
{
public static $dir_image_cache = 'media/cache/images';
public static function getGreagwarImage($file_path)
{
require_once PATH . '/lib/GreagwarImage/GreagwarImage.php';
$greagwar_image = new GreagwarImage($file_path);
$greagwar_image->setCacheDir(Config::get('PATH_WEB_ROOT') . DIRECTORY_SEPARATOR . self::$dir_image_cache);
return $greagwar_image;
}
/**
* @param $class string|Image
* @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
* @param $force_create_variants bool
* @param int $quality
* @return Image
* @throws ErrorException
* TODO: Рефакторинг: убрать параметр $force_create_variants
*/
public static function image($class, $file, $force_create_variants = true)
{
/**
* @var $image Image
*/
if (is_null($class) || $class == '') {
throw new ErrorException('Class not defined.');
}
if (!is_object($class) && !class_exists($class)) {
throw new ErrorException('Class "' . $class . '" not exists.');
}
Image::checkSubClass($class);
$image = is_object($class) ? $class : new $class;
$image->original_filename = $file['name'];
$greagwar_image = self::getGreagwarImage($file['tmp_name']);
self::saveImage($image, $greagwar_image, $class::getMaxWidth(), $class::getMaxHeight());
if ($force_create_variants) {
$sizes = $image->getSizes();
foreach ($sizes as $size) {
self::imageVariant($image, $size);
}
}
// В случае, если был передан объект, возвращение результата не имеет значения
return $image;
}
/**
* @param $image Image
* @param $size string @ex '1200x960'
* @param $greagwar_image GreagwarImage
*/
public static function imageVariant($image, $size, $greagwar_image = null)
{
$image_variant = new ImageVariant();
if (is_null($greagwar_image)) {
$greagwar_image = self::getGreagwarImage(Config::get('PATH_WEB_ROOT') . DIRECTORY_SEPARATOR . $image->path . DIRECTORY_SEPARATOR . $image->filename);
}
$size_parts = explode('x', $size);
self::saveImage($image_variant, $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
$image->variants[$size] = $image_variant;
}
/**
* @param $image ImageVariant|Image
* @param $greagwar_image GreagwarImage
* @param null $width
* @param null $height
* TODO: Сделать возможность передавать параметры для метода _resize()
*/
public static function saveImage($image, $greagwar_image, $width = null, $height = null)
{
$greagwar_image = clone($greagwar_image);
if ($width || $height) {
if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
$greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
} else {
$greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
}
}
$image->type = $greagwar_image->guessType();
$file_path = $greagwar_image->cacheFile($image->type, $quality = 95, true);
$image->size = filesize($file_path);
$path_parts = pathinfo($file_path);
$image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
$image->filename = $path_parts['basename'];
$image->width = $greagwar_image->width();
$image->height = $greagwar_image->height();
}
}