|
@@ -494,4 +494,64 @@ function curl_get($url)
|
|
|
$result = curl_exec($ch);
|
|
|
curl_close($ch);
|
|
|
return $result;
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+function createThumbnail($sourceFile, $thumbnailFile, $thumbnailWidth = 100, $quality = 80) {
|
|
|
+ // 获取原图像信息
|
|
|
+ list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceFile);
|
|
|
+ $sourceMime = image_type_to_mime_type($sourceType);
|
|
|
+
|
|
|
+ // 获取原图像的资源
|
|
|
+ switch ($sourceMime) {
|
|
|
+ case 'image/jpeg':
|
|
|
+ $sourceImage = imagecreatefromjpeg($sourceFile);
|
|
|
+ break;
|
|
|
+ case 'image/png':
|
|
|
+ $sourceImage = imagecreatefrompng($sourceFile);
|
|
|
+ break;
|
|
|
+ case 'image/gif':
|
|
|
+ $sourceImage = imagecreatefromgif($sourceFile);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new Exception('Unsupported image type.');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算缩放比例
|
|
|
+ $xScale = $thumbnailWidth / $sourceWidth;
|
|
|
+ $scale = min($xScale, 1); // 确保不会超过原图的大小
|
|
|
+
|
|
|
+ // 计算新的尺寸
|
|
|
+ $thumbnailHeight = intval($sourceHeight * $scale);
|
|
|
+ $thumbnailWidth = intval($sourceWidth * $scale);
|
|
|
+
|
|
|
+ // 创建缩略图的资源
|
|
|
+ $thumbnailImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
|
|
|
+
|
|
|
+ // 调整缩略图的透明度 (如果源图像是PNG或GIF)
|
|
|
+ if ($sourceMime == 'image/png' || $sourceMime == 'image/gif') {
|
|
|
+ imagealphablending($thumbnailImage, false);
|
|
|
+ imagesavealpha($thumbnailImage, true);
|
|
|
+ $transparent = imagecolorallocatealpha($thumbnailImage, 255, 255, 255, 127);
|
|
|
+ imagefilledrectangle($thumbnailImage, 0, 0, $thumbnailWidth, $thumbnailHeight, $transparent);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 缩放并保留透明度 (如果需要)
|
|
|
+ imagecopyresampled($thumbnailImage, $sourceImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);
|
|
|
+
|
|
|
+ // 保存缩略图
|
|
|
+ switch ($sourceMime) {
|
|
|
+ case 'image/jpeg':
|
|
|
+ imagejpeg($thumbnailImage, $thumbnailFile, $quality);
|
|
|
+ break;
|
|
|
+ case 'image/png':
|
|
|
+ imagepng($thumbnailImage, $thumbnailFile);
|
|
|
+ break;
|
|
|
+ case 'image/gif':
|
|
|
+ imagegif($thumbnailImage, $thumbnailFile);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 释放内存
|
|
|
+ imagedestroy($sourceImage);
|
|
|
+ imagedestroy($thumbnailImage);
|
|
|
+}
|