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