Exemplo n.º 1
0
def encode_to_base64(editor, img_path, pos):
    """
    Encodes image to base64
    @requires: zen_file

    @type editor: ZenEditor
    @param img_path: Path to image
    @type img_path: str
    @param pos: Caret position where image is located in the editor
    @type pos: int
    @return: bool
    """
    editor_file = editor.get_file_path()
    default_mime_type = 'application/octet-stream'

    if editor_file is None:
        raise zencoding.utils.ZenError("You should save your file before using this action")


    # locate real image path
    real_img_path = zen_file.locate_file(editor_file, img_path)
    if real_img_path is None:
        raise zencoding.utils.ZenError("Can't find '%s' file" % img_path)

    b64 = base64.b64encode(zen_file.read(real_img_path))
    if not b64:
        raise zencoding.utils.ZenError("Can't encode file content to base64")


    b64 = 'data:' + (mime_types[zen_file.get_ext(real_img_path)] or default_mime_type) + ';base64,' + b64

    editor.replace_content(b64, pos, pos + len(img_path))
    # editor.replace_content('$0' + b64, pos, pos + len(img_path))
    return True
Exemplo n.º 2
0
def encode_to_base64(editor, img_path, pos):
    """
    Encodes image to base64
    @requires: zen_file

    @type editor: ZenEditor
    @param img_path: Path to image
    @type img_path: str
    @param pos: Caret position where image is located in the editor
    @type pos: int
    @return: bool
    """
    editor_file = editor.get_file_path()
    default_mime_type = 'application/octet-stream'

    if editor_file is None:
        raise zencoding.utils.ZenError("You should save your file before using this action")


    # locate real image path
    real_img_path = zen_file.locate_file(editor_file, img_path)
    if real_img_path is None:
        raise zencoding.utils.ZenError("Can't find '%s' file" % img_path)

    b64 = base64.b64encode(zen_file.read(real_img_path))
    if not b64:
        raise zencoding.utils.ZenError("Can't encode file content to base64")


    b64 = 'data:' + (mime_types[zen_file.get_ext(real_img_path)] or default_mime_type) + ';base64,' + b64

    editor.replace_content('$0' + b64, pos, pos + len(img_path))
    return True
Exemplo n.º 3
0
def get_image_size_for_source(editor, src):
    """
    Returns image dimentions for source
    @param {zen_editor} editor
    @param {String} src Image source (path or data:url)
    """
    if src:
        # check if it is data:url
        if starts_with('data:', src):
            f_content = base64.b64decode( re.sub(r'^data\:.+?;.+?,', '', src) )
        else:
            editor_file = editor.get_file_path()
            
            if editor_file is None:
                raise zencoding.utils.ZenError("You should save your file before using this action")
            
            abs_src = zen_file.locate_file(editor_file, src)
            if not abs_src:
                raise zencoding.utils.ZenError("Can't locate '%s' file" % src)
            
            f_content = zen_file.read(abs_src)
        
        return zencoding.utils.get_image_size(f_content)