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.

114 lines
4.8 KiB

  1. <?php
  2. abstract class Upload
  3. {
  4. public static $dir_image_cache = 'media/cache/images';
  5. public static function getGreagwarImage($file_path)
  6. {
  7. require_once PATH . '/lib/GreagwarImage/GreagwarImage.php';
  8. $greagwar_image = new GreagwarImage($file_path);
  9. $greagwar_image->setCacheDir(Config::get('PATH_WEB_ROOT') . DIRECTORY_SEPARATOR . self::$dir_image_cache);
  10. return $greagwar_image;
  11. }
  12. public static function getFilePath($image)
  13. {
  14. return Config::get('PATH_WEB_ROOT') . DIRECTORY_SEPARATOR . $image->path . DIRECTORY_SEPARATOR . $image->filename;
  15. }
  16. /**
  17. * @param $class string|Image
  18. * @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
  19. * @param $force_create_variants bool
  20. * @return Image
  21. * @throws ErrorException
  22. * TODO: Рефакторинг: убрать параметр $force_create_variants
  23. */
  24. public static function image($class, $file, $force_create_variants = true)
  25. {
  26. /**
  27. * @var $image Image
  28. */
  29. if (is_null($class) || $class == '') {
  30. throw new ErrorException('Class not defined.');
  31. }
  32. if (!is_object($class) && !class_exists($class)) {
  33. throw new ErrorException('Class "' . $class . '" not exists.');
  34. }
  35. Image::checkSubClass($class);
  36. $image = is_object($class) ? $class : new $class;
  37. $image->original_filename = $file['name'];
  38. $greagwar_image = self::getGreagwarImage($file['tmp_name']);
  39. self::saveImage($image, $greagwar_image, $class::getMaxWidth(), $class::getMaxHeight());
  40. if ($force_create_variants) {
  41. $sizes = $image->getSizes();
  42. foreach ($sizes as $size) {
  43. self::imageVariant($image, $size);
  44. }
  45. }
  46. // В случае, если был передан объект, возвращение результата не имеет значения
  47. return $image;
  48. }
  49. /**
  50. * @param $image Image
  51. * @param $size string @ex '1200x960'
  52. * @param $greagwar_image GreagwarImage
  53. */
  54. public static function imageVariant($image, $size, $greagwar_image = null)
  55. {
  56. $image_variant = new ImageVariant();
  57. if (is_null($greagwar_image)) {
  58. $greagwar_image = self::getGreagwarImage(self::getFilePath($image));
  59. }
  60. $size_parts = explode('x', $size);
  61. self::saveImage($image_variant, $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
  62. $image->variants[$size] = $image_variant;
  63. if (Config::get('PYTHON_PIL_RESIZE')) {
  64. $script_file_path = Config::get('PYTHON_PIL_RESIZE')->script_file_path;
  65. $pil_options = isset(Config::get('PYTHON_PIL_RESIZE')->pil_options) ? implode(' ', Config::get('PYTHON_PIL_RESIZE')->pil_options) . ' ' : '';
  66. ob_start();
  67. exec($script_file_path . ' --width=' . $size_parts[0] . ' --height=' . $size_parts[1] . ' ' . $pil_options . self::getFilePath($image) . ' ' . self::getFilePath($image_variant));
  68. ob_clean();
  69. }
  70. }
  71. /**
  72. * @param $image ImageVariant|Image
  73. * @param $greagwar_image GreagwarImage
  74. * @param null $width
  75. * @param null $height
  76. * TODO: Сделать возможность передавать параметры для метода _resize()
  77. */
  78. public static function saveImage($image, $greagwar_image, $width = null, $height = null)
  79. {
  80. $greagwar_image = clone($greagwar_image);
  81. if ($width || $height) {
  82. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  83. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  84. } else {
  85. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  86. }
  87. }
  88. $image->type = $greagwar_image->guessType();
  89. $file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
  90. chmod($file_path, 0664);
  91. $image->size = filesize($file_path);
  92. $path_parts = pathinfo($file_path);
  93. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  94. $image->filename = $path_parts['basename'];
  95. $image->width = $greagwar_image->width();
  96. $image->height = $greagwar_image->height();
  97. }
  98. public static function brightnessContrast($image, $value = '0x7')
  99. {
  100. $file_path = self::getFilePath($image);
  101. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  102. if (isset($output) && $output) {
  103. if (class_exists('ErrorMessage')) {
  104. ErrorMessage::log($output);
  105. }
  106. }
  107. }
  108. }