PHP GD库生成缩略图并保存在指定文件夹,首先确保开启了扩展,下面直接输出代码,其他的就不多讲了
//生成缩略图
/****参数说明:
**1:sponsor:需要操作的图片的存放位置:比如使用的CI框架,这个地址可以是:upload/kv/e7a14bab633cc74f2b83df.png
**2:id唯一值,可以是用项目逻辑主表的主键id
**/
public function pic($params){
//创建文件夹开始
$path_dir = str_replace('\\','/',strstr(__DIR__,'app',true).'upload/invitation/kv/');
if (!is_dir($path_dir)) {
mkdir($path_dir, 0777, true);
}
//创建文件夹结束
//从字符串中的图像流新建图像
$sponsorold = imagecreatefromstring(file_get_contents(ltrim($params['sponsor'],'/')));
//获取需要操作的图片的宽
$sponsorxWidthold = imagesx($sponsorold);
//获取需要操作的图片的搞
$sponsoryWidthold = imagesy($sponsorold);
//新的尺寸,缩小图片指数小于1,方法图片指数大于1
$new_width = $sponsorxWidthold*0.3;
$new_height =$sponsoryWidthold*0.3;
//调动主方法
$this->imagepress($params['sponsor'], $new_width, $new_height,$params['id']);
}
//压缩图片
/****参数说明:
**1:sponsor:需要操作的图片的存放位置:比如使用的CI框架,这个地址可以是:upload/kv/e7a14bab633cc74f2b83df.png
**2:new_width需要缩放的宽
**3:new_height需要缩放的高
**4:id唯一值,可以是用项目逻辑主表的主键id
**/
public function imagepress($filepath, $new_width, $new_height,$id)
{
//获取http协议
if(isset($_SERVER['HTTP_X_CLIENT_SCHEME'])){
$scheme = $_SERVER['HTTP_X_CLIENT_SCHEME'] . '://';
}elseif(isset($_SERVER['REQUEST_SCHEME'])){
$scheme = $_SERVER['REQUEST_SCHEME'] . '://';
}else{
$scheme = 'http://';
}
//域名
$domain = $_SERVER['SERVER_NAME'] ;
//拼接当前需要操作图片所在项目的完整路径
$filepath = $scheme.$domain.$filepath;
//获取图片大小
$source_info = getimagesize($filepath);
//获取图片大小:宽
$source_width = $source_info[0];
//获取图片大小:高
$source_height = $source_info[1];
//获取图片类型
$source_mime = $source_info['mime'];
//图片旧的宽高比
$source_ratio = $source_height / $source_width;
//图片缩放的宽高比
$target_ratio = $new_height / $new_width;
// 图片缩放容错处理:
//源图过高
if ($source_ratio > $target_ratio)
{
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
}
// 源图过宽
elseif ($source_ratio < $target_ratio)
{
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}
// 源图适中
else
{
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
//根据类型获取:imagecreatefromgif函数 接受单个参数$filename来保存图像
switch ($source_mime)
{
case 'image/gif':
$source_image = imagecreatefromgif($filepath);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($filepath);
break;
case 'image/png':
$source_image = imagecreatefrompng($filepath);
break;
default:
return false;
break;
}
//用于创建新的true-color图像,此函数返回给定尺寸的空白图像
$target_image = imagecreatetruecolor($new_width, $new_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 裁剪
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $new_width, $new_height, $cropped_width, $cropped_height);
//输出:(注意imagejpeg使用时,新的图片保存的类型可以根据$source_mime的值灵活来写,我这边要求的是png所以 写死,如果是封装组件的话可以灵活一点)
header('Content-Type: image/jpeg');
imagejpeg($target_image,'./upload/invitation/kv/'.$id.'.png');
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
}