init
This commit is contained in:
236 files changed
+24692
No files matched your search
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class Artwork
|
||||
{
|
||||
public function prepare(string $original): string
|
||||
{
|
||||
$disk = Storage::disk('local');
|
||||
$path = $disk->path($original);
|
||||
$info = is_file($path) ? @getimagesize($path) : false;
|
||||
|
||||
if (! $info || $info[2] !== IMAGETYPE_PNG || max($info[0], $info[1]) > 4096 || filesize($path) > 12 * 1024 * 1024) {
|
||||
throw $this->invalid('requirements');
|
||||
}
|
||||
|
||||
$source = @imagecreatefrompng($path);
|
||||
if (! $source) {
|
||||
throw $this->invalid('unreadable');
|
||||
}
|
||||
|
||||
imagepalettetotruecolor($source);
|
||||
imagealphablending($source, false);
|
||||
imagesavealpha($source, true);
|
||||
$artwork = imagecropauto($source, IMG_CROP_TRANSPARENT);
|
||||
if (! $artwork) {
|
||||
throw $this->invalid('empty');
|
||||
}
|
||||
|
||||
$scale = min(1, 1200 / max(imagesx($artwork), imagesy($artwork)));
|
||||
$resized = imagescale($artwork, max(1, (int) round(imagesx($artwork) * $scale)), max(1, (int) round(imagesy($artwork) * $scale)));
|
||||
if (! $resized) {
|
||||
throw $this->invalid('resize_failed');
|
||||
}
|
||||
|
||||
imagealphablending($resized, false);
|
||||
imagesavealpha($resized, true);
|
||||
$file = 'artwork/'.Str::uuid().'.webp';
|
||||
$disk->makeDirectory('artwork');
|
||||
if (! imagewebp($resized, $disk->path($file), 91) || ! $disk->exists($file) || $disk->size($file) === 0) {
|
||||
throw $this->invalid('write_failed');
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
private function invalid(string $reason): ValidationException
|
||||
{
|
||||
return ValidationException::withMessages(['data.original_path' => __('admin.artwork.'.$reason)]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CatalogImage
|
||||
{
|
||||
public const MIME_TYPES = ['image/jpeg', 'image/png', 'image/webp'];
|
||||
|
||||
public function prepare(string $original, string $directory, string $field, bool $square = false): array
|
||||
{
|
||||
$disk = Storage::disk('local');
|
||||
$source = $disk->path($original);
|
||||
$info = is_file($source) ? @getimagesize($source) : false;
|
||||
if (! $info || ! in_array($info['mime'], self::MIME_TYPES, true)
|
||||
|| max($info[0], $info[1]) > 4096 || filesize($source) > 12 * 1024 * 1024) {
|
||||
throw ValidationException::withMessages([$field => __('admin.catalog.invalid_image')]);
|
||||
}
|
||||
|
||||
if ($square && $info[0] !== $info[1]) {
|
||||
throw ValidationException::withMessages([$field => __('admin.garments.square_image')]);
|
||||
}
|
||||
|
||||
$image = @imagecreatefromstring(file_get_contents($source));
|
||||
if (! $image) {
|
||||
throw ValidationException::withMessages([$field => __('admin.artwork.unreadable')]);
|
||||
}
|
||||
|
||||
imagepalettetotruecolor($image);
|
||||
imagealphablending($image, false);
|
||||
imagesavealpha($image, true);
|
||||
|
||||
$file = $directory.'/'.Str::uuid().'.webp';
|
||||
$disk->makeDirectory($directory);
|
||||
if (! imagewebp($image, $disk->path($file), 90) || ! $disk->exists($file) || $disk->size($file) === 0) {
|
||||
throw ValidationException::withMessages([$field => __('admin.artwork.write_failed')]);
|
||||
}
|
||||
|
||||
return ['path' => $file, 'width' => $info[0]];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\Design;
|
||||
use App\Models\GarmentTemplate;
|
||||
|
||||
class GarmentPreview
|
||||
{
|
||||
public function options(Design $design): array
|
||||
{
|
||||
return GarmentTemplate::orderBy('id')->get()->map(function (GarmentTemplate $template) use ($design): array {
|
||||
$placements = [];
|
||||
foreach ($template->placements as $key => $placement) {
|
||||
if ($design->width_cm <= $placement['max_width_cm'] && $design->height_cm <= $placement['max_height_cm']) {
|
||||
$placements[$key] = ['x' => $placement['x'], 'y' => $placement['y']];
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $template->{'name_'.app()->getLocale()},
|
||||
'garment' => $template->garment,
|
||||
'color' => $template->color,
|
||||
'side' => $template->side,
|
||||
'photo' => $template->photoUrl(),
|
||||
'width' => 100 * $design->width_cm * $template->pixels_per_cm / $template->photo_width_px,
|
||||
'placements' => $placements,
|
||||
'illustrative' => $template->illustrative,
|
||||
];
|
||||
})->filter(fn (array $template) => $template['placements'] !== [])->values()->all();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use IntlChar;
|
||||
|
||||
class PhoneNumber
|
||||
{
|
||||
public static function dialString(string $number): string
|
||||
{
|
||||
$digits = preg_replace_callback('/\p{Nd}/u', fn (array $match): string => (string) IntlChar::digit($match[0]), $number);
|
||||
|
||||
return preg_replace('/[^+0-9]/', '', $digits);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user