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.

54 lines
2.0 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 $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
  14. * @param $force_create_variants bool
  15. * @return Image
  16. */
  17. public static function image($file, $force_create_variants = true)
  18. {
  19. $image = new Image;
  20. $image->original_filename = $file['name'];
  21. $greagwar_image = self::getGreagwarImage($file['tmp_name']);
  22. self::saveImage($image, $greagwar_image);
  23. if ($force_create_variants) {
  24. foreach (Image::$sizes as $size) {
  25. $size_parts = explode('x', $size);
  26. self::saveImage($image->getVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
  27. }
  28. }
  29. return $image;
  30. }
  31. /**
  32. * @param $image Image
  33. * @param $greagwar_image GreagwarImage
  34. * @param null $width
  35. * @param null $height
  36. * @param bool $crop
  37. */
  38. public static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true)
  39. {
  40. $greagwar_image = clone($greagwar_image);
  41. if ($width || $height) {
  42. $greagwar_image->resize($width, $height, 0xffffff, false, false, $crop);
  43. }
  44. $image->type = $greagwar_image->guessType();
  45. $file_path = $greagwar_image->cacheFile($image->type, 87, true);
  46. $image->size = filesize($file_path);
  47. $path_parts = pathinfo($file_path);
  48. $image->path = preg_replace('#^' . self::PATH_ROOT . '/#', '', $path_parts['dirname']);
  49. $image->filename = $path_parts['basename'];
  50. $image->width = $greagwar_image->width();
  51. $image->height = $greagwar_image->height();
  52. }
  53. }