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.

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