Browse Source

Автоматическое создание вариантов изображений.

master
Alexander Demidov 11 years ago
parent
commit
cf2d5efcbd
  1. 9
      File.class.php
  2. 28
      Upload.class.php

9
File.class.php

@ -68,8 +68,15 @@ abstract class File
*/
public function getImageVariant($size)
{
/**
* @var $this Image
*/
if (!array_key_exists($size, $this->variants)) {
$this->variants[$size] = new ImageVariant;
if ($this->getIsNoEmpty()) {
Upload::imageVariant($this, $size);
} else {
$this->variants[$size] = new ImageVariant();
}
} else {
if (!is_object($this->variants[$size])) {
$this->variants[$size] = ImageVariant::getInstance($this->variants[$size]);

28
Upload.class.php

@ -21,7 +21,7 @@ abstract class Upload
* @throws ErrorException
* TODO: Рефакторинг: убрать параметр $force_create_variants
*/
public static function image($class, $file, $force_create_variants = true, $quality = 95)
public static function image($class, $file, $force_create_variants = true)
{
/**
* @var $image Image
@ -36,12 +36,11 @@ abstract class Upload
$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(), $quality);
self::saveImage($image, $greagwar_image, $class::getMaxWidth(), $class::getMaxHeight());
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, $quality);
self::imageVariant($image, $size);
}
}
// В случае, если был передан объект, возвращение результата не имеет значения
@ -49,14 +48,29 @@ abstract class Upload
}
/**
* @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($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
* @param int $quality
* TODO: Сделать возможность передавать параметры для метода _resize()
*/
public static function saveImage($image, $greagwar_image, $width = null, $height = null, $quality = 95)
public static function saveImage($image, $greagwar_image, $width = null, $height = null)
{
$greagwar_image = clone($greagwar_image);
if ($width || $height) {
@ -67,7 +81,7 @@ abstract class Upload
}
}
$image->type = $greagwar_image->guessType();
$file_path = $greagwar_image->cacheFile($image->type, $quality, true);
$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']);

Loading…
Cancel
Save