diff --git a/app/common/tools/Image.php b/app/common/tools/Image.php new file mode 100644 index 0000000..9825741 --- /dev/null +++ b/app/common/tools/Image.php @@ -0,0 +1,75 @@ +getExtension(), ['jpg', 'jpeg', 'png', 'gif'])) { + return false; + } + + ImageManagerStatic::configure(['driver' => 'imagick']); + + $img = ImageManagerStatic::make($file->getPathname()); + + $font_size = 20; + + // 字体大小为宽度的 1/20 + $font_size = $img->width() / 40; + $font_size = ceil($font_size); + + $x = $img->width() - $font_size; + $y = $img->height() - $font_size; + + $img->text($watermark_str, $x + $font_size * 0.05, $y + $font_size * 0.05, function ($font) use ($font_size) { + $font->file(App::getRootPath() . '/source/font/SourceHanSans-Normal.otf'); + $font->size($font_size); + $font->color([0, 0, 0, 0.9]); + $font->align('right'); + $font->valign('bottom'); + }); + $img->text($watermark_str, $x, $y, function ($font) use ($font_size) { + $font->file(App::getRootPath() . '/source/font/SourceHanSans-Normal.otf'); + $font->size($font_size); + $font->color([255, 255, 255, 0.9]); + $font->align('right'); + $font->valign('bottom'); + }); + + return $img; + } + + public static function handelWatermarkSave($src, $watermarked_file_save_name = null) + { + $src_file = new SplFileInfo($src); + $watermark_text = get_system_config('watermark_text', 'phpreturn.com'); + + $src_md5 = md5($src . $watermark_text); + + if (!$watermarked_file_save_name) { + $watermarked_file_save_name = '/upload/watermark/' . $src_md5 . '.' . $src_file->getExtension(); + } + + $watermarked_file = App::getRootPath() . '/public' . $watermarked_file_save_name; + + if (file_exists($watermarked_file)) { + return $watermarked_file_save_name; + } + + if (!is_dir(dirname($watermarked_file))) { + mkdir(dirname($watermarked_file), 0777, true); + } + + static::addWatermark($src_file, $watermark_text)->save($watermarked_file); + + return $watermarked_file_save_name; + } +} diff --git a/app/common/tools/PostShow.php b/app/common/tools/PostShow.php index 9ee27d2..c7dbc23 100644 --- a/app/common/tools/PostShow.php +++ b/app/common/tools/PostShow.php @@ -8,7 +8,6 @@ class PostShow { public static function handleCopyright($content) { - // 防采集版权声明,被采集了会在其他网站显示 // 可以被选中复制,可以被采集,在网页中不会显示,但是在别的网站中可以显示 $content_copyright = '
版权声明:' . get_system_config('post_copyright') . '
'; @@ -27,13 +26,10 @@ class PostShow $p_list = $dom_content->children('p'); foreach ($p_list as $index_p => $dom_p) { - if ($index_p != 0 && $index_p % 8 == 0) { - // $dom_copyright->clone()->children()->insertAfter($dom_p); } if ($index_p != 0 && $index_p % 6 == 0) { - $dom_copyright_hide->clone()->children()->insertAfter($dom_p); } } @@ -42,4 +38,27 @@ class PostShow return $content; } + + public static function handleImage($content) + { + $dom_content = phpQuery::newDocumentHTML($content); + + $img_list = $dom_content->find('img'); + + foreach ($img_list as $index_img => $dom_img) { + $dom_img = pq($dom_img); + + $src = $dom_img->attr('src'); + + $src = get_source_link($src); + + $watermarked_file_save_name = Image::handelWatermarkSave($src); + + $dom_img->attr('src', $watermarked_file_save_name); + } + + $content = $dom_content->html(); + + return $content; + } } diff --git a/app/model/Nav.php b/app/model/Nav.php index 2209dfd..6f831fc 100644 --- a/app/model/Nav.php +++ b/app/model/Nav.php @@ -5,36 +5,45 @@ declare(strict_types=1); namespace app\model; use app\common\model\Base; - +use app\common\tools\Image; /** * @mixin think\Model */ class Nav extends Base { - public static $autoClearCache = [ [ 'type' => 'key', 'name' => 'type_list', - 'field' => 'type' + 'field' => 'type', ], [ 'type' => 'tag', - 'name' => 'page_cache' + 'name' => 'page_cache', ], ]; public static $statusName = [ 0 => '不显示', - 1 => '显示' + 1 => '显示', ]; + // public function getImgAttr($value) { return get_source_link($value); } + public function getImgShowAttr() + { + $src = $this->getData('img'); + + $watermarked_file_save_name = Image::handelWatermarkSave($src); + + return $watermarked_file_save_name; + } + public function getStatusNameAttr() { return self::$statusName[$this->getData('status')]; diff --git a/app/model/Post.php b/app/model/Post.php index 1db9bec..1f5d3ed 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -5,15 +5,14 @@ declare(strict_types=1); namespace app\model; use app\common\model\Base; +use app\common\tools\Image; use app\common\tools\PostBlock; use app\common\tools\PostShow; use League\HTMLToMarkdown\Converter\TableConverter; use League\HTMLToMarkdown\HtmlConverter; use think\facade\Cache; use think\facade\Request; -use think\Model; use think\model\concern\SoftDelete; -use think\Paginator; /** * @mixin think\Model @@ -23,7 +22,6 @@ class Post extends Base use SoftDelete; // - public const CACHE_KEY_HITS = 'cache_hits_'; public static $autoClearCache = [ @@ -32,7 +30,7 @@ class Post extends Base public static $stausNameList = [ 0 => '不发布', - 1 => '发布' + 1 => '发布', ]; protected $defaultSoftDelete = 0; @@ -52,7 +50,6 @@ class Post extends Base return $this->hasMany(PostComment::class, 'post_id'); } - public function getCommentCountAttr() { return PostComment::getPostCommentsCount($this->getData('id')); @@ -62,16 +59,18 @@ class Post extends Base { return strtotime($value); } + public function getPublishTimeTextAttr() { - $value = $this->getData('publish_time'); + return date('Y-m-d', $value); } + public function getPublishTimeDatetimeAttr() { - $value = $this->getData('publish_time'); + return date('Y-m-d H:i:s', $value); } @@ -97,8 +96,7 @@ class Post extends Base public function getDescAttr($value) { - if(empty($value)) { - + if (empty($value)) { } return $value; @@ -157,11 +155,9 @@ class Post extends Base { $content_html = $this->getAttr('content_html'); - $content_html .= PostBlock::copyright($this); - - $converter = new HtmlConverter(array('strip_tags' => true)); + $converter = new HtmlConverter(['strip_tags' => true]); $converter->getEnvironment()->addConverter(new TableConverter()); @@ -172,9 +168,10 @@ class Post extends Base public function getContentHtmlShowAttr() { - $content = $this->getAttr('content_html'); + $content = PostShow::handleImage($content); + return PostShow::handleCopyright($content); } @@ -187,6 +184,15 @@ class Post extends Base return get_source_link($value); } + public function getPosterShowAttr() + { + $poster = $this->getAttr('poster'); + + $watermarked_file_save_name = Image::handelWatermarkSave($poster); + + return $watermarked_file_save_name; + } + public function getReadUrlAttr() { $path = '/index/a' . $this->getData('uid') . '.html'; @@ -211,7 +217,7 @@ class Post extends Base $list_post_category_id = $this->getAttr('categorys')->column('category_id'); - $list_start_post = Post::where('category_id', 'in', $list_post_category_id)->where('type', 'category-start')->cacheAlways(600)->where('status', 1)->select(); + $list_start_post = self::where('category_id', 'in', $list_post_category_id)->where('type', 'category-start')->cacheAlways(600)->where('status', 1)->select(); foreach ($list_start_post as $k_start_post => $v_start_post) { $start_content .= $v_start_post->content_html; @@ -224,20 +230,21 @@ class Post extends Base { $end_content = ''; $list_post_category_id = $this->getAttr('categorys')->column('category_id'); - $list_end_post = Post::where('category_id', 'in', $list_post_category_id)->where('type', 'category-end')->cacheAlways(600)->where('status', 1)->select(); + $list_end_post = self::where('category_id', 'in', $list_post_category_id)->where('type', 'category-end')->cacheAlways(600)->where('status', 1)->select(); foreach ($list_end_post as $k_end_post => $v_end_post) { - $end_content.= $v_end_post->content_html; + $end_content .= $v_end_post->content_html; } + return $end_content; } public function getHitsTitleAttr() { - $cache_key = static::CACHE_KEY_HITS.$this->getAttr('id'); + $cache_key = static::CACHE_KEY_HITS . $this->getAttr('id'); $value = Cache::get($cache_key); - if(!is_null($value)) { + if (!is_null($value)) { return $value; } @@ -250,7 +257,7 @@ class Post extends Base public function setHitsAttr($value) { - $cache_key = static::CACHE_KEY_HITS.$this->getAttr('id'); + $cache_key = static::CACHE_KEY_HITS . $this->getAttr('id'); $this->getAttr('hits_title'); Cache::inc($cache_key); diff --git a/composer.json b/composer.json index c33b80f..f0a81df 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "league/html-to-markdown": "^5.1", "topthink/think-filesystem": "^2.0", "mibe/feedwriter": "^1.1", - "matomo/device-detector": "^6.1" + "matomo/device-detector": "^6.1", + "intervention/image": "^2.7" }, "require-dev": { "symfony/var-dumper": "^4.2" diff --git a/source/font/SourceHanSans-Normal.otf b/source/font/SourceHanSans-Normal.otf new file mode 100644 index 0000000..ef259e2 Binary files /dev/null and b/source/font/SourceHanSans-Normal.otf differ diff --git a/view/admin/system/index.html b/view/admin/system/index.html index 98c1a4b..1917c52 100644 --- a/view/admin/system/index.html +++ b/view/admin/system/index.html @@ -174,6 +174,13 @@ + +
+
水印内容
+
+ +
+
统计代码
diff --git a/view/index/index/index.html b/view/index/index/index.html index 5b1d194..25df875 100644 --- a/view/index/index/index.html +++ b/view/index/index/index.html @@ -26,7 +26,7 @@ {volist name='list_nav_slide' id='nav'} -
+
@@ -78,7 +78,7 @@
- {$post->content_html|raw} + {$post->content_html_show|raw}
{/case} @@ -86,7 +86,7 @@ {notempty name='$post->getData("poster")'} -
+
{/notempty}