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.

106 lines
4.2 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. protected 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' => '...', 'type' => '...'])
  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. $image = is_object($class) ? $class : new $class;
  37. $image->original_filename = $file['name'];
  38. $greagwar_image = self::getGreagwarImage($file['tmp_name']);
  39. self::saveImage($image, $greagwar_image, $class::getMaxWidth(), $class::getMaxHeight());
  40. if ($force_create_variants) {
  41. $sizes = $image->getSizes();
  42. foreach ($sizes as $size) {
  43. self::imageVariant($image, $size);
  44. }
  45. }
  46. // В случае, если был передан объект, возвращение результата не имеет значения
  47. return $image;
  48. }
  49. /**
  50. * @param $image Image
  51. * @param $size string @ex '1200x960'
  52. * @param $greagwar_image GreagwarImage
  53. */
  54. public static function imageVariant($image, $size, $greagwar_image = null)
  55. {
  56. $image_variant = new ImageVariant();
  57. if (is_null($greagwar_image)) {
  58. $greagwar_image = self::getGreagwarImage(self::getFilePath($image));
  59. }
  60. $size_parts = explode('x', $size);
  61. self::saveImage($image_variant, $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
  62. $image->variants[$size] = $image_variant;
  63. }
  64. /**
  65. * @param $image ImageVariant|Image
  66. * @param $greagwar_image GreagwarImage
  67. * @param null $width
  68. * @param null $height
  69. * TODO: Сделать возможность передавать параметры для метода _resize()
  70. */
  71. public static function saveImage($image, $greagwar_image, $width = null, $height = null)
  72. {
  73. $greagwar_image = clone($greagwar_image);
  74. if ($width || $height) {
  75. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  76. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  77. } else {
  78. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  79. }
  80. }
  81. $image->type = $greagwar_image->guessType();
  82. $file_path = $greagwar_image->cacheFile($image->type, $quality = 95, true);
  83. $image->size = filesize($file_path);
  84. $path_parts = pathinfo($file_path);
  85. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  86. $image->filename = $path_parts['basename'];
  87. $image->width = $greagwar_image->width();
  88. $image->height = $greagwar_image->height();
  89. }
  90. public static function brightnessContrast($image, $value = '0x7')
  91. {
  92. $file_path = self::getFilePath($image);
  93. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  94. if (isset($output) && $output) {
  95. if (class_exists('ErrorMessage')) {
  96. ErrorMessage::log($output);
  97. }
  98. }
  99. }
  100. }