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.

80 lines
3.1 KiB

  1. <?php
  2. abstract class Upload
  3. {
  4. const PATH_WEB_ROOT = '/var/www/10ballov/face/htdocs';
  5. protected static $dir_image_cache = '/media/cache/images';
  6. protected static $dir_image_originals = '/media/original';
  7. const SAVE_ORIGINAL_IMAGE = 1;
  8. public static function getGreagwarImage($file_path)
  9. {
  10. require_once PATH . '/lib/GreagwarImage/GreagwarImage.php';
  11. $greagwar_image = new GreagwarImage($file_path);
  12. $greagwar_image->setCacheDir(self::PATH_WEB_ROOT . self::$dir_image_cache);
  13. return $greagwar_image;
  14. }
  15. /**
  16. * @param $class string
  17. * @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
  18. * @param $force_create_variants bool
  19. * @return Image
  20. * @throws ErrorException
  21. */
  22. public static function image($class, $file, $force_create_variants = true)
  23. {
  24. /**
  25. * @var $image Image
  26. */
  27. Image::checkSubClass($class);
  28. $image = new $class;
  29. $image->original_filename = $file['name'];
  30. $greagwar_image = self::getGreagwarImage($file['tmp_name']);
  31. if (self::SAVE_ORIGINAL_IMAGE) {
  32. $path_for_save_original = self::PATH_WEB_ROOT . self::$dir_image_originals . DIRECTORY_SEPARATOR . $class;
  33. if (!file_exists($path_for_save_original)) {
  34. mkdir($path_for_save_original);
  35. chmod($path_for_save_original, 0777);
  36. }
  37. $basename = $image->original_filename . '_' . md5(uniqid($image->original_filename, true));
  38. $extension = mb_substr($image->original_filename, mb_strrpos($image->original_filename, '.') + 1);
  39. $extension = mb_strtolower($extension);
  40. copy($file['tmp_name'], $path_for_save_original . DIRECTORY_SEPARATOR . $basename . '.' . $extension);
  41. }
  42. self::saveImage($image, $greagwar_image);
  43. if ($force_create_variants) {
  44. $sizes = $image->getSizes();
  45. foreach ($sizes as $size) {
  46. $size_parts = explode('x', $size);
  47. self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
  48. }
  49. }
  50. return $image;
  51. }
  52. /**
  53. * @param $image ImageVariant|Image
  54. * @param $greagwar_image GreagwarImage
  55. * @param null $width
  56. * @param null $height
  57. * @param bool $crop
  58. */
  59. public static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true)
  60. {
  61. $greagwar_image = clone($greagwar_image);
  62. if ($width || $height) {
  63. $greagwar_image->resize($width, $height, 0xffffff, false, false, $crop);
  64. }
  65. $image->type = $greagwar_image->guessType();
  66. $file_path = $greagwar_image->cacheFile($image->type, 87, true);
  67. $image->size = filesize($file_path);
  68. $path_parts = pathinfo($file_path);
  69. $image->path = preg_replace('#^' . self::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. }