高清晰GD缩略图生成方法
--------------------------------------------------------------------------------
gd、gd2都适用的写法。
原图
缩略图
--------------------------------------------------------------------------------
gd、gd2都适用的写法。
<?php
$image = "vintdev.JPG"; // 原图
$thumbw = 200; // 期望的目标图宽
$thumbh = 50; // 期望的目标图高
$size = getimagesize($image); // 获取原图大小
$scale = min($thumbw/$size[0], $thumbh/$size[1]); // 计算缩放比例
$width = (int)($size[0]*$scale);
$height = (int)($size[1]*$scale);
$deltaw = (int)(($thumbw - $width)/2);
$deltah = (int)(($thumbh - $height)/2);
$src_img = ImageCreateFromJPEG($image); // 载入原图
if(function_exists("imagecreatetruecolor"))
$dst_img = imagecreatetruecolor($thumbw, $thumbh); // 创建目标图
else
$dst_img = imagecreate($thumbw, $thumbh); // 创建目标图
$back = ImageColorAllocate($dst_img, 255,255,255); // 填充的背景色
imagefill($dst_img,0,0,$back);
if(function_exists("ImageCopyResampled"))
ImageCopyResampled($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img),ImageSY($src_img)); // 复制图片
else
ImageCopyResized($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img),ImageSY($src_img)); // 复制图片
imagejpeg($dst_img,"aaa_2.jpg"); // 创建图片
imagepng($dst_img,"aaa_2.png"); // 创建图片
imagepng($src_img,"ipcover_org.png");
ImageDestroy($src_img);
ImageDestroy($dst_img);
?>
对比$image = "vintdev.JPG"; // 原图
$thumbw = 200; // 期望的目标图宽
$thumbh = 50; // 期望的目标图高
$size = getimagesize($image); // 获取原图大小
$scale = min($thumbw/$size[0], $thumbh/$size[1]); // 计算缩放比例
$width = (int)($size[0]*$scale);
$height = (int)($size[1]*$scale);
$deltaw = (int)(($thumbw - $width)/2);
$deltah = (int)(($thumbh - $height)/2);
$src_img = ImageCreateFromJPEG($image); // 载入原图
if(function_exists("imagecreatetruecolor"))
$dst_img = imagecreatetruecolor($thumbw, $thumbh); // 创建目标图
else
$dst_img = imagecreate($thumbw, $thumbh); // 创建目标图
$back = ImageColorAllocate($dst_img, 255,255,255); // 填充的背景色
imagefill($dst_img,0,0,$back);
if(function_exists("ImageCopyResampled"))
ImageCopyResampled($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img),ImageSY($src_img)); // 复制图片
else
ImageCopyResized($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img),ImageSY($src_img)); // 复制图片
imagejpeg($dst_img,"aaa_2.jpg"); // 创建图片
imagepng($dst_img,"aaa_2.png"); // 创建图片
imagepng($src_img,"ipcover_org.png");
ImageDestroy($src_img);
ImageDestroy($dst_img);
?>
原图
<img src=vintdev.JPG>
缩略图
<img src=aaa_2.png><img src=aaa_2.jpg>
Iris + 注册机
FMS 视频教程

2006/04/12 09:36 | by 



