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.

77 lines
3.3 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, $quality = 95)
  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(), $quality);
  37. if ($force_create_variants) {
  38. $sizes = $image->getSizes();
  39. foreach ($sizes as $size) {
  40. $size_parts = explode('x', $size);
  41. self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null, $quality);
  42. }
  43. }
  44. // В случае, если был передан объект, возвращение результата не имеет значения
  45. return $image;
  46. }
  47. /**
  48. * @param $image ImageVariant|Image
  49. * @param $greagwar_image GreagwarImage
  50. * @param null $width
  51. * @param null $height
  52. * @param int $quality
  53. * TODO: Сделать возможность передавать параметры для метода _resize()
  54. */
  55. public static function saveImage($image, $greagwar_image, $width = null, $height = null, $quality = 95)
  56. {
  57. $greagwar_image = clone($greagwar_image);
  58. if ($width || $height) {
  59. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  60. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  61. } else {
  62. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  63. }
  64. }
  65. $image->type = $greagwar_image->guessType();
  66. $file_path = $greagwar_image->cacheFile($image->type, $quality, true);
  67. $image->size = filesize($file_path);
  68. $path_parts = pathinfo($file_path);
  69. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  70. $image->filename = $path_parts['basename'];
  71. $image->width = $greagwar_image->width();
  72. $image->height = $greagwar_image->height();
  73. }
  74. }