php 缩略图 封装一个方法
来源:互联网 发布:淘宝客手机可以开通吗 编辑:程序博客网 时间:2024/06/05 14:46
在工作中我们可能要用到一些缩略图,这里我封装了一个方法,希望对大家有用
废话不多说了
<?phpsuolue('big.jpg',800,800);suolue('big.jpg',500,500);suolue('big.jpg',300,300);suolue('big.jpg',100,100);suolue('big.jpg',50,50);function suolue($resuce,$width,$height,$type = 'png',$isfileName=true,$path = "pl"){ //打开图片 $imageRes = open($resuce); //获取图片信息 $imageInfo = getImageInfo($resuce); //进行缩略 $newSize = getNewSize($width,$height,$imageInfo); //解决gif黑色 $newRes = kidOfImage($imageRes,$newSize,$imageInfo); //解决文件随机 if($isfileName){ $name = uniqid().'.'.$type; }else{ $info = pathinfo($path); $name = $info['filename'].'.'.$type; } $path = rtrim($path,'/').'/'.$name; //输出 $func = 'image'.$type; if(!function_exists($func)){ return false; } $func($newRes,$path); //销毁 imagedestroy($newRes); return $path;}function getImageInfo($path){ $info = getimagesize($path); $data['width'] = $info[0]; $data['height'] = $info[1]; return $data;}function open($path){ //判断文件是否存在 if(!file_exists($path)){ return '不存在'; } $info = getimagesize($path); switch($info['mime']){ case 'image/jpg': case 'image/jpeg': case 'image/jpe': case 'image/pjpeg': $res = imagecreatefromjpeg($path); break; case 'image/png': $res = imagecreatefrompng($path); break; case 'image/gif': $res = imagecreatefromgif($path); break; } return $res;}//等比缩放function getNewSize($width, $height,$imgInfo){ //将原图片的宽度给数组中的$size["width"] $size["width"]=$imgInfo["width"]; //将原图片的高度给数组中的$size["height"] $size["height"]=$imgInfo["height"]; if($width < $imgInfo["width"]){ //缩放的宽度如果比原图小才重新设置宽度 $size["width"]=$width; } if($height < $imgInfo["height"]){ //缩放的高度如果比原图小才重新设置高度 $size["height"]=$height; } if($imgInfo["width"]*$size["width"] > $imgInfo["height"] * $size["height"]){ $size["height"]=round($imgInfo["height"]*$size["width"]/$imgInfo["width"]); }else{ $size["width"]=round($imgInfo["width"]*$size["height"]/$imgInfo["height"]); } return $size;}//这两个方法不用会,直接复制就行//处理gif变黑function kidOfImage($srcImg,$size, $imgInfo){ //传入新的尺寸,创建一个指定尺寸的图片 $newImg = imagecreatetruecolor($size["width"], $size["height"]); //定义透明色 $otsc = imagecolortransparent($srcImg); if( $otsc >= 0 && $otsc < imagecolorstotal($srcImg)) { //取得透明色 $transparentcolor = imagecolorsforindex( $srcImg, $otsc ); //创建透明色 $newtransparentcolor = imagecolorallocate( $newImg, $transparentcolor['red'], $transparentcolor['green'], $transparentcolor['blue'] ); //背景填充透明 imagefill( $newImg, 0, 0, $newtransparentcolor ); imagecolortransparent( $newImg, $newtransparentcolor ); } imagecopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] ); imagedestroy($srcImg); //最终新资就解决了透明色的题 return $newImg;}
阅读全文
0 0
- php 缩略图 封装一个方法
- 获取缩略图封装方法
- php 缩略图封装类 制作缩略图
- PHP--封装一个下载图片的方法
- 《PHP中生成缩略图函数封装》
- php生成缩略图的方法
- 一个简单的PHP生成缩略图函数
- 一个简单的PHP生成缩略图函数
- php 封装alert方法
- php 下载封装方法
- php gd库生成缩略图的方法
- PHP生成缩略图的方法和实例
- 封装一个JSONP方法
- 一个上传图片生成缩略图的方法
- php缩略图...
- php缩略图
- PHP 缩略图
- php封装一个接口类
- 凸优化问题
- C++学习笔记之SendInput()模拟键盘输入的问题
- 关于迟到和时间管理-来自东月的一篇检讨
- 深度学习岗位面试问题整理笔记
- Redis配置
- php 缩略图 封装一个方法
- 源码解析-getDefault-register过程
- VS2013MFC中回调函数的使用&出现的问题
- Android Theme 主题总结
- python字符串questions中插入" . "的所有情况
- Latex 同时上下标
- java常用的设计模式
- 高层管理者(笔记)——任务、组织与战略
- 在面板上移动的字