yii2 缩略图功能的实现
来源:互联网 发布:声优训练软件 编辑:程序博客网 时间:2024/05/18 13:10
yii2 缩略图功能
这是缩略图的代码,我这里并没有直接去作为一个插件去使用,而是作为一个models 来使用
<?phpnamespace app\models; // 这里的命名空间根据自己的配置,自己来修改use Yii;/** * This is the model class for table "information". * * @property integer $id * @property string $name * @property string $icon * @property string $bigImage * @property integer $mbuyId * @property string $smallTitle * @property string $content * @property integer $browseNum * @property integer $praiseNum * @property integer $commentNum * @property integer $status * @property integer $recommend * @property integer $recitems * @property string $createtime * @property string $remarks * @property string $username */class SmallImage extends \yii\db\ActiveRecord{ /** * @inheritdoc */ private $src; private $imageinfo; private $image; public $percent = 0.1; public function __construct($src){ $this->src = $src; } public static function tableName() { return 'information'; } /** * @inheritdoc */ /** * @inheritdoc */ public function openImage(){ list($width, $height, $type, $attr) = getimagesize($this->src); $this->imageinfo = array( 'width'=>$width, 'height'=>$height, 'type'=>image_type_to_extension($type,false), 'attr'=>$attr ); $fun = 'imagecreatefrom'.$this->imageinfo['type']; $this->image = $fun($this->src); } /** 操作图片 */ public function thumpImage(){ $new_width = $this->imageinfo['width'] * $this->percent; $new_height = $this->imageinfo['height'] * $this->percent; $image_thump = imagecreatetruecolor($new_width,$new_height); //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度 imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']); imagedestroy($this->image); $this->image = $image_thump; } /** 输出图片 */ public function showImage(){ header('Content-Type: image/'.$this->imageinfo['type']); $funcs = "image".$this->imageinfo['type']; $funcs($this->image); } /** 保存图片到硬盘 */ public function saveImage($sname){ $funcs = "image".$this->imageinfo['type']; $funcs($this->image,$sname.$this->imageinfo['type']); } /** 销毁图片 */ public function __destruct(){ imagedestroy($this->image); }}把上面代码放入models 之后,然后就可以在控制器里面写代码了这里是我的控制器的代码$image = new SmallImage($src); //实例化图片models$image->percent = 0.5625; // 这里是宽高比,根据自己需要来调整$image->openImage(); //打开图片$image->thumpImage();//生成缩略图$name = time();$sname = 'uploads/small/'.$name.'.'; //这是我自己写的路径$mname = Yii::$app->request->hostInfo.Yii::$app->request->baseUrl.'/'.'uploads/small/'.$name.'.'. //这里是写的一个路径,为了缩略图的 'jpeg';$image->saveImage($sname); //保存缩略图到上述的路径下 $model->icon=$mname; //这里是yii2的保存
喜欢的可以收藏下,我会不定时更新一些实用的东西
0 0
- yii2 缩略图功能的实现
- yii2-分页,Yii2实现带搜索的分页功能
- yii2.0 验证码功能的实现
- dedecms实现图集的缩略图功能
- js实现缩略图功能的源代码
- yii2 实现购物车功能
- yii2.0使用插件实现一个简单的上传功能
- 学习记录—yii2简单的登录功能实现
- Yii2实现让关联字段支持搜索功能的方法
- wordpress日志缩略图功能实现
- Eclipse RCP中实现资源管理器的缩略图、平铺功能
- 缩略图的实现
- datagrid+yii2实现数据查询分页功能
- Yii2.0登录功能代码实现
- Yii2.0实现验证码功能
- yii2框架实现引导安装功能
- yii2 使用mysql ,实现点赞功能
- Yii2.0实现语言包切换功能
- PHP中file_put_contents追加和换行
- Android图片加载框架Glide用法
- LeetCode 69. Sqrt(x)
- 自动化之AccessibilityService原理
- #1062 : 最近公共祖先·一
- yii2 缩略图功能的实现
- 高并发web系统调优(十)数据读写分离和廉价存储方案
- 2016-12-14
- Swagger - 前后端分离后的契约
- Python-Dictionary-创建字典的方式
- SS安卓无效电脑有效
- git 步骤记录
- EAS BOS 常用代码
- 一个比较有趣的c语言程序纠错,大家都可以学习下哦