# 寸法が1未満のSVG画像はアップロードに失敗します

**URL:** https://meta.discourse.org/t/svg-images-with-dimension-less-than-1-fail-to-upload/194009
**Category:** Bug
**Created:** [2021 年 6 月 16 日午前 5:08 UTC](https://meta.discourse.org/t/svg-images-with-dimension-less-than-1-fail-to-upload/194009 "2021-06-16T05:08:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![µMerlin](https://avatars.discourse-cdn.com/v4/letter/%C2%B5/e56c9b/32.png) [@µMerlin](https://meta.discourse.org/u/%C2%B5Merlin)
#### Post date: [2021 年 6 月 16 日午前 5:08 UTC](https://meta.discourse.org/t/svg-images-with-dimension-less-than-1-fail-to-upload/194009/1 "2021-06-16T05:08:20Z")

</div>

SVG画像を、メインのsvg要素のheight属性またはwidth属性のどちらかが「1」未満の数値サイズ（関連する単位は無視）で設定された投稿にアップロード（含める）すると、以下のメッセージで失敗します。

> お詫び申し上げますが、画像のサイズを特定できませんでした。画像が破損している可能性があります。

どうやら、height属性とwidth属性がチェックされ、実際の表示サイズを計算するために単位情報を参照するコード（[https://github.com/discourse/discourse/pull/12230）が実行される前に、アップロードが却下されているようです。以下の検索クエリでフォーラムを検索すると、現在9件の結果が返ってきます。](https://github.com/discourse/discourse/pull/12230%EF%BC%89%E3%81%8C%E5%AE%9F%E8%A1%8C%E3%81%95%E3%82%8C%E3%82%8B%E5%89%8D%E3%81%AB%E3%80%81%E3%82%A2%E3%83%83%E3%83%97%E3%83%AD%E3%83%BC%E3%83%89%E3%81%8C%E5%8D%B4%E4%B8%8B%E3%81%95%E3%82%8C%E3%81%A6%E3%81%84%E3%82%8B%E3%82%88%E3%81%86%E3%81%A7%E3%81%99%E3%80%82%E4%BB%A5%E4%B8%8B%E3%81%AE%E6%A4%9C%E7%B4%A2%E3%82%AF%E3%82%A8%E3%83%AA%E3%81%A7%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A9%E3%83%A0%E3%82%92%E6%A4%9C%E7%B4%A2%E3%81%99%E3%82%8B%E3%81%A8%E3%80%81%E7%8F%BE%E5%9C%A89%E4%BB%B6%E3%81%AE%E7%B5%90%E6%9E%9C%E3%81%8C%E8%BF%94%E3%81%A3%E3%81%A6%E3%81%8D%E3%81%BE%E3%81%99%E3%80%82)

> svg “determine the size of the image” “maybe your image is corrupted”

[How to include fontawesome icons in posts?](https://meta.discourse.org/t/how-to-include-fontawesome-icons-in-posts/31130/14) という投稿も同様の原因によるものと思われますが、その報告は古く、修正済みとされています。

調査したところ、これは使用されているfastimageライブラリに起因する問題のようです。[SVG images with small dimension detected as zero pixels, ignoring units](https://github.com/sdsykes/fastimage/issues/129) という件名で同ライブラリ側にissueを作成し、ここで参照しています。fastimageライブラリが修正されれば、Discourse側でも更新されたバージョンを取得するための変更が必要になるでしょう。

以下は、失敗したサンプルSVGファイルのXMLソースです。

```xml
<?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 issueの添付ファイルと一致します。

![image2](https://global.discourse-cdn.com/meta/original/3X/2/6/267a976fa65b2a7fddf81077efb718a9e4094a9c.svg)

 ![image3](https://global.discourse-cdn.com/meta/original/3X/9/6/96b55d397e6c9d01ca618826a3e709de5cfc22f6.svg) ![image4](https://global.discourse-cdn.com/meta/original/3X/3/8/38ab63a72bd8fc09fca9170d70cea0b465ca94b7.svg)

---

<div class="post-metadata">

### Author: ![jamie.wilson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamie.wilson/32/185466_2.png) [@jamie.wilson](https://meta.discourse.org/u/jamie.wilson)
#### Post date: [2021 年 6 月 16 日午後 10:11 UTC](https://meta.discourse.org/t/svg-images-with-dimension-less-than-1-fail-to-upload/194009/4 "2021-06-16T22:11:21Z")

</div>

> [@µMerlin](#):
>
> fastimage ライブラリが修正されたとしても、更新されたバージョンを取得するには Discourse 側の変更が必要になる可能性が高いでしょう。

問題の詳細な説明をありがとうございます。

私もこの問題を再現することができました。おそらく、このプルリクエストがご指摘の問題を解決するはずです。

> <https://github.com/discourse/discourse/pull/13409>
>
> \`UploadCreator\` counts the number of pixels in an file to determine if it is val…id. \`pixels\` is calculated by multiplying the width and height of the image, as determined by FastImage.
> 
> SVG files can have their width/height expressed in a variety of different units of measurement. For example, ‘px’, ‘in’, ‘cm’, ‘mm’, ‘pt’, ‘pc’, etc are all valid within SVG files. If an image has a width of \`0.5in\`, FastImage may interpret this as being a width of \`0\`, meaning it will report the \`size\` as being \`0\`.
> 
> However, we don’t need to concern ourselves with the number of ‘pixels’ in a SVG files, as that is irrelevant for this file format, so we can skip over the check for \`pixels == 0\` when processing this file type.

編集：不思議ですね…ローカルではテストがパスするのですが、プッシュすると失敗してしまいます。明日詳しく調べてみます。

---

<div class="post-metadata">

### Author: ![jamie.wilson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamie.wilson/32/185466_2.png) [@jamie.wilson](https://meta.discourse.org/u/jamie.wilson)
#### Post date: [2021 年 6 月 21 日午後 4:03 UTC](https://meta.discourse.org/t/svg-images-with-dimension-less-than-1-fail-to-upload/194009/5 "2021-06-21T16:03:23Z")

</div>

先週マージされました。

@µMerlin さん、ご自身の環境で更新を行い、問題ないか確認していただけますでしょうか？

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [2021 年 6 月 26 日午前 8:00 UTC](https://meta.discourse.org/t/svg-images-with-dimension-less-than-1-fail-to-upload/194009/6 "2021-06-26T08:00:55Z")

</div>

このトピックは4日後に自動的に閉鎖されました。新しい返信は受け付けられません。
