| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\admin\controller;
- use cmf\controller\AdminBaseController;
- use think\Db;
- class ImgController {
- // public function initialize(){
- // parent::initialize();
- // $username = $_SESSION['think']['name'];
- // $userid = $_SESSION['think']['ADMIN_ID'];
- // //判断是否是编辑管理员
- // $iseditor = 0;
- // $iseditor = Db::name('role_user')->where('role_id',3)->where('user_id',$userid)->count();
- // $this->iseditor = $iseditor;
- // $this->uname = $username;
- // $this->assign('iseditor',$iseditor);
- // }
-
- /**
- * desription 压缩图片
- * @param sting $imgsrc 图片路径
- * @param string $imgdst 压缩后保存路径
- */
- public function compressedImage($imgsrc, $imgdst) {
-
- list($width, $height, $type) = getimagesize($imgsrc); //list可以将返回的数组值赋值给变量
-
- $new_width = $width;//压缩后的图片宽
- $new_height = $height;//压缩后的图片高
- // echo $new_width;echo "<br/>";echo $new_height;echo "<br/>";
- // if($width >= 600){
- // $per = 600 / $width;//计算比例
- // $new_width = $width * $per;
- // $new_height = $height * $per;
- // }
- // echo $new_width;echo "<br/>";echo $new_height;die();
- switch ($type) {
- case 1:
- $giftype = check_gifcartoon($imgsrc);
- if ($giftype) {
- header('Content-Type:image/gif');
- $image_wp = imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefromgif($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- //90代表的是质量、压缩图片容量大小
- imagejpeg($image_wp, $imgdst, 90);
- imagedestroy($image_wp);
- imagedestroy($image);
- }
- break;
- case 2:
- // header('Content-Type:image/png');echo 'fdsfsfs111';die();
- $image_wp = imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefromjpeg($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- //90代表的是质量、压缩图片容量大小
- imagejpeg($image_wp, $imgdst, 90);
- imagedestroy($image_wp);
- imagedestroy($image);
- break;
- case 3:
- // header('Content-Type:image/png');
- $image_wp = imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefrompng($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- //90代表的是质量、压缩图片容量大小
- imagejpeg($image_wp, $imgdst, 50);
- imagedestroy($image_wp);
- imagedestroy($image);
- break;
- }
- }
- /**
- * 缩略图
- * @param $file
- * @param int $type
- * @return $this
- */
- public static function imageThumb($file, $type=1, $params=[]){
- $siteInfo = cmf_get_site_info();
- $thumbWidth = isset($siteInfo['thumb_width'])? intval($siteInfo['thumb_width']) : 0;
- $thumbHeight = isset($siteInfo['thumb_height'])? intval($siteInfo['thumb_height']) : 0;
- $width = isset($params['width'])? $params['width'] : 0;
- $height = isset($params['height'])? $params['height'] : 0;
- $thumbWidth = $thumbWidth>600? $thumbWidth : 600;
- $thumbHeight = $thumbHeight>600? $thumbHeight : 600;
- $thumbWidth = $width? $width : $thumbWidth;
- $thumbHeight = $height? $height : $thumbHeight;
- // 要加水印的图片
- $realFilename = 'upload/' . $file;
- if (!file_exists($realFilename)) {
- return false;
- }
- $image = \think\Image::open($realFilename);
- $filepath = dirname($realFilename);
- $paths = explode('_',basename($realFilename));
- $filename = end($paths);
- $thumbfile = $filepath . '/thumb_' . $thumbWidth . '_' . $thumbHeight . '_' . $type . '_' . $filename;
- $image->thumb($thumbWidth, $thumbHeight, $type)->save($thumbfile);
- return preg_replace("/^upload\//", '', $thumbfile);
- }
- //压缩图片2
- public static function imageThumb2($imgsrc, $imgdst, $type=3){
-
- list($width, $height, $type) = getimagesize($imgsrc);
-
- $new_width = $width;//压缩后的图片宽
- $new_height = $height;//压缩后的图片高
-
- // if($width >= 600){
- // $per = 600 / $width;//计算比例
- // $new_width = $width * $per;
- // $new_height = $height * $per;
- // }
- $image = \think\Image::open($imgsrc);
- // $filepath = dirname($imgsrc); //不需要获取根目录
- //$paths = explode('_',basename($realFilename));
- //$filename = end($paths);
- //
- $image->thumb($new_width, $new_height, $type)->save($imgdst);
- return preg_replace("/^upload\//", '', $imgdst);
- }
- }
- ?>
|