File manager - Edit - /var/www/html/portalHomolog/app/Support/MobiangraManifestacaoImageProcessor.php
Back
<?php namespace App\Support; use App\Helpers\FileUploadHelper; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use RuntimeException; /** * Redimensiona e comprime imagens de manifestações MobiAngra até o limite configurado. */ class MobiangraManifestacaoImageProcessor { private const ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png', 'webp']; private const ALLOWED_MIME_TYPES = [ 'image/jpeg', 'image/png', 'image/webp', ]; private const MAX_DIMENSION = 1920; private const MIN_DIMENSION = 480; /** * Valida, otimiza e grava a imagem no disco public. * * @return string|null Caminho relativo no disco public (ex.: mobiangra/manifestacoes/uuid.jpg) */ public static function storeOptimized(UploadedFile $file, string $directoryWithinPublicDisk, ?int $maxKb = null): ?string { if (! FileUploadHelper::validateFile($file, self::ALLOWED_EXTENSIONS, self::ALLOWED_MIME_TYPES)) { return null; } $maxKb = $maxKb ?? (int) config('mobiangra.manifestacao_imagem_max_kb', 2048); $targetBytes = max(1, $maxKb) * 1024; if (! extension_loaded('gd')) { return self::storeWithoutGd($file, $directoryWithinPublicDisk, $targetBytes); } $source = @file_get_contents($file->getRealPath() ?: ''); if ($source === false || $source === '') { return null; } $image = @imagecreatefromstring($source); if ($image === false) { return null; } $width = imagesx($image); $height = imagesy($image); $image = self::resizeToMaxDimension($image, $width, $height); $binary = self::encodeToTargetSize($image, $targetBytes); if ($binary === null) { throw new RuntimeException(__('Não foi possível reduzir a imagem ao tamanho permitido. Tente outra foto.')); } $dir = trim(str_replace('\\', '/', $directoryWithinPublicDisk), '/'); $path = $dir.'/'.Str::uuid()->toString().'.jpg'; if (! Storage::disk('public')->put($path, $binary)) { return null; } return $path; } private static function storeWithoutGd(UploadedFile $file, string $directoryWithinPublicDisk, int $targetBytes): ?string { if ($file->getSize() > $targetBytes) { Log::warning('[MobiAngra] Extensão GD indisponível e imagem excede o limite', [ 'size' => $file->getSize(), 'target' => $targetBytes, ]); return null; } try { $fileName = Str::uuid()->toString().'.'.strtolower($file->getClientOriginalExtension() ?: 'jpg'); $dir = trim(str_replace('\\', '/', $directoryWithinPublicDisk), '/'); $path = Storage::disk('public')->putFileAs($dir, $file, $fileName); return $path ?: null; } catch (\Throwable $e) { Log::error('[MobiAngra] Falha ao salvar imagem sem GD: '.$e->getMessage()); return null; } } /** * @return \GdImage */ private static function resizeToMaxDimension(\GdImage $image, int $width, int $height): \GdImage { $longest = max($width, $height); if ($longest <= self::MAX_DIMENSION) { return $image; } $ratio = self::MAX_DIMENSION / $longest; $newWidth = (int) round($width * $ratio); $newHeight = (int) round($height * $ratio); return self::scaleImage($image, $newWidth, $newHeight) ?? $image; } /** * @return \GdImage|null */ private static function scaleImage(\GdImage $image, int $newWidth, int $newHeight): ?\GdImage { $scaled = imagecreatetruecolor($newWidth, $newHeight); if ($scaled === false) { return null; } imagecopyresampled( $scaled, $image, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($image), imagesy($image) ); imagedestroy($image); return $scaled; } private static function encodeToTargetSize(\GdImage $image, int $targetBytes): ?string { $qualities = [85, 75, 65, 55, 45, 35, 25]; $lastBinary = null; $current = $image; try { while (true) { foreach ($qualities as $quality) { ob_start(); imagejpeg($current, null, $quality); $binary = ob_get_clean(); if ($binary === false) { continue; } $lastBinary = $binary; if (strlen($binary) <= $targetBytes) { return $binary; } } $width = imagesx($current); $height = imagesy($current); $longest = max($width, $height); if ($longest <= self::MIN_DIMENSION) { break; } $newWidth = max(self::MIN_DIMENSION, (int) round($width * 0.85)); $newHeight = max(self::MIN_DIMENSION, (int) round($height * 0.85)); $scaled = self::scaleImage($current, $newWidth, $newHeight); if ($scaled === null) { break; } $current = $scaled; } return $lastBinary; } finally { imagedestroy($current); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.04 |
proxy
|
phpinfo
|
Settings