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.

128 lines
5.1 KiB

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