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.

91 lines
3.7 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. /**
  13. * @param $class string|Image
  14. * @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
  15. * @param $force_create_variants bool
  16. * @param int $quality
  17. * @return Image
  18. * @throws ErrorException
  19. * TODO: Рефакторинг: убрать параметр $force_create_variants
  20. */
  21. public static function image($class, $file, $force_create_variants = true)
  22. {
  23. /**
  24. * @var $image Image
  25. */
  26. if (is_null($class) || $class == '') {
  27. throw new ErrorException('Class not defined.');
  28. }
  29. if (!is_object($class) && !class_exists($class)) {
  30. throw new ErrorException('Class "' . $class . '" not exists.');
  31. }
  32. Image::checkSubClass($class);
  33. $image = is_object($class) ? $class : new $class;
  34. $image->original_filename = $file['name'];
  35. $greagwar_image = self::getGreagwarImage($file['tmp_name']);
  36. self::saveImage($image, $greagwar_image, $class::getMaxWidth(), $class::getMaxHeight());
  37. if ($force_create_variants) {
  38. $sizes = $image->getSizes();
  39. foreach ($sizes as $size) {
  40. self::imageVariant($image, $size);
  41. }
  42. }
  43. // В случае, если был передан объект, возвращение результата не имеет значения
  44. return $image;
  45. }
  46. /**
  47. * @param $image Image
  48. * @param $size string @ex '1200x960'
  49. * @param $greagwar_image GreagwarImage
  50. */
  51. public static function imageVariant($image, $size, $greagwar_image = null)
  52. {
  53. $image_variant = new ImageVariant();
  54. if (is_null($greagwar_image)) {
  55. $greagwar_image = self::getGreagwarImage($image->path . DIRECTORY_SEPARATOR . $image->filename);
  56. }
  57. $size_parts = explode('x', $size);
  58. self::saveImage($image_variant, $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
  59. $image->variants[$size] = $image_variant;
  60. }
  61. /**
  62. * @param $image ImageVariant|Image
  63. * @param $greagwar_image GreagwarImage
  64. * @param null $width
  65. * @param null $height
  66. * TODO: Сделать возможность передавать параметры для метода _resize()
  67. */
  68. public static function saveImage($image, $greagwar_image, $width = null, $height = null)
  69. {
  70. $greagwar_image = clone($greagwar_image);
  71. if ($width || $height) {
  72. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  73. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  74. } else {
  75. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  76. }
  77. }
  78. $image->type = $greagwar_image->guessType();
  79. $file_path = $greagwar_image->cacheFile($image->type, $quality = 95, true);
  80. $image->size = filesize($file_path);
  81. $path_parts = pathinfo($file_path);
  82. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  83. $image->filename = $path_parts['basename'];
  84. $image->width = $greagwar_image->width();
  85. $image->height = $greagwar_image->height();
  86. }
  87. }