暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Attachment.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 附件上传模型
  13. // +----------------------------------------------------------------------
  14. namespace app\common\model;
  15. use think\Image;
  16. use think\Model;
  17. class Attachment extends Model
  18. {
  19. // 自动写入时间戳
  20. protected $autoWriteTimestamp = true;
  21. protected $insert = ['status' => 1];
  22. public function getSizeAttr($value)
  23. {
  24. return format_bytes($value);
  25. }
  26. /**
  27. * 创建缩略图
  28. *
  29. * @param string $file 目标文件,可以是文件对象或文件路径
  30. * @param string $filename
  31. * @param string $save_name 缩略图名
  32. * @param string $thumb_size 尺寸
  33. * @param string $thumb_type 裁剪类型
  34. *
  35. * @return string 缩略图路径
  36. */
  37. public function create_thumb($file = '', $filename = '', $save_name = '', $thumb_size = '', $thumb_type = '')
  38. {
  39. // 获取要生成的缩略图最大宽度和高度
  40. $thumb_size = $thumb_size == '' ? config('site.upload_image_thumb') : $thumb_size;
  41. [$thumb_max_width, $thumb_max_height] = explode(',', $thumb_size);
  42. // 读取图片
  43. $image = Image::open($file);
  44. // 生成缩略图
  45. $thumb_type = $thumb_type == '' ? config('site.upload_image_thumb_type') : $thumb_type;
  46. $image->thumb($thumb_max_width, $thumb_max_height, $thumb_type);
  47. if (!is_dir($filename)) {
  48. mkdir($filename, 0766, true);
  49. }
  50. $image->save($filename . $save_name);
  51. return $filename;
  52. }
  53. /**
  54. * 添加水印
  55. */
  56. public function create_water($file = '', $path = '', $watermark_pos = '', $watermark_alpha = '')
  57. {
  58. $thumb_water_pic = realpath(ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . $path);
  59. if (is_file($thumb_water_pic)) {
  60. // 读取图片
  61. $image = Image::open($file);
  62. // 添加水印
  63. $watermark_pos = $watermark_pos == '' ? config('site.upload_thumb_water_position') : $watermark_pos;
  64. $watermark_alpha = $watermark_alpha == '' ? config('site.upload_thumb_water_alpha') : $watermark_alpha;
  65. $image->water($thumb_water_pic, $watermark_pos, $watermark_alpha);
  66. // 保存水印图片,覆盖原图
  67. $image->save($file);
  68. }
  69. }
  70. }