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.

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