import deterfile
import getjpggeom
-usage = """usage: %s <target file> <output_file> <image_dir> [link_prefix] [image_width]""" % (os.path.basename(sys.argv[0]),)
+usage = """usage: %s <target file> <output_file> <image_dir> [link_prefix] [max_image_width] [max_image_height]""" % (os.path.basename(sys.argv[0]),)
rex_imgtag = re.compile(r"""<img\s+src=["'](.*?)["'].*?>""")
rex_atag = re.compile(r"""<a\s+href=["'](.*?)["'].*?>""")
link_prefix = ""
try:
- image_width = int(sys.argv[5])
+ max_image_width = int(sys.argv[5])
except IndexError:
- image_width = 480
+ max_image_width = 480
+try:
+ max_image_height = int(sys.argv[6])
+except IndexError:
+ max_image_height = 640
def _get_png_geom(filepath):
s = filepath.split('.')
attrs = htmltaglib.parse_attributes(tagstr)
(root, ext) = os.path.splitext(os.path.basename(path))
+ (w, h) = _get_png_geom(path)
- filename = ""
- if 'width' in attrs:
- (w, h) = _get_png_geom(path)
- if int(w) > image_width:
- attrs['height'] = str(h * image_width / w)
- attrs['width'] = str(image_width)
- filename = '''%s-%sx%s%s''' % (root, attrs['width'], attrs['height'], ext)
- else:
- attrs['height'] = str(h)
- attrs['width'] = str(w)
- filename = '''%s%s''' % (root, ext)
+ if (w <= max_image_width) and (h <= max_image_height):
+ # use image as-is.
+ filename = root + ext
+ attrs['height'] = str(h)
+ attrs['width'] = str(w)
+ else:
+ # use resized image
+ new_w = int(w)
+ new_h = int(h)
+ if new_w > max_image_width:
+ new_h = h * max_image_width / w
+ new_w = max_image_width
+ print "replace width, %d %d" % (new_w, new_h)
+ if new_h > max_image_height:
+ new_h = max_image_height
+ new_w = w * max_image_height / h
+ print "replace height, %d %d" % (new_w, new_h)
+ attrs['height'] = str(new_h)
+ attrs['width'] = str(new_w)
+ filename = '''%s-%sx%s%s''' % (root, attrs['width'], attrs['height'], ext)
wp_image_url = '''%s%s''' % (image_dir, filename)
attrs['src'] = wp_image_url
def replace_figure_tag(line, tagstr, path):
attrs = htmltaglib.parse_attributes(tagstr)
- width = image_width
+ width = max_image_width
if 'style' in attrs:
m = re.search(ur'width:\s*([0-9]+)px', attrs['style'])
if m: