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()函数.
<?php
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 < $heightratio) {
$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() 慢 .
本文地址:http://www.phprm.com/tuxiang/fs4452.html
转载随意,但请附上文章地址:-)