'jpg', 'image/png' => 'png', 'image/webp' => 'webp', ]; return $map[$mime] ?? null; } protected static function imageIsSvgUpload(string $mime, string $path): bool { if ($mime === 'image/svg+xml') { return true; } $head = @file_get_contents($path, false, null, 0, 1024); if (!is_string($head) || $head === '') { return false; } return stripos($head, ' filemtime($a); }); return $matches[0]; } protected static function imageCreateResource(string $path, string $mime) { if ($mime === 'image/jpeg' && function_exists('imagecreatefromjpeg')) { return imagecreatefromjpeg($path); } if ($mime === 'image/png' && function_exists('imagecreatefrompng')) { return imagecreatefrompng($path); } if ($mime === 'image/webp' && function_exists('imagecreatefromwebp')) { return imagecreatefromwebp($path); } return false; } protected static function imageResizeAndFit(string $sourcePath, string $targetPath, int $width, int $height, string $ext): bool { $mime = self::imageDetectMime($sourcePath); $src = self::imageCreateResource($sourcePath, $mime); if (!$src) { return false; } $srcWidth = imagesx($src); $srcHeight = imagesy($src); $scale = min($width / $srcWidth, $height / $srcHeight); $dstWidth = (int) round($srcWidth * $scale); $dstHeight = (int) round($srcHeight * $scale); if ($dstWidth < 1) { $dstWidth = 1; } if ($dstHeight < 1) { $dstHeight = 1; } $dst = imagecreatetruecolor($dstWidth, $dstHeight); if ($ext === 'png' || $ext === 'webp') { imagealphablending($dst, false); imagesavealpha($dst, true); $transparent = imagecolorallocatealpha($dst, 0, 0, 0, 127); imagefilledrectangle($dst, 0, 0, $dstWidth, $dstHeight, $transparent); } imagecopyresampled( $dst, $src, 0, 0, 0, 0, $dstWidth, $dstHeight, $srcWidth, $srcHeight ); $saved = false; if ($ext === 'webp' && function_exists('imagewebp')) { $saved = imagewebp($dst, $targetPath, 82); } elseif ($ext === 'png' && function_exists('imagepng')) { $saved = imagepng($dst, $targetPath, 6); } else { $saved = imagejpeg($dst, $targetPath, 85); } if ($saved) { @chmod($targetPath, 0644); } return $saved; } protected static function imageResizeSquare(string $sourcePath, string $targetPath, int $size): bool { $src = self::imageLoadPng($sourcePath); if (!$src) { return false; } $srcWidth = imagesx($src); $srcHeight = imagesy($src); $cropSize = min($srcWidth, $srcHeight); $srcX = (int) round(($srcWidth - $cropSize) / 2); $srcY = (int) round(($srcHeight - $cropSize) / 2); $dst = imagecreatetruecolor($size, $size); imagealphablending($dst, false); imagesavealpha($dst, true); $transparent = imagecolorallocatealpha($dst, 0, 0, 0, 127); imagefilledrectangle($dst, 0, 0, $size, $size, $transparent); imagecopyresampled( $dst, $src, 0, 0, $srcX, $srcY, $size, $size, $cropSize, $cropSize ); $saved = false; if (function_exists('imagepng')) { $saved = imagepng($dst, $targetPath, 6); } return $saved; } protected static function imageLoadPng(string $path) { if (function_exists('imagecreatefrompng')) { return imagecreatefrompng($path); } return false; } }