ckeditor

Posted realadmin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ckeditor相关的知识,希望对你有一定的参考价值。

from io import BytesIO

from django.core.files.storage import FileSystemStorage
from django.core.files.uploadedfile import InMemoryUploadedFile

from PIL import Image, ImageDraw, ImageFont


class WatermarkStorage(FileSystemStorage):
    def save(self, name, content, max_length=None):
        # 处理逻辑
        if ‘image‘ in content.content_type:
            # 加水印
            image = self.watermark_with_text(content, ‘the5fire.com‘, ‘red‘)
            content = self.convert_image_to_file(image, name)

        return super().save(name, content, max_length=max_length)

    def convert_image_to_file(self, image, name):
        temp = BytesIO()
        image.save(temp, format=‘PNG‘)
        file_size = temp.tell()
        return InMemoryUploadedFile(temp, None, name, ‘image/png‘, file_size, None)

    def watermark_with_text(self, file_obj, text, color, fontfamily=None):
        image = Image.open(file_obj).convert(‘RGBA‘)
        draw = ImageDraw.Draw(image)
        width, height = image.size
        margin = 10
        if fontfamily:
            font = ImageFont.truetype(fontfamily, int(height / 20))
        else:
            font = None
        textWidth, textHeight = draw.textsize(text, font)
        x = (width - textWidth - margin) / 2  # 计算横轴位置
        y = height - textHeight - margin  # 计算纵轴位置
        draw.text((x, y), text, color, font)

        return image

  

以上是关于ckeditor的主要内容,如果未能解决你的问题,请参考以下文章

当我进入Source视图时,如何跳转到CKEditor 4中的当前位置?

在ckeditor中为keypress添加事件监听器的代码

ckeditor在保存时弄乱了代码

单击外部按钮时将代码添加到CKeditor

ckeditor 不读取媒体嵌入代码

CKEDITOR 脚本何时加载并准备好使用?