[TOC]
### 1、先說里面的方法
該類中包含
1.文件上傳upload
2.獲取圖片屬性get_image_attr
3.生成縮略圖create_image_thumb
4.圖片添加水印create_image_water
5.圖片裁剪create_image_crop
6.圖片翻轉create_image_flip
7.圖片旋轉create_image_rotate
8.圖片混合處理image_mixed
### 2、再多一句嘴
里面涉及到創建目錄 引入
~~~
use app\common\controller\Directory
//Directory參考***Directory通用封裝***
~~~
涉及到think\Image 安裝topthink-image
### 3、composer 安裝 topthink/think-image
~~~
composer require topthink/think-image
~~~
### 4、文件上傳upload
~~~
/**
* 文件上傳
*
* @param string $key files配置文件的key值
* @return array
*/
public function upload($key = null): array
{
$file = request()->file('file');
if ($file == null) {
return ['status' => false, 'message' => '請上傳文件'];
}
if ($key == null) {
return ['status' => false, 'message' => 'key不能為空'];
}
$file_path = './uploads/file/' . explode('.', $key)[1] . '/';
if (!is_dir($file_path)) {
(new Directory())->mkdirs($file_path);
}
//獲取配置key
$keys = Config::get('files.' . $key);
$base_info = $file->getInfo();
$info = $file ->validate(['size' => $keys['size'], 'ext'=> $keys['ext']])
->rule($keys['rule'])
->move($file_path);
$get_success = function () use (&$file_path, &$base_info, &$info) {
return [
'status' => true,
'message' => '上傳成功',
'data' => [
'suffix' => $info->getExtension(),
'size' => $base_info['size'],
'file_path' => $file_path . $info->getSaveName(),
'file_name' => $info->getFilename(),
'base_name' => $base_info['name'],
'base_type' => $base_info['type'],
]
];
};
return $info ? $get_success() : ['status' => false, 'message' => $file->getError()];
}
//多句嘴
//該key是在config目錄下的files.php配置文件中
//因為代碼中使用了文件名files,所以只需傳入key即可不帶文件名
//方式如下:upload('default.images.default')
~~~
### 5、插入一段文件上傳配置說明
~~~
// +----------------------------------------------------------------------
// | 文件上傳配置
// +----------------------------------------------------------------------
return [
//默認配置 獲取配置格式參數$this->upload('default.images.default');
'default' => [
'images' => [
//圖片上傳默認配置
'default' => [
'size' => 10485760,
//rule說明,默認提供date/uniqid/sha1/md5 四種生成文件名的方法
//其中,如果選擇date/uniqid 的生成方法的話,相同文件的話不做任何修改
//其他兩種則不管是不是同一個文件都會當成新的文件上傳
'rule' => 'md5',
'ext' => ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'],
],
//值允許上傳png后綴的圖片
'only_png' => [
'size' => 10485760,
'rule' => 'md5',
'ext' => ['png'],
],
],
'only_excel' => [
'size' => 10485760 * 100,
'rule' => 'md5',
'ext' => [
'xls', 'xlsx'
]
],
],
//所有能上傳的文件
'all' => [
'file' => [
'default' => [
'size' => 10485760 * 100,
'rule' => 'sha1',
'ext' => [
//圖片
'gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf', 'pcx', 'tiff', 'tga', 'exif', 'fpx', 'svg', 'psd', 'cdr',
//辦公
'xls', 'xlsx', 'xlsm', 'xlt', 'xltx', 'xltm', 'doc', 'docx', 'ppt', 'pps', 'pptx', 'txt', 'pdf', 'md',
//視頻
'wmv', 'asf', 'asx', 'rm', 'rmvb', 'mpg', 'mpeg', 'mpe', '3gp', 'mov', 'mp4', 'm4v', 'avi', 'dat', 'mkv', 'flv', 'vob',
//音頻
'mp3', 'ogg', 'ape', 'ape', 'cda', 'au', 'midi', 'mac', 'aac',
//壓縮
'rar', 'zip', '7z', 'gz', 'bz', 'ace', 'uha', 'uda', 'zpaq',
],
],
],
],
];
~~~
### 6、獲取圖片屬性get_image_attr
~~~
//這個直接看代碼就好了
/**
* 獲取圖片屬性
*
* @param string $path 圖片地址
* @return array
*/
public function get_image_attr($path): array
{
if (!file_exists($path)) {
return ['status' => false, 'message' => '文件不存在'];
}
try {
$image = Image::open($path);
$data = [
'width' => $image->width(),
'height' => $image->height(),
'type' => $image->type(),
'mime' => $image->mime(),
'size' => $image->size()
];
return ['status' => true, 'message' => '獲取屬性成功', 'data' => $data];
} catch (\Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
}
~~~
### 7、圖片處理配置文件說明
在后面的方法都是一些圖片處理的方法
先貼出配置文件代碼
調用的key是config目錄下images.php中的key
---相信都能看的懂配置吧-配合代碼
~~~
return [
//縮略圖
'thumb' => [
//圖片生成縮略圖默認配置
'default' => [
'width' => 150,
'height' => 150,
//類型說明:
//1、常量,標識縮略圖等比例縮放類型 //2、常量,標識縮略圖縮放后填充類型 //3、常量,標識縮略圖居中裁剪類型
//4、常量,標識縮略圖左上角裁剪類型 //5、常量,標識縮略圖右下角裁剪類型 //6、常量,標識縮略圖固定尺寸縮放類型
'type' => 1,
],
],
//添加水印
'water' => [
//圖片默認添加水印配置
'default' => [
//水印圖片
'img' => './resouce/logo.png',
//透明度
'alpha' => 100,
//圖片顯示位置----位置說明
//1、標識左上角水印 //2、標識上居中水印 //3、標識右上角水印 //4、標識左居中水印 //5、標識居中水印
//6、標識右居中水印 //7、標識左下角水印 //8、標識下居中水印 //9、標識右下角水印
'imgtype' => 1,
//文字
'text' => '經典宋體',
//顯示位置tong imagetype
'texttype' => 1,
//字體必填且為全路徑
'font' => __DIR__ . '../../public/resouce/font/jdsj.ttf',
//字體大小
'size' => 20,
//字體顏色
'color' => '#00000000',
//文字相對當前位置的偏移量
'offset' => 0,
//文字傾斜角度
'angle' => 0,
],
'wimg' => [
'img' => './resouce/logo.png',
'alpha' => 100,
'imgtype' => 1,
],
'wtext' => [
'text' => '經典宋體',
'texttype' => 1,
'font' => __DIR__ . '../../public/resouce/font/jdsj.ttf',
'size' => 20,
'color' => '#00000000',
'offset' => 0,
'angle' => 0,
],
],
//裁剪圖片
'crop' => [
//圖片默認裁剪配置
'default' => [
//裁剪區域寬度
'cropw' => 150,
//裁剪區域高度
'croph' => 150,
//裁剪區域x坐標
'x' => 0,
//裁剪區域y坐標
'y' => 0,
//圖像保存寬度
'width' => null,
//圖像保存高度
'height' => null,
],
],
//圖像翻轉
'flip' => [
//圖像翻轉默認配置
'default' => [
//圖片翻轉 type 1=按X軸,2=按Y軸
'type' => 1
],
],
//圖片旋轉
'rotate' => [
//圖片旋轉默認配置
'default' => [
//圖片旋轉 degrees旋轉角度
'degrees' => 90
],
],
//混合連續動作--高級配置應用
'mixed' => [
//此默認配置為示例配置--具體操作而應實際情況生產
'default' => [
'sort0' => ['thumb' => ['width' => 150, 'height' => 150, 'type' => 1]],
'sort1' => ['water' => ['img' => './resouce/logo.png', 'alpha' => 100, 'imgtype' => 1]],
'sort2' => ['crop' => ['cropw' => 150, 'croph' => 150, 'x' => 0, 'y' => 0, 'width' => null, 'height' => null]],
'sort3' => ['flip' => ['type' => 1]],
'sort4' => ['rotate' => ['degrees' => 90]]
],
],
];
~~~
### 8、生成縮略圖create_image_thumb
~~~
/**
* 生成縮略圖
*
* @param string $path 圖片地址
* @param string $key images配置文件中thumb的縮略圖配置key
* @return array
*/
public function create_image_thumb($path, $key = null): array
{
try {
if (!file_exists($path)) {
return ['status' => false, 'message' => '文件不存在'];
}
if ($key == null) {
return ['status' => false, 'message' => 'key不能為空'];
}
if (explode('.', $key)[0] != 'thumb') {
return ['status' => false, 'message' => '非法key,不是縮略圖的key'];
}
$keys = Config::get('images.' . $key);
if ($keys == null) {
return ['status' => false, 'message' => '沒有該配置'];
}
$base_info = explode('.', basename($path));
$image = Image::open($path);
$width = $keys['width'] ?? 150;
$height = $keys['height'] ?? 150;
$type = $keys['type'] ?? 1;
$save_path = str_replace(basename($path), '', $path) .
$base_info[0] . '_' . $type . '_thumb.' . $base_info[1];
$thumb = $image ->thumb($width, $height, $type)
->save($save_path);
$get_success = function () use (&$base_info, &$save_path) {
return [
'status' => true,
'message' => '生成縮略圖成功',
'data' => [
'suffix' => $base_info[1],
'size' => filesize($save_path),
'file_path' => $save_path,
'file_name' => basename($save_path)
]
];
};
return $thumb ? $get_success() : ['status' => false, 'message' => '生成縮略圖失敗'];
} catch (\Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
}
~~~
### 9、圖片添加水印create_image_water
~~~
/**
* 圖片添加水印
*
* @param string $path 圖片路徑
* @param string $key images配置文件中water的縮略圖配置key
* @param string $text 當添加文字水印的時候需要手動傳入值
* @return array
*/
public function create_image_water($path, $key = null, $text = null): array
{
try {
if (!file_exists($path)) {
return ['status' => false, 'message' => '文件不存在'];
}
if ($key == null) {
return ['status' => false, 'message' => 'key不能為空'];
}
if (explode('.', $key)[0] != 'water') {
return ['status' => false, 'message' => '非法key,不是加水印的key'];
}
$keys = Config::get('images.' . $key);
if ($keys == null) {
return ['status' => false, 'message' => '沒有該配置'];
}
$base_info = explode('.', basename($path));
$image = Image::open($path);
$imgtype = $keys['imgtype'] ?? 1;
$texttype = $keys['texttype'] ?? 1;
$text = $text ?? $keys['text'];
$save_path = str_replace(basename($path), '', $path) .
$base_info[0] . '_' . $imgtype . $texttype . '_water.' . $base_info[1];
if ($keys['img']) {
if (file_exists($keys['img'])) {
$alpha = $keys['alpha'] ?? 100;
$image->water($keys['img'], $imgtype, $alpha);
} else {
return ['status' => false, 'message' => '水印圖片不存在'];
}
}
if (!empty($text)) {
$size = $keys['size'] ?? 20;
$color = $keys['color'] ?? '#000000';
$offset = $keys['offset'] ?? 0;
$angle = $keys['angle'] ?? 0;
$font = $keys['font'];
$image->text(
mb_convert_encoding($text, 'html-entities', 'UTF-8'),
$font,
$size,
$color,
$texttype,
$offset,
$angle
);
}
$water = $image->save($save_path);
$get_success = function () use (&$base_info, &$save_path) {
return [
'status' => true,
'message' => '添加水印成功',
'data' => [
'suffix' => $base_info[1],
'size' => filesize($save_path),
'file_path' => $save_path,
'file_name' => basename($save_path)
]
];
};
return $water ? $get_success() : ['status' => false, 'message' => '添加水印失敗'];
} catch (\Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
}
~~~
### 10、圖片裁剪create_image_crop
~~~
/**
* 圖片裁剪
*
* @param string $path 圖片路徑
* @param string $key images配置文件中water的縮略圖配置key
* @return array
*/
public function create_image_crop($path, $key = null): array
{
try {
if (!file_exists($path)) {
return ['status' => false, 'message' => '文件不存在'];
}
if ($key == null) {
return ['status' => false, 'message' => 'key不能為空'];
}
if (explode('.', $key)[0] != 'crop') {
return ['status' => false, 'message' => '非法key,不是加水印的key'];
}
$keys = Config::get('images.' . $key);
if ($keys == null) {
return ['status' => false, 'message' => '沒有該配置'];
}
$base_info = explode('.', basename($path));
$image = Image::open($path);
$save_path = str_replace(basename($path), '', $path) .
$base_info[0] . '_cut.' . $base_info[1];
$cropw = $keys['cropw'] ?? 150;
$croph = $keys['croph'] ?? 150;
$x = $keys['x'] ?? 0;
$y = $keys['y'] ?? 0;
$width = $keys['width'] ?? null;
$height = $keys['height'] ?? null;
$crop = $image ->crop(
$cropw,
$croph,
$x,
$y,
$width,
$height
)
->save($save_path);
$get_success = function () use (&$base_info, &$save_path) {
return [
'status' => true,
'message' => '裁剪圖片成功',
'data' => [
'suffix' => $base_info[1],
'size' => filesize($save_path),
'file_path' => $save_path,
'file_name' => basename($save_path)
]
];
};
return $crop ? $get_success() : ['status' => false, 'message' => '裁剪圖片失敗'];
} catch (\Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
}
~~~
### 11、圖片翻轉create_image_flip
~~~
/**
* 圖片翻轉
*
* @param string $path 圖片路徑
* @param string $key images配置文件中flip的縮略圖配置key
* @return array
*/
public function create_image_flip($path, $key = null): array
{
try {
if (!file_exists($path)) {
return ['status' => false, 'message' => '文件不存在'];
}
if ($key == null) {
return ['status' => false, 'message' => 'key不能為空'];
}
if (explode('.', $key)[0] != 'flip') {
return ['status' => false, 'message' => '非法key,不是加水印的key'];
}
$keys = Config::get('images.' . $key);
if ($keys == null) {
return ['status' => false, 'message' => '沒有該配置'];
}
$base_info = explode('.', basename($path));
$image = Image::open($path);
$save_path = str_replace(basename($path), '', $path) . $base_info[0] . '_flip.' . $base_info[1];
$type = $keys['type'] ?? 1;
$flip = $image->flip($type)->save($save_path);
$get_success = function () use (&$base_info, &$save_path) {
return [
'status' => true,
'message' => '翻轉圖片成功',
'data' => [
'suffix' => $base_info[1],
'size' => filesize($save_path),
'file_path' => $save_path,
'file_name' => basename($save_path)
]
];
};
return $flip ? $get_success() : ['status' => false, 'message' => '翻轉圖片失敗'];
} catch (\Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
}
~~~
### 12、圖片旋轉create_image_rotate
~~~
/**
* 圖片旋轉
*
* @param string $path 圖片路徑
* @param string $key images配置文件中rotate的縮略圖配置key
* @return array
*/
public function create_image_rotate($path, $key = null): array
{
try {
if (!file_exists($path)) {
return ['status' => false, 'message' => '文件不存在'];
}
if ($key == null) {
return ['status' => false, 'message' => 'key不能為空'];
}
if (explode('.', $key)[0] != 'rotate') {
return ['status' => false, 'message' => '非法key,不是加水印的key'];
}
$keys = Config::get('images.' . $key);
if ($keys == null) {
return ['status' => false, 'message' => '沒有該配置'];
}
$base_info = explode('.', basename($path));
$image = Image::open($path);
$save_path = str_replace(basename($path), '', $path) . $base_info[0] . '_rotate.' . $base_info[1];
$degrees = $keys['degrees'] ?? 90;
$rotate = $image->rotate($degrees)->save($save_path);
$get_success = function () use (&$base_info, &$save_path) {
return [
'status' => true,
'message' => '旋轉圖片成功',
'data' => [
'suffix' => $base_info[1],
'size' => filesize($save_path),
'file_path' => $save_path,
'file_name' => basename($save_path)
]
];
};
return $rotate ? $get_success() : ['status' => false, 'message' => '旋轉圖片失敗'];
} catch (\Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
}
~~~
### 13、圖片混合處理image_mixed
~~~
/**
* 圖片混合處理
*
* @param integer $type 圖片上傳處理類型 1表示指定上傳圖片路徑 2表示直接上傳
* @param string $key 混合圖片上傳的key值
* @param string $path 當type=1的時候$path必須指定已知圖片路徑
* @param string $upkey 當type=2的時候$upkey必須指定上傳的key值
* @return array
*/
public function image_mixed(
$type = 1,
$key = null,
$path = null,
$upkey = null
): array
{
try {
//轉換int
$type = intval($type);
//判斷是否正確類型
if (!in_array($type, [1, 2])) {
return ['status' => false, 'message' => 'type類型不正確'];
}
//判斷key是否存在
if ($key == null) {
return ['status' => false, 'message' => 'key不能為空'];
}
//判斷是否是正確的key
if (explode('.', $key)[0] != 'mixed') {
return ['status' => false, 'message' => '非法key,不是加水印的key'];
}
//獲取key的信息
$keys = Config::get('images.' . $key);
//判斷key是否有配置
if ($keys == null) {
return ['status' => false, 'message' => '沒有該配置'];
}
//判斷類型是直接路徑還是上傳后的路徑
if ($type == 2) {
$upinfo = $this->upload($upkey);
if (!$upinfo['status']) {
return $upinfo;
} else {
$path = $upinfo['data']['file_path'];
}
}
//判斷文件路徑是否正確,是否存在
if (!file_exists($path)) {
return ['status' => false, 'message' => '文件不存在'];
}
return $this->mixed_image_deal($path, $keys);
} catch (\Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
}
/**
* 處理混合處理
*
* @param string $path 圖片路徑
* @param array $keys 配置值
* @return array
*/
protected function mixed_image_deal($path, &$keys): array
{
try {
$base_info = explode('.', basename($path));
$image = Image::open($path);
$save_path = str_replace(basename($path), '', $path) . $base_info[0] . '_mixed.' . $base_info[1];
//根據鍵值升序排列
ksort($keys);
$image_render = function(&$image_type, &$image) {
foreach ($image_type as $key => $val) {
switch ($key) {
case 'thumb':
$width = $val['width'] ?? 150;
$height = $val['height'] ?? 150;
$type = $val['type'] ?? 1;
$image->thumb($width, $height, $type);
break;
case 'water':
$alpha = $val['alpha'] ?? 100;
$img = $val['img'] ?? '';
$imgtype = $val['imgtype'] ?? 1;
$image->water($img, $imgtype, $alpha);
break;
case 'text':
$text = $val['text'];
$texttype = $val['texttype'] ?? 1;
$size = $val['size'] ?? 20;
$color = $val['color'] ?? '#000000';
$offset = $val['offset'] ?? 0;
$angle = $val['angle'] ?? 0;
$font = $val['font'];
$image->text(
mb_convert_encoding($text, 'html-entities', 'UTF-8'),
$font,
$size,
$color,
$texttype,
$offset,
$angle
);
break;
case 'crop':
$cropw = $val['cropw'] ?? 150;
$croph = $val['croph'] ?? 150;
$x = $val['x'] ?? 0;
$y = $val['y'] ?? 0;
$width = $val['width'] ?? null;
$height = $val['height'] ?? null;
$image->crop(
$cropw,
$croph,
$x,
$y,
$width,
$height
);
break;
case 'flip':
$type = $val['type'] ?? 1;
$image->flip($type);
break;
case 'rotate':
$degrees = $val['degrees'] ?? 90;
$image->rotate($degrees);
break;
default:
throw new \Exception('未找到相應的類型',10006);
break;
}
}
};
foreach ($keys as $kd => $kv) {
$image_render($kv,$image);
}
$info = $image->save($save_path);
$get_success = function () use (&$base_info, &$save_path) {
return [
'status' => true,
'message' => '操作成功',
'data' => [
'suffix' => $base_info[1],
'size' => filesize($save_path),
'file_path' => $save_path,
'file_name' => basename($save_path)
]
];
};
return $info ? $get_success() : ['status' => false, 'message' => '操作失敗'];
} catch (\Exception $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
}
~~~
### 14、作者沒話說也要說兩句
不想解釋了
只能相信各位phper的理解能力了
留言說明-解答
- 產品介紹
- 功能介紹
- 聯系合作
- 修訂記錄
- 環境安裝
- 依賴環境
- 依賴服務
- 前端環境
- 核心功能
- 流程引擎
- 流程編輯步驟
- 流程設計
- 業務表設計
- 表單設計
- 報表設計
- 節點配置
- 全局配置
- 規則引擎
- 模塊功能
- 系統管理
- 平臺架構
- 平臺(單位)管理
- 組織架構
- 用戶管理
- 角色管理
- 運維管理
- 菜單管理
- 流程管理
- 代碼管理
- 字典管理
- 車型庫管理
- 報表管理
- 產品管理
- 經銷商管理
- 數據字典
- 業務設置
- 業務報表設置
- 系統配置
- 微信小程序設置
- OCR識別
- 釘釘通知
- 釘釘登錄授權
- 云儲存設置
- 服務規劃
- 其他產品
- 教育點播
- 退役政務
- 三方報表
- 微信社群
- 場景名片
- 合同電子簽
- TP5實用封裝
- 通用封裝
- Export通用封裝
- Import通用封裝
- 配合Import通用封裝的ImportBaseVerify類
- Files通用封裝
- Directory通用封裝
- Pdf通用封裝
- Words通用封裝
- Nredis(redis封裝)
- ZipArchives壓縮zip文件封裝
- BarQrcode條形碼二維碼
- Publics公共方法封裝
- Curls(cUrl請求封裝)
- extend擴展開發
- 何時編寫擴展文件包
- 擴展文件包如何編寫
- 題外話
- 常用的compose安裝
- 一些建議及細節
- 實用工具
- 源碼分析
- 入口文件index.php
- think\Facade類詳解
- App對接Api設計
- 設計模式說明(為什么這么做)
- 代碼設計邏輯方案
- 數據庫備份
- 實用案例
- 如何“偷”JAVA代碼
- SAAS接口設計
- 1、創建saas基類文件
- 2、初始化控制器服務
- 3、身份校驗
- 4、創建登錄控制器
- 5、實現登錄服務層
- 6、服務層繼承的service類
- 7、該案例的完整模塊包
- 校驗型規則校驗擴展包
- 自定義規則解析(可供參考)
- 通用文件擴展包(File,Zip)
- PHP實現word轉PDF功能環境搭建
- 日志通用擴展包
- 定時任務
- PHP視頻處理器安裝環境搭建
- 日常開發問題記錄
- 1、mysql存儲中帶一些不可見字符
- 2、php開發規范校驗及修復