PHP等比缩放并补白
2016-08-10 22:21
183 查看
/** * 图片等比缩放并补白 * @param string $filename 图片路径 * @param number $width 缩放宽度 * @param number $height 缩放高度 * @return $newname 新文件名 */ function zoom($filename,$width=400,$height=400){ //获取图片信息 $info = getimagesize($filename); //判断是不是图片 if(!$info) exit('请处理图片'); $arr = explode('/',$info['mime']); $ext = $arr[1]; //拼接相关函数 打开 保存 $create = 'imagecreatefrom'.$ext; $save = 'image'.$ext; //获取图片原来信息 list($t_w,$t_h) = $info; //要计算图片最终的宽高 //假设用图片的宽度来计算出图片的高度 $des_w = $width; $des_h = $des_w * ($t_h/$t_w); if($des_h > $height){ $des_h = $height; $des_w = $des_h * ($t_w/$t_h); }else{ $des_w = $width; $des_h = $des_w * ($t_h/$t_w); } //创建画布 $huabu = imagecreatetruecolor($width,$height); $white = imagecolorallocate($huabu,255,255,255); imagefill($huabu,0,0,$white); $tupian = $create($filename); //画布起点 //横坐标 $hua_x = ($width-$des_w)/2; $hua_y = ($height-$des_h)/2; imagecopyresampled($huabu,$tupian,$hua_x,$hua_y,0,0,$des_w,$des_h,$t_w,$t_h); if($ext == 'jpeg') $ext = 'jpg'; $newname = uniqid().'.'.$ext; $savepath = rtrim(dirname($filename),'/').'/'.$newname; $save($huabu,$savepath); imagedestroy($huabu); imagedestroy($tupian); return $newname; }
相关文章推荐
- PHP实现图片的等比缩放和Logo水印功能示例
- php按指定大小等比缩放生成上传图片缩略图
- PHP的一个完美GIF等比缩放类,附带去除缩放黑背景
- 常用的php图片处理类(水印、等比缩放、固定高宽)分享
- PHP 等比缩放 缩略图 类
- php和js对数据库图片进行等比缩放示例
- PHP实现图片的等比缩放和Logo水印功能示例
- PHP图片的等比缩放和增加Logo水印详细代码和注释
- PHP图片等比缩放,并添加Logo水印特定代码和盯
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- PHP上传图片进行等比缩放
- PHP普通缩放和等比缩放函数
- 常用的php图片处理类(水印、等比缩放、固定高宽)分享
- PHP上传图片进行等比缩放可增加水印功能
- php等比缩放图片
- php实现按指定大小等比缩放生成上传图片缩略图的方法
- PHP上传图片进行等比缩放可增加水印功能
- php Imagick 实现等比缩放图片
- php等比缩放输出图片
- PHP图像处理函数:文字水印,图片水印,缩略图(等比缩放)