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.

61 lines
2.2 KiB

  1. <?php
  2. abstract class Upload
  3. {
  4. const PATH_ROOT = '/var/www/10ballov/face/htdocs';
  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(self::PATH_ROOT . '/media/cache/images');
  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. * @return Image
  17. * @throws ErrorException
  18. */
  19. public static function image($class, $file, $force_create_variants = true)
  20. {
  21. /**
  22. * @var $image Image
  23. */
  24. Image::checkSubClass($class);
  25. $image = new $class;
  26. $image->original_filename = $file['name'];
  27. $greagwar_image = self::getGreagwarImage($file['tmp_name']);
  28. self::saveImage($image, $greagwar_image);
  29. if ($force_create_variants) {
  30. $sizes = $image->getSizes();
  31. foreach ($sizes as $size) {
  32. $size_parts = explode('x', $size);
  33. self::saveImage($image->getImageVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
  34. }
  35. }
  36. return $image;
  37. }
  38. /**
  39. * @param $image ImageVariant
  40. * @param $greagwar_image GreagwarImage
  41. * @param null $width
  42. * @param null $height
  43. * @param bool $crop
  44. */
  45. public static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true)
  46. {
  47. $greagwar_image = clone($greagwar_image);
  48. if ($width || $height) {
  49. $greagwar_image->resize($width, $height, 0xffffff, false, false, $crop);
  50. }
  51. $image->type = $greagwar_image->guessType();
  52. $file_path = $greagwar_image->cacheFile($image->type, 87, true);
  53. $image->size = filesize($file_path);
  54. $path_parts = pathinfo($file_path);
  55. $image->path = preg_replace('#^' . self::PATH_ROOT . '/#', '', $path_parts['dirname']);
  56. $image->filename = $path_parts['basename'];
  57. $image->width = $greagwar_image->width();
  58. $image->height = $greagwar_image->height();
  59. }
  60. }