Sythumb.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace addons\sythumb;
  3. use think\Addons;
  4. use think\Config;
  5. use think\Loader;
  6. use addons\sythumb\library\imgclass\Image;
  7. /**
  8. * 缩略图插件
  9. * 2018-02-12
  10. * author: dilu
  11. * 253407587@qq.com
  12. */
  13. class Sythumb extends Addons
  14. {
  15. /**
  16. * 插件安装方法
  17. * @return bool
  18. */
  19. public function install()
  20. {
  21. return true;
  22. }
  23. /**
  24. * 插件卸载方法
  25. * @return bool
  26. */
  27. public function uninstall()
  28. {
  29. return true;
  30. }
  31. /**
  32. * 添加命名空间
  33. */
  34. public function appInit()
  35. {
  36. //添加命名空间
  37. }
  38. /**
  39. * 实现钩子方法
  40. * @return mixed
  41. */
  42. public function uploadAfter($param)
  43. {
  44. // 获取传进来的附件模型数据
  45. $data = $param->getData();
  46. //对文件进行检测 不是图片类型的不做处理
  47. if(!strpos($data['mimetype'],'image') === true && strpos($data['mimetype'],'image') === 0)
  48. {
  49. //附件id
  50. if(array_key_exists("id",$data)){
  51. $attachment_id = $param['id'];
  52. }else{
  53. $imgdate = $param->where('url',$param['url'])->find();
  54. $attachment_id = $imgdate['id'];
  55. }
  56. //获取配置
  57. $config = $this->getConfig();
  58. //图片质量
  59. $quality = isset($config['quality']) ? $config['quality'] : '100';
  60. if ($quality > 100 || $quality < 10) {
  61. $quality = 100;
  62. }
  63. //打开图片
  64. $image = Image::open(ROOT_PATH . '/public' . $data['url']);
  65. //获取后缀
  66. $ext = isset($config['ext']) ? $config['ext'] : '-thumb';
  67. $url = explode('.', $data['url']);
  68. //判断是否图片缩放
  69. if($config['deploy'] > 0){
  70. $image->thumb($config['zoomwith'],$config['zoomheight'],$config['deploy']);
  71. }
  72. //判断是否增加水印
  73. if ($config['type'] > 0){
  74. //判断是图片还是文字
  75. if($config['type'] == 1){ //图片
  76. //地址
  77. $imgurl = ROOT_PATH . '/public/'. DS . $config['images'];
  78. $image->water($imgurl,$config['location'],$config['diaphaneity']);
  79. }else{
  80. //地址
  81. $fonturl = ROOT_PATH . '/public/'. DS . $config['font'];;
  82. $image->text($config['text'],$fonturl,$config['size'],$config['color'],$config['location'],$config['offset']);
  83. }
  84. }
  85. //判断是否替换文件
  86. if (1 == $config['replace'] && !empty($attachment_id)) //如果是选择替换原文件
  87. {
  88. $image->save(ROOT_PATH . '/public' . $data['url'], null, $quality);
  89. $data = array(
  90. 'filesize' => filesize(ROOT_PATH . '/public' . $data['url']),
  91. 'imagewidth' => $image->width(),
  92. 'imageheight' => $image->height(),
  93. 'imagetype' => $image->type(),
  94. 'imageframes' => 1,
  95. 'mimetype' => $image->mime(),
  96. 'url' => $data['url'],
  97. 'uploadtime' => time(),
  98. 'storage' => 'local',
  99. 'sha1' => sha1_file(ROOT_PATH . '/public' . $data['url']),
  100. 'updatetime' => time(),
  101. );
  102. $param->where('id', $attachment_id)->update($data);
  103. } else {
  104. //组装缩略图的url
  105. $url = $url[0] . $ext . '.' . $url[1];
  106. $image->save(ROOT_PATH . '/public' . $url, null, $quality);
  107. $data = array(
  108. 'filesize' => filesize(ROOT_PATH . '/public' . $url),
  109. 'imagewidth' => $image->width(),
  110. 'imageheight' => $image->height(),
  111. 'imagetype' => $image->type(),
  112. 'imageframes' => 1,
  113. 'filename'=>substr($data['filename'], 0, strripos($data['filename'], '.')).'-thumb',
  114. 'mimetype' => $image->mime(),
  115. 'url' => $url,
  116. 'uploadtime' => time(),
  117. 'storage' => 'local',
  118. 'sha1' => sha1_file(ROOT_PATH . '/public' . $url),
  119. 'createtime' => time(),
  120. 'updatetime' => time(),
  121. );
  122. $param->insert($data);
  123. }
  124. }
  125. }
  126. }