Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG images with small dimension detected as zero pixels, ignoring units #129

Closed
mMerlin opened this issue Jun 16, 2021 · 3 comments
Closed

Comments

@mMerlin
Copy link

mMerlin commented Jun 16, 2021

Discourse uses fastimage as an initial check on image sizes. Fastimage detects svg images that have the "height" or "width" attribute value less than "1" as zero pixels. That ignores the units specified with the attribute value. If it was less than "1px", zero could be a reasonable interpretation. However less than "1in" is considerably different. Especially when the "viewBox" value shows that 1 inch is actually 1000 units. The (current) associated discourse code is quoted here

  def extract_image_info!
    @image_info = FastImage.new(@file) rescue nil
    @file.rewind

    if @image_info.nil?That is
      @upload.errors.add(:base, I18n.t("upload.images.size_not_found"))
    elsif max_image_pixels > 0 && pixels >= max_image_pixels * 2
      @upload.errors.add(:base, I18n.t("upload.images.larger_than_x_megapixels", max_image_megapixels: SiteSetting.max_image_megapixels * 2))
    end
  end

For the problem images, the size_not_found error is displayed, indicating pixels == 0.

Here is a minimal failing example

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg
  xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"
  width="1.2in" height="0.9in"
  viewBox="0 0 1200 900"
>
  <line x1="1100" x2="100" y1="100" y2="800" stroke="#000000" stroke-width="50"/>
</svg>

github does support uploading svg files, so I renamed to append ".txt" to the name.

image1.svg.txt

Increasing the size and adjusting viewbox to maintain aspect ratio to height="1in" viewBox="0 0 1200 1000" works.

image2.svg.txt

Doubling the height and width to width="2.4in" height="1.8in", then scaling back down in the post works.

image3.svg.txt

Changing the 0.9in height to the equivalent 22.86mm works.

image4.svg.txt

None of those workarounds are helpful for a typical forum user (https://forum.fritzing.org/). The images are associated with an application, where dimensions have meaning beyond the display size. Typical users works with inkscape, without knowing anything about the raw svg xml. Or the image is out of a part file created by other users or application code. The units are typically inches, because the convention is 0.1 spacing for many things.

@SamSaffron
Copy link
Collaborator

Looks like we have a fix in Discourse. Not sure if we need to change anything with the gem.

@mMerlin
Copy link
Author

mMerlin commented Jun 17, 2021

Looks like we have a fix in Discourse. Not sure if we need to change anything with the gem.

That would depend on whether anyone else is using fastimage for something similar. The latest information for the Discourse fix is still tentative.

@sdsykes
Copy link
Owner

sdsykes commented Jun 17, 2021

Yes, thanks for the detailed description. I do think it would be useful to somehow fix this case in FastImage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants