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.
65 lines
2.3 KiB
65 lines
2.3 KiB
<?php
|
|
|
|
abstract class Upload
|
|
{
|
|
protected 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
|
|
* @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
|
|
* @param $force_create_variants bool
|
|
* @return Image
|
|
* @throws ErrorException
|
|
*/
|
|
public static function image($class, $file, $force_create_variants = true)
|
|
{
|
|
/**
|
|
* @var $image Image
|
|
*/
|
|
Image::checkSubClass($class);
|
|
$image = new $class;
|
|
$image->original_filename = $file['name'];
|
|
|
|
$greagwar_image = self::getGreagwarImage($file['tmp_name']);
|
|
self::saveImage($image, $greagwar_image);
|
|
if ($force_create_variants) {
|
|
$sizes = $image->getSizes();
|
|
foreach ($sizes as $size) {
|
|
$size_parts = explode('x', $size);
|
|
self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
|
|
}
|
|
}
|
|
return $image;
|
|
}
|
|
|
|
/**
|
|
* @param $image ImageVariant|Image
|
|
* @param $greagwar_image GreagwarImage
|
|
* @param null $width
|
|
* @param null $height
|
|
* @param bool $crop
|
|
*/
|
|
public static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true)
|
|
{
|
|
$greagwar_image = clone($greagwar_image);
|
|
if ($width || $height) {
|
|
$greagwar_image->resize($width, $height, 0xffffff, false, false, $crop);
|
|
}
|
|
$image->type = $greagwar_image->guessType();
|
|
$file_path = $greagwar_image->cacheFile($image->type, 87, 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();
|
|
}
|
|
}
|