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.

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