PHP-GD 缩图失真解决方案

Home Home
引用 | 编辑 月光
2007-05-14 20:36
楼主
推文 x0
复制程式
<?php
// 连结到这页的图片语法:<img src="share_resize.php?cn=$imgfile" alt="点选图片看大图" border="0">
$filename =htmlspecialchars($HTTP_GET_VARS['cn']);
//echo $filename;
//echo exit;
$subname = substr($filename, strrpos($filename, ".")+1); //读取附档名
//echo $subname;
//echo exit;
$photo_path = "../../../upload/o/o05/".$filename; // 相片档案的存放路径
//echo $photo_path;
//echo exit;
// 依副档名来判断相片的图形格式
switch ( strtoupper($subname) ) {
case "JPEG":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 传 Header 给浏览器,告知图档格式
$src_img = ImageCreateFromJPEG($photo_path); // 视原图的格式,用不同的方法读取原图

// 定义一个新的图档
// 将缩图的尺寸设为原图的 20%
$new_W = ImageSX($src_img) * 0.2; //取得图形的宽度
$new_H = ImageSY($src_img) * 0.2; //取得图形的高度
if ( $new_W > 150 ) { // 不让缩图的宽度多于 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//旧版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 将原图复制、调整尺寸,再贴在新图上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//旧版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img); // 视原图的格式,用不同的方法将所建立的新图档输出
break;
case "jpeg":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 传 Header 给浏览器,告知图档格式
$src_img = ImageCreateFromJPEG($photo_path); // 视原图的格式,用不同的方法读取原图

// 定义一个新的图档
// 将缩图的尺寸设为原图的 20%
$new_W = ImageSX($src_img) * 0.2; //取得图形的宽度
$new_H = ImageSY($src_img) * 0.2; //取得图形的高度
if ( $new_W > 150 ) { // 不让缩图的宽度多于 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//旧版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 将原图复制、调整尺寸,再贴在新图上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//旧版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img); // 视原图的格式,用不同的方法将所建立的新图档输出
break;
case "JPG":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 传 Header 给浏览器,告知图档格式
$src_img = ImageCreateFromJPEG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得图形的宽度
$new_H = ImageSY($src_img) * 0.2; //取得图形的高度
if ( $new_W > 150 ) { // 不让缩图的宽度多于 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}


//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//旧版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 将原图复制、调整尺寸,再贴在新图上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//旧版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img);
break;
case "jpg":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 传 Header 给浏览器,告知图档格式
$src_img = ImageCreateFromJPEG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得图形的宽度
$new_H = ImageSY($src_img) * 0.2; //取得图形的高度
if ( $new_W > 150 ) { // 不让缩图的宽度多于 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//旧版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 将原图复制、调整尺寸,再贴在新图上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//旧版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img);
break;
case "JPE":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 传 Header 给浏览器,告知图档格式
$src_img = ImageCreateFromJPEG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得图形的宽度
$new_H = ImageSY($src_img) * 0.2; //取得图形的高度
if ( $new_W > 150 ) { // 不让缩图的宽度多于 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//旧版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 将原图复制、调整尺寸,再贴在新图上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//旧版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img);
break;
case "GIF":
$img_Type = "GIF";
header("Content-type: image/" . $img_Type); // 传 Header 给浏览器,告知图档格式
$src_img = ImageCreateFromGIF($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得图形的宽度
$new_H = ImageSY($src_img) * 0.2; //取得图形的高度
if ( $new_W > 150 ) { // 不让缩图的宽度多于 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//旧版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 将原图复制、调整尺寸,再贴在新图上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//旧版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageGIF($dst_img);
break;
case "PNG":
$img_Type = "PNG";
header("Content-type: image/" . $img_Type); // 传 Header 给浏览器,告知图档格式
$src_img = ImageCreateFromPNG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得图形的宽度
$new_H = ImageSY($src_img) * 0.2; //取得图形的高度
if ( $new_W > 150 ) { // 不让缩图的宽度多于 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//旧版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 将原图复制、调整尺寸,再贴在新图上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//旧版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImagePNG($dst_img);
break;
case "png":
$img_Type = "PNG";
header("Content-type: image/" . $img_Type); // 传 Header 给浏览器,告知图档格式
$src_img = ImageCreateFromPNG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得图形的宽度
$new_H = ImageSY($src_img) * 0.2; //取得图形的高度
if ( $new_W > 150 ) { // 不让缩图的宽度多于 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//旧版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 将原图复制、调整尺寸,再贴在新图上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//旧版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImagePNG($dst_img);
break;
}
// 分别释放 $dst_img 与 $src_img 所占用的储存空间
ImageDestroy($dst_img);
ImageDestroy($src_img);
?> 


献花 x0