2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-08-20 18:52:44 +08:00

automatically resizes images

This commit is contained in:
Régis Hanol 2013-04-13 16:31:20 +02:00
parent 97ba06d954
commit c2e58b61c9
8 changed files with 131 additions and 64 deletions

View file

@ -2,9 +2,9 @@
# example, inserting the onebox content, or image sizes.
require_dependency 'oneboxer'
require_dependency 'image_optimizer'
class CookedPostProcessor
require 'open-uri'
def initialize(post, opts={})
@dirty = false
@ -34,58 +34,59 @@ class CookedPostProcessor
images = @doc.search("img")
return unless images.present?
images.each do |img|
src = img['src']
src = Discourse.base_url_no_prefix + src if src[0] == "/"
if src.present?
if img['width'].blank? || img['height'].blank?
w, h = get_size_from_image_sizes(src, @opts[:image_sizes]) || image_dimensions(src)
if w && h
img['width'] = w.to_s
img['height'] = h.to_s
@dirty = true
end
end
if src != img['src']
img['src'] = src
@dirty = true
end
convert_to_link!(img)
img['src'] = optimize_image(img)
end
end
# Extract the first image from the first post and use it as the 'topic image'
if @post.post_number == 1
img = images.first
@post.topic.update_column :image_url, img['src'] if img['src'].present?
end
images.each do |img|
src = img['src']
src = Discourse.base_url_no_prefix + src if src[0] == "/"
if src.present? && (img['width'].blank? || img['height'].blank?)
w,h =
get_size_from_image_sizes(src, @opts[:image_sizes]) ||
image_dimensions(src)
if w && h
img['width'] = w.to_s
img['height'] = h.to_s
@dirty = true
end
end
if src.present?
if src != img['src']
img['src'] = src
@dirty = true
end
convert_to_link!(img)
img.set_attribute('src', optimize_image(src))
end
end
end
def optimize_image(src)
# uri = get_image_uri(src)
# uri.open(read_timeout: 20) do |f|
#
# end
def optimize_image(img)
src = img["src"]
src
# supports only local uploads
return src if SiteSetting.enable_imgur? || SiteSetting.enable_s3_uploads?
width, height = img["width"].to_i, img["height"].to_i
ImageOptimizer.new(src).optimized_image_url(width, height)
end
def convert_to_link!(img)
src = img["src"]
width = img["width"].to_i
height = img["height"].to_i
width, height = img["width"].to_i, img["height"].to_i
return unless src.present? && width > SiteSetting.auto_link_images_wider_than
original_width, original_height = get_size(src)
original_width, original_height = get_size(src)
return unless original_width.to_i > width && original_height.to_i > height