本文主要介绍在php中生成高清缩略图的方法。有兴趣的朋友可以参考一下。希望对大家有所帮助。
1。使用 imagecreatetruecolor 和 imageCopyreSampled 函数分别替换 imagecreate 和 imagecopyresized
2。 imagejpeg的第三个参数加100(示例:imagejpeg($ni,$toFile,100))
具体功能如下
函数CreateSmallImage( $OldImagePath, $NewImagePath, $NewWidth=154, $NewHeight=134) { // 取出原图,获取图形信息 getimagesize 参数说明:0(宽度)、1(高度)、2(1gif/2jpg/3png)、3(width="638" height="340") $OldImageInfo = getimagesize($OldImagePath); 如果 ( $OldImageInfo[2] == 1 ) $OldImg = @imagecreatefromgif($OldImagePath); elseif ( $OldImageInfo[2] == 2 ) $OldImg = @imagecreatefromjpeg($OldImagePath); 否则 $OldImg = @imagecreatefrompng($OldImagePath); //创建图形,imagecreate参数说明:width、height $NewImg = imagecreatetruecolor( $NewWidth, $NewHeight ); //创建颜色,参数:图形,红色(0-255),绿色(0-255),蓝色(0-255) $black = ImageColorAllocate( $NewImg, 0, 0, 0 ); // 黑色的 $white = ImageColorAllocate( $NewImg, 255, 255, 255 ); //白色的$red = ImageColorAllocate( $NewImg, 255, 0, 0 ); //红色的 $blue = ImageColorAllocate( $NewImg, 0, 0, 255 ); // 蓝色的 $其他 = ImageColorAllocate( $NewImg, 0, 255, 0 ); //新建图形高宽处理 $WriteNewWidth = $NewHeight*($OldImageInfo[0] / $OldImageInfo[1]); //要写入的高度 $WriteNewHeight = $NewWidth*($OldImageInfo[1] / $OldImageInfo[0]); //要写入的宽度 //这样处理的话图片比例会不平衡,但是可以填充背景 if ($OldImageInfo[0] / $NewWidth > $org_info[1] / $NewHeight) { $WriteNewWidth = $NewWidth; $WriteNewHeight = $NewWidth / ($OldImageInfo[0] / $OldImageInfo[1]); } 别的 { $WriteNewWidth = $NewHeight * ($OldImageInfo[0] / $OldImageInfo[1]); $WriteNewHeight = $NewHeight; } //根据$NewHeight,如果新宽度小于等于$NewWidth则为true 如果( $WriteNewWidth <= $NewWidth ){ $WriteNewWidth = $WriteNewWidth; //使用确定的大小 $WriteNewHeight = $NewHeight; //使用指定的大小 $WriteX = 地板( ($NewWidth-$WriteNewWidth) / 2 ); // 计算新图片上写入的X位置 $WriteY = 0; } 别的 {$WriteNewWidth = $NewWidth; // 使用指定的大小 $WriteNewHeight = $WriteNewHeight; //使用确定的大小 $WriteX = 0; $WriteY = 地板( ($NewHeight-$WriteNewHeight) / 2 ); //计算新图片上写的X位置 } //旧图形缩小后,写入新图形(复制),imagecopyresized参数说明:new和old、新xy和旧xy、新宽高、旧宽高 @imageCopyreSampled( $NewImg, $OldImg, $WriteX, $WriteY, 0, 0, $WriteNewWidth, $WriteNewHeight, $OldImageInfo[0], $OldImageInfo[1] ); //保存文档 // @imagegif( $NewImg, $NewImagePath ); @imagejpeg($NewImg, $NewImagePath, 100); //结束图形 @imagedestroy($NewImg); } CreateSmallImage("./images/jiexie.jpg","./images/jiexie.small.jpg",200,300); CreateSmallImage("./images/jiexie.jpg","./images/jiexie.middle.jpg",400,500);
总结:以上就是本文全部内容了,我希望能够对大家的学习有所帮助。
相关推荐:
PHP分页原理分页代码分页类制作方法实例详解
PHP如何基于glob函数实现遍历文件和目录的详细讲解
PHP版微信公众平台开发验证步骤详解(必读)