PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php创建高清缩略图详细使用方法_PHP教程

原创
2016-07-13 10:45:42 637浏览

php教程创建高清缩略图详细使用方法

1.用imagecreatetruecolor和imagecopyresampled函数分别取代imagecreate和imagecopyresized
2.给imagejpeg的第三个参数带上100(例:imagejpeg($ni,$tofile,100))

imagecreatetruecolor -- 新建一个真彩色图像
说明
resource imagecreatetruecolor ( int x_size, int y_size )
imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像

*/

header ("content-type: image/png");
$im = @imagecreatetruecolor (50, 100)
or die ("cannot initialize new gd image stream");
$text_color = imagecolorallocate ($im, 233, 14, 91);
imagestring ($im, 1, 5, 5, "a simple text string", $text_color);
imagepng ($im);
imagedestroy ($im);

/*


如果使用普通的imagecreate()函数将造成图片质量失真的情况,从网上搜了一下解决办法,方法是用imagecreateruecolor()函数替换imagecreate()函数。
*/

function createpreview($img,$name,$path,$maxwidth,$maxheight,$quality){//图片,保存名称,保存路径,最大宽,最大高,质量
$widthratio=0;
$heightratio=0;
$width=imagesx($img);
$height=imagesy($img);
//开始计算缩小比例
if($width>$maxwidth||$height>$maxheight){
if($width>$maxwidth){
$widthratio=$maxwidth/$width;
}
if($height>$maxheight){
$heightratio=$maxheight/$height;
}
if($widthratio>0&&$heightratio>0){
if($widthratio $ratio=$widthratio;
}else{
$ratio=$heightratio;
}
}elseif($widthratio>0){
$ratio=$widthratio;
}elseif($heightratio>0){
$ratio=$heightratio;
}
//根据得出的比例,重新计算缩略图的宽和高
$newwidth=$ratio*$width;
$newheight=$ratio*$height;
$newimg=imagecreatetruecolor($newwidth,$newheight); // 创建目标图
imagecopyresized($newimg,$img,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($newimg,$path."s_".$name,$quality);
imagedestroy($newimg);
}else{
imagejpeg($img,$path."s_".$name,$quality);
}
}
/*

imagecopyresamples() ,其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比 imagecopyresized() 慢).

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633003.htmlTechArticlephp教程创建高清缩略图详细使用方法 1.用imagecreatetruecolor和imagecopyresampled函数分别取代imagecreate和imagecopyresized 2.给imagejpeg的第三个参数带上...
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。