/**
* 按比例缩略图片
*/
public function actionThumbnail()
{
//调整php分配内存大小,避免出现处理图像的内存不足
if(intval(ini_get('memory_limit')) <= 20){
ini_set('memory_limit','20M');
}
$imgfile=dirname(dirname(dirname(dirname(__FILE__)))).'\web\upload_pic\test.jpg';
//获取图片信息
list($imgw, $imgh, $imgt, $attr) = getimagesize($imgfile);
//计算缩小的比例,目标最长边缩至150
$percent = $imgw>$imgh?(1000/$imgw):(1500/$imgh); //以最长边作为缩放参考
if($percent<1){ //计算缩略图的新尺寸
$new_width = floor($imgw*$percent);
$new_height = floor($imgh*$percent);
}else{ //如果原图尺寸小于 150x150 直接输出原图尺寸
$new_width = $imgw;
$new_height = $imgh;
}
$thumb=imagecreatetruecolor($new_width,$new_height);
//读取图片
switch($imgt){ //判断格式,图像类型,但缩略图输出的都是jpg..参考下文
case 1:
$orgimg=imagecreatefromgif($imgfile);
break;
case 2:
$orgimg=imagecreatefromjpeg($imgfile);
break;
case 3:
$orgimg=imagecreatefrompng($imgfile);
break;
}
header('Content-type: image/jpeg'); //指定header ,告诉浏览器输出的文件为jpg
//imagecopyresampled(缩略图片资源, 源图片资源, dx, dy, sx,sy, 新图像宽度, 新图像高度, 源图像宽度, 源图像高度);
imagecopyresampled($thumb, $orgimg, 0, 0, 0, 0, $new_width, $new_height, $imgw, $imgh); //缩放核心函数
imagejpeg($thumb,'./upload_pic/Thumb/thumb.jpg',100); //输出图像
//销毁资源
imagedestroy($thumb);
imagedestroy($orgimg);
}