Example #1
0
	def testImageSize(self):
		size = {'width': 25, 'height': 25}
		
		self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.png')))
		self.assertEqual({'width': 125, 'height': 125}, zen.get_image_size(zen_file.read('./assets/sample2.png')))
		self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.gif')))
		self.assertEqual({'width': 300, 'height': 300}, zen.get_image_size(zen_file.read('./assets/sample2.gif')))
		self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.jpg')))
Example #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 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 ZenError("Can't find %s file" % img_path)

    b64 = base64.b64encode(zen_file.read(real_img_path))
    if not b64:
        raise 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
Example #3
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 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 ZenError("Can't find %s file" % img_path)
	
	b64 = base64.b64encode(zen_file.read(real_img_path))
	if not b64:
		raise 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
Example #4
0
def update_image_size(editor):
    """
	Updates <img> tag's width and height attributes
	@type editor: ZenEditor
	@since: 0.65
	"""
    editor_file = editor.get_file_path()
    caret_pos = editor.get_caret_pos()

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

    image = _find_image(editor)
    if image:
        # search for image path
        m = re.search(r'src=(["\'])(.+?)\1', image['tag'], re.IGNORECASE)
        if m:
            src = zen_file.locate_file(editor.get_file_path(), m.group(2))
            if not src:
                raise ZenError("Can't locate file %s" % m.group(2))

            stream = zen_file.read(src)
            if not stream:
                raise ZenError("Can't read file %s" % src)

            size = zen_coding.get_image_size(zen_file.read(src))
            if size:
                new_tag = _replace_or_append(image['tag'], 'width',
                                             size['width'])
                new_tag = _replace_or_append(new_tag, 'height', size['height'])

                # try to preserve caret position
                if caret_pos < image['start'] + len(new_tag):
                    relative_pos = caret_pos - image['start']
                    new_tag = new_tag[:
                                      relative_pos] + zen_coding.get_caret_placeholder(
                                      ) + new_tag[relative_pos:]

                editor.replace_content(new_tag, image['start'], image['end'])
                return True

    return False
Example #5
0
def update_image_size(editor):
	"""
	Updates <img> tag's width and height attributes
	@type editor: ZenEditor
	@since: 0.65
	"""
	editor_file = editor.get_file_path()
	caret_pos = editor.get_caret_pos()
		
	if editor_file is None:
		raise ZenError("You should save your file before using this action")
	
	image = _find_image(editor)
	if image:
		# search for image path
		m = re.search(r'src=(["\'])(.+?)\1', image['tag'], re.IGNORECASE)
		if m:
			src = zen_file.locate_file(editor.get_file_path(), m.group(2))
			if not src:
				raise ZenError("Can't locate file %s" % m.group(2))
			
			stream = zen_file.read(src)
			if not stream:
				raise ZenError("Can't read file %s" % src)
			
			size = zen_coding.get_image_size(zen_file.read(src))
			if size:
				new_tag = _replace_or_append(image['tag'], 'width', size['width'])
				new_tag = _replace_or_append(new_tag, 'height', size['height'])
				
				# try to preserve caret position
				if caret_pos < image['start'] + len(new_tag):
					relative_pos = caret_pos - image['start']
					new_tag = new_tag[:relative_pos] + zen_coding.get_caret_placeholder() + new_tag[relative_pos:]
				
				editor.replace_content(new_tag, image['start'], image['end'])
				return True
				
	return False