php生成图片缩略图功能示例
本文实例讲述了php生成图片缩略图功能。分享给大家供大家参考,具体如下:
完整代码如下(为方便阅读,代码使用http://tools.jb51.net/code/phpformat进行了格式化处理):
<?php /* *Createdon2011-3-18 * *Tochangethetemplateforthisgeneratedfilegoto *Window-Preferences-PHPeclipse-PHP-CodeTemplates */ /*构造函数-生成缩略图+水印,参数说明: $srcFile-图片文件名, $dstFile-另存文件名, $markwords-水印文字, $markimage-水印图片, $dstW-图片保存宽度, $dstH-图片保存高度, $rate-图片保存品质*/ makethumb("1.jpg","aa/b.jpg","50","50"); functionmakethumb($srcFile,$dstFile,$dstW,$dstH,$rate=100,$markwords=null,$markimage=null){ $data=GetImageSize($srcFile); switch($data[2]){ case1: $im=@ImageCreateFromGIF($srcFile); break; case2: $im=@ImageCreateFromJPEG($srcFile); break; case3: $im=@ImageCreateFromPNG($srcFile); break; } if(!$im)returnFalse; $srcW=ImageSX($im); $srcH=ImageSY($im); $dstX=0; $dstY=0; if($srcW*$dstH>$srcH*$dstW){ $fdstH=round($srcH*$dstW/$srcW); $dstY=floor(($dstH-$fdstH)/2); $fdstW=$dstW; }else{ $fdstW=round($srcW*$dstH/$srcH); $dstX=floor(($dstW-$fdstW)/2); $fdstH=$dstH; } $ni=ImageCreateTrueColor($dstW,$dstH); $dstX=($dstX<0)?0:$dstX; $dstY=($dstX<0)?0:$dstY; $dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX; $dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY; $white=ImageColorAllocate($ni,255,255,255); $black=ImageColorAllocate($ni,0,0,0); imagefilledrectangle($ni,0,0,$dstW,$dstH,$white);//填充背景色 ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH); if($markwords!=null){ $markwords=iconv("gb2312","UTF-8",$markwords); //转换文字编码 ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$markwords);//写入文字水印 //参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容 }elseif($markimage!=null){ $wimage_data=GetImageSize($markimage); switch($wimage_data[2]){ case1: $wimage=@ImageCreateFromGIF($markimage); break; case2: $wimage=@ImageCreateFromJPEG($markimage); break; case3: $wimage=@ImageCreateFromPNG($markimage); break; } imagecopy($ni,$wimage,500,560,0,0,88,31);//写入图片水印,水印图片大小默认为88*31 imagedestroy($wimage); } ImageJpeg($ni,$dstFile,$rate); ImageJpeg($ni,$srcFile,$rate); imagedestroy($im); imagedestroy($ni); } ?>
PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:
在线图片转换BASE64工具:
http://tools.jb51.net/transcoding/img2base64
ICO图标在线生成工具:
http://tools.jb51.net/aideddesign/ico_img
在线Email邮箱图标制作工具:
http://tools.jb51.net/email/emaillogo
在线图片格式转换(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。