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.

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