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.

67 lines
2.6 KiB

  1. <?php
  2. abstract class Upload
  3. {
  4. protected 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
  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 = 87)
  22. {
  23. /**
  24. * @var $image Image
  25. */
  26. Image::checkSubClass($class);
  27. $image = new $class;
  28. $image->original_filename = $file['name'];
  29. $greagwar_image = self::getGreagwarImage($file['tmp_name']);
  30. self::saveImage($image, $greagwar_image, null, null, true, $quality);
  31. if ($force_create_variants) {
  32. $sizes = $image->getSizes();
  33. foreach ($sizes as $size) {
  34. $size_parts = explode('x', $size);
  35. self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null, true, $quality);
  36. }
  37. }
  38. return $image;
  39. }
  40. /**
  41. * @param $image ImageVariant|Image
  42. * @param $greagwar_image GreagwarImage
  43. * @param null $width
  44. * @param null $height
  45. * @param bool $crop TODO: разъяснить за что отвечает этот "магический" параметер
  46. * @param int $quality
  47. */
  48. public static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true, $quality = 87)
  49. {
  50. $greagwar_image = clone($greagwar_image);
  51. if ($width || $height) {
  52. $greagwar_image->resize($width, $height, 0xffffff, false, false, $crop);
  53. }
  54. $image->type = $greagwar_image->guessType();
  55. $file_path = $greagwar_image->cacheFile($image->type, $quality, true);
  56. $image->size = filesize($file_path);
  57. $path_parts = pathinfo($file_path);
  58. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  59. $image->filename = $path_parts['basename'];
  60. $image->width = $greagwar_image->width();
  61. $image->height = $greagwar_image->height();
  62. }
  63. }