Yii 框架生成缩略图
控制器
if($model->load(Yii::$app->request->post()))
{
//原图
$model->img = UploadedFile::getInstance($model, 'img');
$imgname=$model->img->name = time().$model->img->name;
//$model->img->saveAs('./upload/image/' . $model->img->baseName . '.' . $model->img->extension);
//设定上传目录
$path=Yii::$app->basePath.'/web/upload/image/'.$imgname;
//上传原图 图片
$model->img->saveAs($path);
//获取上传原图文件的名称
$image=$model->img;
$img=$image->name;
//echo $img;die;
//利用扩展生成缩略图
$im = null;
//取图片类型
$imagetype = strtolower($model->img->extension);
if($imagetype == 'gif')
$im = imagecreatefromgif($path);
else if ($imagetype == 'jpg')
$im = imagecreatefromjpeg($path);
else if ($imagetype == 'png')
$im = imagecreatefrompng($path);
//按时间生成新文件名
$newName=$model->img->baseName.'_thumb_'.'.'.$model->img->extension;
//设定缩略图的存放目录+名字
$thumb_path=Yii::$app->basePath.'/web/upload/image/image_thumb/'.$newName;
//生成缩略图
$sql=$model->resizeImage($im,800,450,$thumb_path,$model->img->extension);
}
在model 模型层里添加 resizeImage 方法
public static function resizeImage($im, $maxwidth, $maxheight, $name,$filetype) {
$pic_width = imagesx ( $im );
$pic_height = imagesy ( $im );
if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
if ($maxwidth && $pic_width > $maxwidth) {
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}
if ($maxheight && $pic_height > $maxheight) {
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}
if ($resizewidth_tag && $resizeheight_tag) {
if ($widthratio < $heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if ($resizewidth_tag && ! $resizeheight_tag)
$ratio = $widthratio;
if ($resizeheight_tag && ! $resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if (function_exists ( "imagecopyresampled" )) {
$newim = imagecreatetruecolor ( $newwidth, $newheight );
imagecopyresampled ( $newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height );
} else {
$newim = imagecreate ( $newwidth, $newheight );
imagecopyresized ( $newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height );
}
imagejpeg ( $newim, $name );
imagedestroy ( $newim );
} else {
//$name = $name . $filetype;//这句好像多余了
imagejpeg ( $im, $name );
}
}
很简单,,这样就可以了
Yii 框架生成缩略图的更多相关文章
- Yii 图片FTP批量上传 并生成缩略图
图片批量上传,前台使用 uploadify.swf,这个就不介绍了.这里使用两个扩展,一个是FTP上传的扩展,还有一个是生成缩略图的扩展地址:http://www.yiiframework.com/e ...
- YII框架路由和URL生成
路由和URL生成 当一个YII应用开始处理一个请求的时候,它首先要做的便是将请求的URL转化成一个路由.路由的作用是用于后续实例化相应的控制器和操作,以便处理请求,整个处理过程便叫做路由.路由的逆过程 ...
- yii php 图片上传与生成缩略图
今天需要做图片上传与生成缩略图的功能,把代码进行记录如下: html 视图 ($pic_action_url = $this->createAbsoluteUrl('h ...
- PHP.24-TP框架商城应用实例-后台1-添加商品功能、钩子函数、在线编辑器、过滤XSS、上传图片并生成缩略图
添加商品功能 1.创建商品控制器[C] /www.test.com/shop/Admin/Controller/GoodsController.class.php <?php namespace ...
- Yii 框架学习--01 框架入门
Yii 是一个高性能的,适用于开发 WEB2.0 应用的 PHP 框架. Yii目前有两个主要的版本: 2.0 和 1.1.本文以YII 2.0.7为例. 环境需求 Yii2.0 框架有一些系统上的需 ...
- 使用AVFoundation仅仅生成缩略图,不进行播放视频(本地和网络文件都可以创建视频缩略图)
使用MPMoviePlayerController来生成缩略图足够简单,但是如果仅仅是是为了生成缩略图而不进行视频播放的话,此刻使用 MPMoviePlayerController就有点大材小用了.其 ...
- yii框架
Yii Framework是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web 2.0应用开发所需要的几乎一切功能.Yii是最有效率的PHP框架之一.Yii是创始人 ...
- Yii 框架表单验证---实例
- YII框架源码分析(百度PHP大牛创作-原版-无广告无水印)
YII 框架源码分析 百度联盟事业部——黄银锋 目 录 1. 引言 3 1.1.Yii 简介 3 1.2.本文内容与结构 3 2.组件化与模块化 4 2.1.框架加载和运行流程 4 ...
随机推荐
- php会话(session)生命周期概念介绍及设置更改和回收
http://www.169it.com/article/8429580816135935852.html https://my.oschina.net/jiec/blog/227252 sessi ...
- faster rcnn test demo ---repaired for video input and save the image, label, score et al. into .mat format
faster rcnn test demo ---repaired for video input and save the image, label, score et al. into .mat ...
- PostgreSQL: Query for location of global tablespace?
Q: I have been trying to make our database clients pro-active about not filling up the partition on ...
- 查看jquery绑定的事件函数
作为技术狂热分子的职业本能,看到一个技术产品的功能,总会忍不住想知道它是怎么被实现的.比如我每每看到别人网站一个很炫的界面或者很酷的功能,就忍不住打开了浏览器的控制台... 好,不扯远,说说当你想看到 ...
- IE10、IE11 无法写入Cookie
IE10.IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题 你是否遇到过当使用一个涉及到Cookie操作的网站或者管理系统时,IE 6.7.8.9下都跑的好好的, ...
- oracle组件
目前在用的四个oracle版本 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Parti ...
- docker 数据共享,数据复制
docker 提供的数据共享的方式有 docker run -it -v:/dataname image 数据复制使用 docker cp containerid:/dataname ...
- 【转】WMI使用的WIN32_类库名
ShadowBy--Win32_ShadowContext--Win32_ShadowCopy--Win32_ShadowDiffVolumeSupport--Win32_ShadowFor--Win ...
- nginx 注册为服务
使用java service wrapper将java程序注册为windows服务 分类:Java (5677) (8) 将java注册为windows服务后,我们就直接可以通过windows的服务 ...
- 如何通过SecureCRT FTP上传下载文件
通过SecureCRT FTP方式从一台机器下载文件到另一台机器上: [root@TEST144239 ~]# ftp 10.30.1.25 Connected to 10.30.1.25 (10. ...