Корректировка. Рефакторинг для раздельной работы PythonLibs/Image и GreagwarImage

This commit is contained in:
2014-05-15 17:49:47 +04:00
parent 153cd49d1d
commit 039e021a52
2 changed files with 50 additions and 46 deletions

View File

@ -78,7 +78,6 @@ abstract class File
* @param $size string * @param $size string
* @param $force_create bool * @param $force_create bool
* @return ImageVariant * @return ImageVariant
* TODO: при форсировании создания варианта, получать owner (объекта - владельца изображения) и пытаться связать с ним созданное изображение и записать его реквизиты в БД)
*/ */
public function getImageVariant($size, $force_create = false) public function getImageVariant($size, $force_create = false)
{ {

View File

@ -69,33 +69,21 @@ abstract class Upload
$image_variant = new ImageVariant(); $image_variant = new ImageVariant();
$size_parts = explode('x', $size); $size_parts = explode('x', $size);
self::saveImage($image_variant, self::getFilePath($image), $size_parts[0] ? : null, $size_parts[1] ? : null); self::saveImage($image_variant, self::getFilePath($image), $size_parts[0] ? : null, $size_parts[1] ? : null);
if (Config::get('PYTHON_PIL_PASTE') &&
function_exists(array($image, 'getWatermark')) &&
$image->getWatermark($size)
) {
$script_file_path = Config::get('PYTHON_PIL_PASTE')->script_file_path;
$pil_options = (isset(Config::get('PYTHON_PIL_PASTE')->pil_options) && Config::get('PYTHON_PIL_PASTE')->pil_options) ? ' ' . implode(' ', Config::get('PYTHON_PIL_PASTE')->pil_options) : '';
// ob_start();
passthru($script_file_path
. $pil_options
. ' ' . self::getFilePath($image_variant)
. ' ' . Config::get('PATH_WEB_ROOT') . '/' . $image->getWatermark($size));
// ob_clean();
}
self::defineSizeWidthAndHeight($image_variant);
$image->variants[$size] = $image_variant; $image->variants[$size] = $image_variant;
if (Config::get('PYTHON_PIL_RESIZE')) {
$script_file_path = Config::get('PYTHON_PIL_RESIZE')->script_file_path;
$pil_options = isset(Config::get('PYTHON_PIL_RESIZE')->pil_options) ? ' ' . implode(' ', Config::get('PYTHON_PIL_RESIZE')->pil_options) : '';
// ob_start();
passthru($script_file_path
. (($size_parts[0])?' --width=' . $size_parts[0]:'')
. (($size_parts[1])?' --height=' . $size_parts[1]:'')
. $pil_options
. ' ' . self::getFilePath($image)
. ' ' . self::getFilePath($image_variant));
// ob_clean();
}
if (Config::get('PYTHON_PIL_PASTE') &&
function_exists(array($image, 'getWatermark')) &&
$image->getWatermark($size)
) {
$script_file_path = Config::get('PYTHON_PIL_PASTE')->script_file_path;
$pil_options = (isset(Config::get('PYTHON_PIL_PASTE')->pil_options) && Config::get('PYTHON_PIL_PASTE')->pil_options) ? ' ' . implode(' ', Config::get('PYTHON_PIL_PASTE')->pil_options) : '';
// ob_start();
passthru($script_file_path
. $pil_options
. ' ' . self::getFilePath($image_variant)
. ' ' . Config::get('PATH_WEB_ROOT') . '/' . $image->getWatermark($size));
// ob_clean();
}
self::defineSizeWidthAndHeight($image_variant);
} }
/** /**
@ -103,7 +91,7 @@ abstract class Upload
* @param $tmp_file_path string|GreagwarImage * @param $tmp_file_path string|GreagwarImage
* @param null $width * @param null $width
* @param null $height * @param null $height
* TODO: Refactoring * TODO: Возможно, стоит искоренить GreagwarImage
*/ */
public static function saveImage($image, $tmp_file_path , $width = null, $height = null) public static function saveImage($image, $tmp_file_path , $width = null, $height = null)
{ {
@ -116,24 +104,41 @@ abstract class Upload
$greagwar_image = self::getGreagwarImage($tmp_file_path); $greagwar_image = self::getGreagwarImage($tmp_file_path);
} }
$image->type = $greagwar_image->guessType(); $image->type = $greagwar_image->guessType();
if ($width || $height) { $hash = $greagwar_image->getHash($image->type, $quality = 100);
if (!Config::get('PYTHON_PIL_RESIZE')) { $file_path = $greagwar_image->generateFileFromhash($hash) . '.' . $image->type;
if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) { if (Image::getIsSubClass($image)) {
$greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true); if (is_uploaded_file($tmp_file_path)) {
} else { move_uploaded_file($tmp_file_path, $file_path);
$greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false); } else {
} copy($tmp_file_path, $file_path);
$file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true); }
} } else {
} if (Config::get('PYTHON_PIL_RESIZE')) {
if (Config::get('PYTHON_PIL_RESIZE')) { $script_file_path = Config::get( 'PYTHON_PIL_RESIZE' )->script_file_path;
$hash = $greagwar_image->getHash($image->type, $quality = 100); $pil_options = isset( Config::get( 'PYTHON_PIL_RESIZE' )->pil_options ) ? ' ' . implode( ' ', Config::get( 'PYTHON_PIL_RESIZE' )->pil_options ) : '';
$file_path = $greagwar_image->generateFileFromhash($hash) . '.' . $image->type; // ob_start();
if (is_uploaded_file($tmp_file_path)) { $code = null;
move_uploaded_file($tmp_file_path, $file_path); $command = $script_file_path
} else { . ( ( $width ) ? ' --width=' . $width : '' )
copy($tmp_file_path, $file_path); . ( ( $height ) ? ' --height=' . $height : '' )
} . $pil_options
. ' ' . $tmp_file_path
. ' ' . $file_path;
passthru( $command, $code );
if ($code !== 0) {
throw new ErrorException('Command PYTHON_PIL_RESIZE: ' . $command );
}
// ob_clean();
} else {
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);
}
$file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
}
}
} }
$path_parts = pathinfo($file_path); $path_parts = pathinfo($file_path);
$image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']); $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);