123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace core\upload;
- use core\exception\UploadFileException;
- use Exception;
- use Grafika\Color;
- use Grafika\Grafika;
- use Grafika\Position;
- /**
- * 文件管理驱动类
- */
- class Local extends BaseUpload
- {
- //位置
- private $position = array(
- 'top-left' => 'top-left',
- 'top-center' => 'top-center',
- 'top-right' => 'top-right',
- 'center-left' => 'center-left',
- 'center' => 'center',
- 'center-right' => 'center-right',
- 'bottom-left' => 'bottom-left',
- 'bottom-center' => 'bottom-center',
- 'bottom-right' => 'bottom-right',
- );
- protected function initialize(array $config = [])
- {
- parent::initialize($config);
- }
- public function upload(string $dir)
- {
- $this->validate();
- mkdirs_or_notexist($dir, 0777);
- $this->file->move($dir, $this->file_name);
- //错误一般是已经被抛出了
- return true;
- }
- /**
- * 远程获取图片
- * @param string $url
- * @param string|null $key
- * @return true
- */
- public function fetch(string $url, ?string $key)
- {
- try {
- mkdirs_or_notexist(dirname($key), 0777);
- $content = @file_get_contents($url);
- if (!empty($content)) {
- file_put_contents($key, $content);
- $image_info = getimagesize($key);
- $image_type = $image_info[2];
- // if($image_type == IMAGETYPE_JPEG){
- // // 保存图片为PNG格式
- // $image = imagecreatefromjpeg($key);
- // }else if($image_type == IMAGETYPE_PNG){
- // // 保存图片为PNG格式
- // $image = imagecreatefrompng($key);
- // }
- if($image_type == IMAGETYPE_WEBP){
- // 保存图片为PNG格式
- $image = imagecreatefromwebp($key);
- }
- // 创建图片资源
- // 检查图片是否创建成功
- if (!empty($image)){
- // 图片类型常量定义:IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF
- if ($image_type == IMAGETYPE_WEBP) {
- $temp_arr = explode('.', $key);
- $ext = end($temp_arr);
- if($ext == 'jpg' || $ext == 'jpeg'){
- // 保存图片为PNG格式
- imagejpeg($image, $key);
- }else if($ext = 'png'){
- // 保存图片为PNG格式
- imagepng($image, $key);
- }
- }
- // 释放内存
- imagedestroy($image);
- }
- } else {
- throw new UploadFileException(203006);
- }
- return true;
- } catch ( Exception $e ) {
- throw new UploadFileException($e->getMessage());
- }
- }
- /**
- * base64转图片
- * @param string $content
- * @param string|null $key
- * @return true
- */
- public function base64(string $content, ?string $key = null)
- {
- mkdirs_or_notexist(dirname($key));
- file_put_contents(url_to_path($key), base64_decode($content));
- return true;
- }
- /**
- * 删除本地附件
- * @param string $file_name
- * @return bool
- */
- public function delete(string $file_name)
- {
- $file_path = url_to_path($file_name);
- if (file_exists($file_path)) {
- $result = unlink($file_path);
- // throw new UploadFileException(100013);
- }else{
- $result = true;
- }
- //顺便删除相关的缩略图
- $dirname = dirname($file_name);
- $file_list = [];
- search_dir($dirname, $file_list);
- if(!empty($file_list)){
- $file_arr = explode('/', $file_name);
- $only_file_name = end($file_arr);
- foreach($file_list as $v){
- if(str_contains($v, $only_file_name) && file_exists($v)){
- unlink($v);
- }
- }
- }
- return $result;
- }
- /**
- * 缩略图
- * @param $file_path
- * @param $thumb_type
- * @return array
- * @throws Exception
- */
- public function thumb($file_path, $thumb_type)
- {
- //todo 判断缩略图是否存在
- $thumb_config = config('upload.thumb.thumb_type');
- // ……
- //获取文件原名 获取
- $file_arr = explode('/', $file_path);
- $file_name = end($file_arr);
- $thumb_list = [];
- //获取文件后缀
- foreach ($thumb_config as $k => $v) {
- if ($thumb_type == 'all' || $thumb_type == $k || (is_array($thumb_type) && in_array($k, $thumb_type))) {
- $new_width = $v['width'];
- $new_height = $v['height'];
- $new_thumb_path = str_replace($file_name, $new_width . 'x' . $new_height . '_' . $file_name, $file_path);
- if (!file_exists($new_thumb_path)) {
- $editor = Grafika::createEditor();
- $editor->open($image, $file_path);
- $editor->resizeFit($image, $new_width, $new_height);
- //新缩略图文件名称
- $editor->save($image, $new_thumb_path, null, null, false, 0777);
- }
- $thumb_list[$k] = $new_thumb_path;
- }
- }
- return $thumb_list;
- }
- /**
- * 图片水印
- * @param $file_path
- * @return mixed
- * @throws Exception
- */
- public function water($file_path)
- {
- $water_config = [];
- if (!empty($water_config)) {
- $status = $water_config['status'];//是否启用
- if ($status) {
- $editor = Grafika::createEditor();
- $editor->open($image, $file_path);
- if ($water_config['type'] == 'image') {
- $water_image = $water_config['image'];
- if (!empty($water_image)) {
- //判断水印图片是否是本地图片
- if (check_file_is_remote($water_image)) {
- $file_arr = explode('.', $water_image);
- $ext_name = end($file_arr);
- $name = $this->createFileName($water_image, $ext_name);
- $watermark_image = 'upload/water/' . $name;
- $this->fetch($water_image, $watermark_image);
- }
- if (file_exists($water_image)) {
- }
- $editor->open($image1, $water_config['image']);
- $editor->blend($image, $image1, 'normal', $water_config['opacity'], $this->position[$water_config['position']], $water_config['offset_x'], $water_config['offset_y']);
- }
- } else {
- if ($water_config['text']) {
- $position = $this->position[$water_config['position']];
- $offset_x = $water_config['offset_x'];//水平偏移值
- $offset_y = $water_config['offset_y'];//垂直偏移值
- $width = $image->getWidth();
- $height = $image->getHeight();
- //获取文字信息
- $info = imagettfbbox($water_config['size'], $water_config['angle'], $water_config['font'], $water_config['text']);
- $minx = min($info[0], $info[2], $info[4], $info[6]);
- $maxx = max($info[0], $info[2], $info[4], $info[6]);
- $miny = min($info[1], $info[3], $info[5], $info[7]);
- $maxy = max($info[1], $info[3], $info[5], $info[7]);
- /* 计算文字初始坐标和尺寸 */
- $x = $minx;
- $y = abs($miny);
- $w = $maxx - $minx;
- $h = $maxy - $miny;
- //转化坐标
- $position = new Position($position, $offset_x, $offset_y);
- // Position is for $image2. $image1 is canvas.
- list($offset_x, $offset_y) = $position->getXY($width, $height, $w, $h);
- $editor->text($image, $water_config['text'], $water_config['size'], $offset_x, $offset_y, new Color($water_config['color']), $water_config['font'], $water_config['angle']);
- }
- $editor->save($image, $file_path);
- }
- }
- return $file_path;
- }
- }
- }
|