|
|
@@ -4,6 +4,7 @@ namespace App\Storefront\Logos;
|
|
4
|
4
|
|
|
5
|
5
|
use App\Ai\AiException;
|
|
6
|
6
|
use App\Ai\AiManager;
|
|
|
7
|
+use App\Mail\StoreBrand;
|
|
7
|
8
|
use App\Models\Store;
|
|
8
|
9
|
use App\StorePlan\Ai\JsonAnswer;
|
|
9
|
10
|
use App\StorePlan\PlanSchema;
|
|
|
@@ -31,6 +32,12 @@ class StoreLogos
|
|
31
|
32
|
|
|
32
|
33
|
public const int MIN_SIDE = 64;
|
|
33
|
34
|
|
|
|
35
|
+ /** The square browser tab and home screen icon made from an uploaded logo, in pixels. */
|
|
|
36
|
+ public const int ICON_SIDE = 180;
|
|
|
37
|
+
|
|
|
38
|
+ /** Uploaded logos wider than this (width to height) are too thin to read as a tab icon. */
|
|
|
39
|
+ private const float ICON_MAX_RATIO = 2.0;
|
|
|
40
|
+
|
|
34
|
41
|
/** How many logos one "Draw with AI" try asks for. */
|
|
35
|
42
|
public const int PER_TRY = 3;
|
|
36
|
43
|
|
|
|
@@ -107,12 +114,104 @@ class StoreLogos
|
|
107
|
114
|
imagefill($canvas, 0, 0, imagecolorallocatealpha($canvas, 0, 0, 0, 127));
|
|
108
|
115
|
imagecopyresampled($canvas, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
109
|
116
|
|
|
110
|
|
- $path = 'logos/'.$store->id.'-'.Str::random(12).'.webp';
|
|
|
117
|
+ $name = 'logos/'.$store->id.'-'.Str::random(12);
|
|
111
|
118
|
ob_start();
|
|
112
|
119
|
imagewebp($canvas, null, 90);
|
|
113
|
|
- Storage::disk('public')->put($path, (string) ob_get_clean());
|
|
|
120
|
+ Storage::disk('public')->put($name.'.webp', (string) ob_get_clean());
|
|
|
121
|
+
|
|
|
122
|
+ $icon = $this->squareIcon($canvas);
|
|
|
123
|
+
|
|
|
124
|
+ if ($icon !== null) {
|
|
|
125
|
+ Storage::disk('public')->put($name.'-icon.png', $icon);
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ $this->replace($store, array_filter([
|
|
|
129
|
+ 'type' => 'upload',
|
|
|
130
|
+ 'path' => $name.'.webp',
|
|
|
131
|
+ 'icon_path' => $icon !== null ? $name.'-icon.png' : null,
|
|
|
132
|
+ 'width' => $newWidth,
|
|
|
133
|
+ 'height' => $newHeight,
|
|
|
134
|
+ ], fn ($value) => $value !== null));
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ /**
|
|
|
138
|
+ * The browser tab icon: the uploaded logo when it is square enough to read at a small size,
|
|
|
139
|
+ * otherwise the store's initials on its main colour. The same icon is used for every logo type,
|
|
|
140
|
+ * and for stores with no logo, so a shop never shows our own icon.
|
|
|
141
|
+ *
|
|
|
142
|
+ * @return array{href: string, type: string, touch: string|null}
|
|
|
143
|
+ */
|
|
|
144
|
+ public function icon(Store $store): array
|
|
|
145
|
+ {
|
|
|
146
|
+ $logo = $store->logo;
|
|
|
147
|
+
|
|
|
148
|
+ if (($logo['type'] ?? null) === 'upload' && is_string($logo['icon_path'] ?? null)) {
|
|
|
149
|
+ $url = Storage::disk('public')->url($logo['icon_path']);
|
|
|
150
|
+
|
|
|
151
|
+ return ['href' => $url, 'type' => 'image/png', 'touch' => $url];
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ $brand = StoreBrand::for($store);
|
|
|
155
|
+ $initials = $this->initials($brand->name);
|
|
|
156
|
+ $letters = htmlspecialchars($initials, ENT_XML1);
|
|
|
157
|
+ $size = mb_strlen($initials) > 1 ? 28 : 36;
|
|
|
158
|
+ $round = ($logo['type'] ?? null) === 'design' && ($logo['key'] ?? null) === 'monogram-circle';
|
|
|
159
|
+ $radius = $round ? 32 : min(32, (int) round($brand->radius * 1.5));
|
|
|
160
|
+ $font = htmlspecialchars($brand->headingFont, ENT_XML1 | ENT_QUOTES);
|
|
|
161
|
+
|
|
|
162
|
+ $svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">'
|
|
|
163
|
+ .'<rect width="64" height="64" rx="'.$radius.'" fill="'.$brand->color('primary').'"/>'
|
|
|
164
|
+ .'<text x="32" y="33" text-anchor="middle" dominant-baseline="central" font-family="'.$font.'"'
|
|
|
165
|
+ .' font-size="'.$size.'" font-weight="700" fill="'.$brand->color('primaryForeground').'">'.$letters.'</text>'
|
|
|
166
|
+ .'</svg>';
|
|
|
167
|
+
|
|
|
168
|
+ return ['href' => 'data:image/svg+xml,'.rawurlencode($svg), 'type' => 'image/svg+xml', 'touch' => null];
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ /**
|
|
|
172
|
+ * Up to two letters: the first letters of the first two words. Keep in step with initials() in logo.tsx.
|
|
|
173
|
+ */
|
|
|
174
|
+ public function initials(string $name): string
|
|
|
175
|
+ {
|
|
|
176
|
+ $words = preg_split('/\s+/u', trim((string) preg_replace('/[^\p{L}\p{N}\s]/u', ' ', $name)), -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
|
|
177
|
+
|
|
|
178
|
+ $letters = count($words) > 1
|
|
|
179
|
+ ? mb_substr($words[0], 0, 1).mb_substr($words[1], 0, 1)
|
|
|
180
|
+ : mb_substr($words[0] ?? '?', 0, 2);
|
|
|
181
|
+
|
|
|
182
|
+ return mb_strtoupper($letters);
|
|
|
183
|
+ }
|
|
|
184
|
+
|
|
|
185
|
+ /**
|
|
|
186
|
+ * The logo on a square see-through canvas, with plain edges (see-through or one solid colour) cut off first. Null when the logo is too wide to read.
|
|
|
187
|
+ */
|
|
|
188
|
+ private function squareIcon(GdImage $image): ?string
|
|
|
189
|
+ {
|
|
|
190
|
+ $trimmed = @imagecropauto($image, IMG_CROP_SIDES);
|
|
|
191
|
+ $image = $trimmed instanceof GdImage ? $trimmed : $image;
|
|
|
192
|
+ $width = imagesx($image);
|
|
|
193
|
+ $height = imagesy($image);
|
|
|
194
|
+
|
|
|
195
|
+ if ($width / max(1, $height) > self::ICON_MAX_RATIO || $height / max(1, $width) > self::ICON_MAX_RATIO) {
|
|
|
196
|
+ return null;
|
|
|
197
|
+ }
|
|
|
198
|
+
|
|
|
199
|
+ // A little room around the logo, so it does not touch the edges of the tab icon.
|
|
|
200
|
+ $inner = (int) round(self::ICON_SIDE * 0.9);
|
|
|
201
|
+ $scale = min($inner / $width, $inner / $height);
|
|
|
202
|
+ $newWidth = max(1, (int) round($width * $scale));
|
|
|
203
|
+ $newHeight = max(1, (int) round($height * $scale));
|
|
|
204
|
+
|
|
|
205
|
+ $canvas = imagecreatetruecolor(self::ICON_SIDE, self::ICON_SIDE);
|
|
|
206
|
+ imagealphablending($canvas, false);
|
|
|
207
|
+ imagesavealpha($canvas, true);
|
|
|
208
|
+ imagefill($canvas, 0, 0, imagecolorallocatealpha($canvas, 0, 0, 0, 127));
|
|
|
209
|
+ imagecopyresampled($canvas, $image, intdiv(self::ICON_SIDE - $newWidth, 2), intdiv(self::ICON_SIDE - $newHeight, 2), 0, 0, $newWidth, $newHeight, $width, $height);
|
|
|
210
|
+
|
|
|
211
|
+ ob_start();
|
|
|
212
|
+ imagepng($canvas, null, 9);
|
|
114
|
213
|
|
|
115
|
|
- $this->replace($store, ['type' => 'upload', 'path' => $path, 'width' => $newWidth, 'height' => $newHeight]);
|
|
|
214
|
+ return (string) ob_get_clean();
|
|
116
|
215
|
}
|
|
117
|
216
|
|
|
118
|
217
|
public function useDesign(Store $store, string $key): void
|
|
|
@@ -192,7 +291,7 @@ class StoreLogos
|
|
192
|
291
|
$store->save();
|
|
193
|
292
|
|
|
194
|
293
|
if (($old['type'] ?? null) === 'upload' && ($old['path'] ?? null) !== ($logo['path'] ?? null)) {
|
|
195
|
|
- Storage::disk('public')->delete($old['path']);
|
|
|
294
|
+ Storage::disk('public')->delete(array_filter([$old['path'], $old['icon_path'] ?? null]));
|
|
196
|
295
|
}
|
|
197
|
296
|
}
|
|
198
|
297
|
|