Browse Source

Добавлено поддержка указания качества результирующего изображения в Upload.class

master
Alexander Demidov 12 years ago
parent
commit
f3f36b421b
  1. 13
      Upload.class.php

13
Upload.class.php

@ -16,10 +16,12 @@ abstract class Upload
* @param $class string
* @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)
public static function image($class, $file, $force_create_variants = true, $quality = 87)
{
/**
* @var $image Image
@ -29,12 +31,12 @@ abstract class Upload
$image->original_filename = $file['name'];
$greagwar_image = self::getGreagwarImage($file['tmp_name']);
self::saveImage($image, $greagwar_image);
self::saveImage($image, $greagwar_image, null, null, true, $quality);
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);
self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null, true, $quality);
}
}
return $image;
@ -46,15 +48,16 @@ abstract class Upload
* @param null $width
* @param null $height
* @param bool $crop TODO: разъяснить за что отвечает этот "магический" параметер
* @param int $quality
*/
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, $quality = 87)
{
$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);
$file_path = $greagwar_image->cacheFile($image->type, $quality, 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