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.

116 lines
4.7 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' => '...', 'error' => '...'])
  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. if (isset($file['error']) && $file['error'] != 0) {
  37. return false;
  38. }
  39. $image = is_object($class) ? $class : new $class;
  40. $image->original_filename = $file['name'];
  41. $greagwar_image = self::getGreagwarImage($file['tmp_name']);
  42. self::saveImage($image, $greagwar_image, $class::getMaxWidth(), $class::getMaxHeight());
  43. if ($force_create_variants) {
  44. $sizes = $image->getSizes();
  45. foreach ($sizes as $size) {
  46. self::imageVariant($image, $size);
  47. }
  48. }
  49. return $image;
  50. }
  51. /**
  52. * @param $image Image
  53. * @param $size string @ex '1200x960'
  54. * @param $greagwar_image GreagwarImage
  55. */
  56. public static function imageVariant($image, $size, $greagwar_image = null)
  57. {
  58. $image_variant = new ImageVariant();
  59. if (is_null($greagwar_image)) {
  60. $greagwar_image = self::getGreagwarImage(self::getFilePath($image));
  61. }
  62. $size_parts = explode('x', $size);
  63. self::saveImage($image_variant, $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
  64. $image->variants[$size] = $image_variant;
  65. if (Config::get('PYTHON_PIL_RESIZE')) {
  66. $script_file_path = Config::get('PYTHON_PIL_RESIZE')->script_file_path;
  67. $pil_options = isset(Config::get('PYTHON_PIL_RESIZE')->pil_options) ? implode(' ', Config::get('PYTHON_PIL_RESIZE')->pil_options) . ' ' : '';
  68. ob_start();
  69. exec($script_file_path . ' --width=' . $size_parts[0] . ' --height=' . $size_parts[1] . ' ' . $pil_options . self::getFilePath($image) . ' ' . self::getFilePath($image_variant));
  70. ob_clean();
  71. }
  72. }
  73. /**
  74. * @param $image ImageVariant|Image
  75. * @param $greagwar_image GreagwarImage
  76. * @param null $width
  77. * @param null $height
  78. * TODO: Сделать возможность передавать параметры для метода _resize()
  79. */
  80. public static function saveImage($image, $greagwar_image, $width = null, $height = null)
  81. {
  82. $greagwar_image = clone($greagwar_image);
  83. if ($width || $height) {
  84. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  85. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  86. } else {
  87. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  88. }
  89. }
  90. $image->type = $greagwar_image->guessType();
  91. $file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
  92. chmod($file_path, 0664);
  93. $image->size = filesize($file_path);
  94. $path_parts = pathinfo($file_path);
  95. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  96. $image->filename = $path_parts['basename'];
  97. $image->width = $greagwar_image->width();
  98. $image->height = $greagwar_image->height();
  99. }
  100. public static function brightnessContrast($image, $value = '0x7')
  101. {
  102. $file_path = self::getFilePath($image);
  103. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  104. if (isset($output) && $output) {
  105. if (class_exists('ErrorMessage')) {
  106. ErrorMessage::log($output);
  107. }
  108. }
  109. }
  110. }