mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-03 16:24:28 +08:00
完成显示水印功能;
This commit is contained in:
75
app/common/tools/Image.php
Normal file
75
app/common/tools/Image.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\tools;
|
||||
|
||||
use Intervention\Image\Image as ImageImage;
|
||||
use Intervention\Image\ImageManagerStatic;
|
||||
use SplFileInfo;
|
||||
use think\facade\App;
|
||||
|
||||
class Image
|
||||
{
|
||||
public static function addWatermark(SplFileInfo $file, $watermark_str = 'phpreturn.com'):ImageImage
|
||||
{
|
||||
// 检查文件是否是图片
|
||||
if (!in_array($file->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;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ class PostShow
|
||||
{
|
||||
public static function handleCopyright($content)
|
||||
{
|
||||
|
||||
// 防采集版权声明,被采集了会在其他网站显示
|
||||
// 可以被选中复制,可以被采集,在网页中不会显示,但是在别的网站中可以显示
|
||||
$content_copyright = '<blockquote class="post-content-inner-copyright"> 版权声明:' . get_system_config('post_copyright') . '</blockquote>';
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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')];
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user