mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 14:08:32 +08:00
FIX: Perform crop using user-specified image sizes (#9224)
* FIX: Perform crop using user-specified image sizes It used to resize the images to max width and height first and then perform the crop operation. This is wrong because it ignored the user specified image sizes from the Markdown. * DEV: Use real images in test
This commit is contained in:
@ -308,29 +308,34 @@ class CookedPostProcessor
|
||||
end
|
||||
|
||||
def convert_to_link!(img)
|
||||
w, h = img["width"].to_i, img["height"].to_i
|
||||
user_width, user_height = (w > 0 && h > 0 && [w, h]) ||
|
||||
get_size_from_attributes(img) ||
|
||||
get_size_from_image_sizes(img["src"], @opts[:image_sizes])
|
||||
|
||||
limit_size!(img)
|
||||
|
||||
src = img["src"]
|
||||
return if src.blank? || is_a_hyperlink?(img) || is_svg?(img)
|
||||
|
||||
width, height = img["width"].to_i, img["height"].to_i
|
||||
# TODO: store original dimentions in db
|
||||
original_width, original_height = (get_size(src) || [0, 0]).map(&:to_i)
|
||||
|
||||
# can't reach the image...
|
||||
if original_width == 0 || original_height == 0
|
||||
Rails.logger.info "Can't reach '#{src}' to get its dimension."
|
||||
return
|
||||
end
|
||||
|
||||
return if original_width <= width && original_height <= height
|
||||
return if original_width <= SiteSetting.max_image_width && original_height <= SiteSetting.max_image_height
|
||||
|
||||
crop = SiteSetting.min_ratio_to_crop > 0
|
||||
crop &&= original_width.to_f / original_height.to_f < SiteSetting.min_ratio_to_crop
|
||||
user_width, user_height = [original_width, original_height] if user_width.to_i <= 0 && user_height.to_i <= 0
|
||||
width, height = user_width, user_height
|
||||
|
||||
crop = SiteSetting.min_ratio_to_crop > 0 && width.to_f / height.to_f < SiteSetting.min_ratio_to_crop
|
||||
|
||||
if crop
|
||||
width, height = ImageSizer.crop(original_width, original_height)
|
||||
img["width"] = width
|
||||
img["height"] = height
|
||||
width, height = ImageSizer.crop(width, height)
|
||||
img["width"], img["height"] = width, height
|
||||
else
|
||||
width, height = ImageSizer.resize(width, height)
|
||||
end
|
||||
|
||||
# if the upload already exists and is attached to a different post,
|
||||
@ -700,10 +705,7 @@ class CookedPostProcessor
|
||||
|
||||
def post_process_images
|
||||
extract_images.each do |img|
|
||||
unless add_image_placeholder!(img)
|
||||
limit_size!(img)
|
||||
convert_to_link!(img)
|
||||
end
|
||||
convert_to_link!(img) unless add_image_placeholder!(img)
|
||||
end
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user