- 論壇徽章:
- 0
|
生成高質(zhì)量的縮略圖函數(shù)
createDst(原圖片地址,縮略圖最大寬度,縮略圖最大高度 ,生成縮略圖地址)
*/
function createDst ($img_url,$max_width,$max_height,$dst_url='''')
{
if ( empty( $dst_url ) )//如果 $dst_url 參數(shù)未賦值的話,則縮略圖生成在原圖片所在的文件夾
{
$sub_url = substr($img_url,0,strrpos( $img_url,"." ));
$dst_url = $sub_url . "_dst.jpg";
}
if(!file_exists($img_url))
{
die("圖片不存在");
}
$img_src = file_get_contents($img_url);
$image = ImageCreateFromString( $img_src );//用該方法獲得圖象,可以避免“圖片格式”的問題。
$width = imageSx( $image );
$height = imageSy( $image );
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if ( ($width
/*生成高質(zhì)量的縮略圖方法*/
$dst = imagecreatetruecolor($tn_width,$tn_height);//新建一個(gè)真彩色圖像
imagecopyresampled($dst, $image, 0, 0, 0, 0,$tn_width,$tn_height,$width,$height);
//header(''Content-type: image/jpeg'');
ImageJpeg($dst, $dst_url);
ImageDestroy($image);
ImageDestroy($dst);
if(!file_exists($dst_url))
return FALSE;
else
return basename($dst_url);
}
//example 1:
createDst("test.jpg",200,200);
?>
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/12569/showart_64402.html |
|