AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\StorePlan;
  3. use GdImage;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Str;
  6. use RuntimeException;
  7. /**
  8. * Gets an uploaded banner photo ready for a wide hero, so merchants can upload what they have.
  9. *
  10. * - Small photos are enlarged to 1600 pixels across and lightly sharpened.
  11. * - Very large photos are made smaller (longest side 2400), so the store loads fast.
  12. * - Phone photos are turned the right way up.
  13. * - Everything is saved as WebP. Saving again also drops hidden details such as the GPS location.
  14. *
  15. * Built into PHP (GD), so it costs nothing. An AI upscaler could replace `resize()` later.
  16. */
  17. class BannerPhoto
  18. {
  19. /** The smallest photo we accept. Enlarging more than about 5 times looks too soft. */
  20. public const MIN_WIDTH = 300;
  21. public const MIN_HEIGHT = 200;
  22. /** Small photos are enlarged to this width. */
  23. public const TARGET_WIDTH = 1600;
  24. /** No side is ever longer than this. */
  25. public const MAX_SIDE = 2400;
  26. public const QUALITY = 82;
  27. /**
  28. * Prepare the photo at this path on the "public" disk and save it as a new WebP file.
  29. * Returns the new file's path. The original file is left for the caller to remove.
  30. */
  31. public function prepare(string $path): string
  32. {
  33. $disk = Storage::disk('public');
  34. $contents = $disk->get($path);
  35. if ($contents === null) {
  36. throw new RuntimeException("Banner photo {$path} is missing.");
  37. }
  38. // A 6 MB photo can be very large once opened, so give GD room for this one job.
  39. ini_set('memory_limit', '512M');
  40. $image = @imagecreatefromstring($contents);
  41. if (! $image instanceof GdImage) {
  42. throw new RuntimeException("Banner photo {$path} could not be opened.");
  43. }
  44. $image = $this->turnUpright($image, $contents);
  45. $image = $this->resize($image);
  46. ob_start();
  47. imagewebp($image, null, self::QUALITY);
  48. $webp = (string) ob_get_clean();
  49. $folder = dirname($path);
  50. $newPath = ($folder === '.' ? '' : $folder.'/').'banner-'.Str::random(24).'.webp';
  51. $disk->put($newPath, $webp);
  52. return $newPath;
  53. }
  54. /**
  55. * The size the photo will be saved at.
  56. *
  57. * @return array{0: int, 1: int}
  58. */
  59. public static function targetSize(int $width, int $height): array
  60. {
  61. $scale = min(
  62. max(1, self::TARGET_WIDTH / $width),
  63. self::MAX_SIDE / max($width, $height),
  64. );
  65. return [(int) round($width * $scale), (int) round($height * $scale)];
  66. }
  67. private function resize(GdImage $image): GdImage
  68. {
  69. $width = imagesx($image);
  70. $height = imagesy($image);
  71. [$newWidth, $newHeight] = self::targetSize($width, $height);
  72. if ($newWidth === $width && $newHeight === $height) {
  73. return $image;
  74. }
  75. $resized = imagecreatetruecolor($newWidth, $newHeight);
  76. // Keep see-through parts of PNG and WebP photos.
  77. imagealphablending($resized, false);
  78. imagesavealpha($resized, true);
  79. imagefill($resized, 0, 0, imagecolorallocatealpha($resized, 0, 0, 0, 127));
  80. imagecopyresampled($resized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  81. // Enlarged photos turn a little soft, so sharpen them gently.
  82. if ($newWidth > $width * 1.05) {
  83. imageconvolution($resized, [[-1, -1, -1], [-1, 20, -1], [-1, -1, -1]], 12, 0);
  84. }
  85. return $resized;
  86. }
  87. /**
  88. * Phones often save photos sideways with a note saying which way is up. Follow that note.
  89. */
  90. private function turnUpright(GdImage $image, string $contents): GdImage
  91. {
  92. if (! function_exists('exif_read_data') || ! str_starts_with($contents, "\xFF\xD8")) {
  93. return $image;
  94. }
  95. $exif = @exif_read_data('data://image/jpeg;base64,'.base64_encode($contents));
  96. $orientation = is_array($exif) ? (int) ($exif['Orientation'] ?? 1) : 1;
  97. if (in_array($orientation, [2, 4, 5, 7], true)) {
  98. imageflip($image, IMG_FLIP_HORIZONTAL);
  99. }
  100. $angle = match ($orientation) {
  101. 3, 4 => 180,
  102. 5, 6 => -90,
  103. 7, 8 => 90,
  104. default => 0,
  105. };
  106. if ($angle === 0) {
  107. return $image;
  108. }
  109. $turned = imagerotate($image, $angle, 0);
  110. return $turned instanceof GdImage ? $turned : $image;
  111. }
  112. }