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.

113 lines
4.6 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. ob_start();
  66. exec($script_file_path . ' --width=' . $size_parts[0] . ' --height=' . $size_parts[1] . ' ' . self::getFilePath($image) . ' ' . self::getFilePath($image_variant));
  67. ob_clean();
  68. }
  69. }
  70. /**
  71. * @param $image ImageVariant|Image
  72. * @param $greagwar_image GreagwarImage
  73. * @param null $width
  74. * @param null $height
  75. * TODO: Сделать возможность передавать параметры для метода _resize()
  76. */
  77. public static function saveImage($image, $greagwar_image, $width = null, $height = null)
  78. {
  79. $greagwar_image = clone($greagwar_image);
  80. if ($width || $height) {
  81. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  82. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  83. } else {
  84. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  85. }
  86. }
  87. $image->type = $greagwar_image->guessType();
  88. $file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
  89. chmod($file_path, 0664);
  90. $image->size = filesize($file_path);
  91. $path_parts = pathinfo($file_path);
  92. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  93. $image->filename = $path_parts['basename'];
  94. $image->width = $greagwar_image->width();
  95. $image->height = $greagwar_image->height();
  96. }
  97. public static function brightnessContrast($image, $value = '0x7')
  98. {
  99. $file_path = self::getFilePath($image);
  100. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  101. if (isset($output) && $output) {
  102. if (class_exists('ErrorMessage')) {
  103. ErrorMessage::log($output);
  104. }
  105. }
  106. }
  107. }