This commit is contained in:
2026-07-08 15:39:43 +08:00
parent 1bca442f55
commit 614f21b240
+39 -4
View File
@@ -38,7 +38,7 @@ class File
4 => 'No file was uploaded.',
6 => 'Missing a temporary folder.',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.'
8 => 'A PHP extension stopped the file upload.',
];
/**
@@ -65,14 +65,19 @@ class File
*/
public function rename(): string
{
if (!empty($this->newName)) return $this->newName;
if (!empty($this->newName))
return $this->newName;
if (!file_exists($this->getTmpPath())) {
throw new Exception('(' . $this->name . ')Failed to open stream: No such file or directory');
}
$hash = md5_file($this->getTmpPath());
$later = '.' . exif_imagetype($this->getTmpPath());
$later = match ($this->type) {
'image/jpeg', 'image/jpg' => '.jpeg',
'image/gif' => '.gif',
'image/png' => '.png',
default => '.' . $this->exif_imageType(),
};
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
@@ -80,6 +85,36 @@ class File
return $this->name = strtoupper($tmp) . $later;
}
/**
* @return int|null
* @throws Exception
*/
private function exif_imageType(): ?int
{
return match (\exif_imagetype($this->tmp_name)) {
IMAGETYPE_GIF => '.gif',
IMAGETYPE_JPEG => '.jpg',
IMAGETYPE_PNG => '.png',
IMAGETYPE_SWF => '.swf',
IMAGETYPE_PSD => '.psd',
IMAGETYPE_BMP => '.bmp',
IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM => '.tiff',
IMAGETYPE_JPC => '.jpc',
IMAGETYPE_JP2 => '.jp2',
IMAGETYPE_JPX => '.jpx',
IMAGETYPE_JB2 => '.jb2',
IMAGETYPE_SWC => '.swc',
IMAGETYPE_IFF => '.iff',
IMAGETYPE_WBMP => '.wbmp',
IMAGETYPE_XBM => '.xbm',
IMAGETYPE_ICO => '.ico',
IMAGETYPE_WEBP => '.webp',
IMAGETYPE_AVIF => '.avif',
default => throw new Exception('未知的图片类型'),
};
}
/**
* @return string
*/