|
|
@@ -24,17 +24,18 @@ class ImgController {
|
|
|
*/
|
|
|
public function compressedImage($imgsrc, $imgdst) {
|
|
|
|
|
|
- list($width, $height, $type) = getimagesize($imgsrc);
|
|
|
+ list($width, $height, $type) = getimagesize($imgsrc); //list可以将返回的数组值赋值给变量
|
|
|
|
|
|
+
|
|
|
$new_width = $width;//压缩后的图片宽
|
|
|
$new_height = $height;//压缩后的图片高
|
|
|
-
|
|
|
- if($width >= 600){
|
|
|
- $per = 600 / $width;//计算比例
|
|
|
- $new_width = $width * $per;
|
|
|
- $new_height = $height * $per;
|
|
|
- }
|
|
|
- // echo $type.':'.$width.":".$height;die();
|
|
|
+ // 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);
|
|
|
@@ -44,33 +45,90 @@ class ImgController {
|
|
|
$image = imagecreatefromgif($imgsrc);
|
|
|
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
|
|
//90代表的是质量、压缩图片容量大小
|
|
|
- imagejpeg($image_wp, $imgdst, 100);
|
|
|
+ imagejpeg($image_wp, $imgdst, 90);
|
|
|
imagedestroy($image_wp);
|
|
|
imagedestroy($image);
|
|
|
}
|
|
|
break;
|
|
|
case 2:
|
|
|
- header('Content-Type:image/jpeg');
|
|
|
+ // header('Content-Type:image/png');echo 'fdsfsfs111';die();
|
|
|
$image_wp = imagecreatetruecolor($new_width, $new_height);
|
|
|
- $image = imagecreatefromjpeg($imgsrc);
|
|
|
+ $image = imagecreatefromjpeg($imgsrc);
|
|
|
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
|
|
//90代表的是质量、压缩图片容量大小
|
|
|
- imagejpeg($image_wp, $imgdst, 100);
|
|
|
+ imagejpeg($image_wp, $imgdst, 90);
|
|
|
imagedestroy($image_wp);
|
|
|
imagedestroy($image);
|
|
|
break;
|
|
|
case 3:
|
|
|
- header('Content-Type:image/png');
|
|
|
+ // 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, 100);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
?>
|