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.

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