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.

56 lines
2.0 KiB

  1. <?php
  2. abstract class Upload
  3. {
  4. /**
  5. * @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'type' => '...'])
  6. * @return Image
  7. */
  8. public static function image($file)
  9. {
  10. require_once PATH . '/lib/GreagwarImage/GreagwarImage.php';
  11. $greagwar_image = new GreagwarImage($file['tmp_name']);
  12. $greagwar_image->setCacheDir('media/cache/images');
  13. $image = new Image;
  14. $image->original_filename = $file['name'];
  15. $image->type = $file['type'];
  16. self::saveImage($image, $greagwar_image);
  17. foreach (Image::$sizes as $size) {
  18. $size_parts = explode('x', $size);
  19. self::saveImage($image->getVariant($size), $greagwar_image, $size_parts[0] ? : null, $size_parts[1] ? : null);
  20. }
  21. return $image;
  22. }
  23. /**
  24. * @param $image Image
  25. * @param $greagwar_image GreagwarImage
  26. * @param null $width
  27. * @param null $height
  28. * @param bool $crop
  29. */
  30. protected static function saveImage($image, $greagwar_image, $width = null, $height = null, $crop = true)
  31. {
  32. if ($width || $height) {
  33. $greagwar_image->resize($width, $height, 0xffffff, false, false, $crop);
  34. }
  35. $file_path = $greagwar_image->cacheFile($greagwar_image->guessType(), 87, true);
  36. $path_parts = pathinfo($file_path);
  37. $image->path = $path_parts['dirname'];
  38. $image->filename = $path_parts['basename'];
  39. $image->size = filesize($image->path . '/' . $image->filename);
  40. $image->width = $greagwar_image->width();
  41. $image->height = $greagwar_image->height();
  42. $image->setIsNotEmpty();
  43. }
  44. public static function imageCompareAspectRatio($proportion, $file_path)
  45. {
  46. $imagesize = getimagesize($file_path);
  47. $proprtion_parts = explode('x', $proportion);
  48. if ($proprtion_parts[0] / $proprtion_parts[1] === $imagesize[0] / $imagesize[1]) {
  49. return true;
  50. } else {
  51. false;
  52. }
  53. }
  54. }