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 improvements, also more verbose images-test page #133

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
matrix:
config:
# [Python version, tox env]
- ["3.7", "plone52-py37"]
- ["3.8", "plone52-py38"]
- ["3.8", "plone60-py38"]
- ["3.9", "plone60-py39"]
Expand Down
21 changes: 11 additions & 10 deletions plone/namedfile/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from Acquisition import aq_base
from DateTime import DateTime
from io import BytesIO
from io import StringIO
from io import TextIOWrapper
from plone.memoize import ram
from plone.namedfile.file import FILECHUNK_CLASSES
from plone.namedfile.browser import ALLOWED_INLINE_MIMETYPES
Expand All @@ -20,9 +22,10 @@
from plone.scale.interfaces import IImageScaleFactory
from plone.scale.interfaces import IScaledImageQuality
from plone.scale.scale import scaleImage
from plone.scale.scale import scale_svg_image
from plone.scale.storage import IImageScaleStorage
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import safe_encode
from Products.CMFPlone.utils import safe_text
from Products.Five import BrowserView
from xml.sax.saxutils import quoteattr
from zExceptions import BadRequest
Expand Down Expand Up @@ -58,7 +61,7 @@ def _image_tag_from_values(*values):
for k, v in values:
if v is None:
continue
if isinstance(v, int):
if isinstance(v, (int, float)):
v = str(v)
elif isinstance(v, bytes):
v = str(v, "utf8")
Expand Down Expand Up @@ -327,14 +330,12 @@ def create_scale(self, data, mode, height, width, **parameters):
def handle_image(self, orig_value, orig_data, mode, height, width, **parameters):
"""Return a scaled image, its mimetype format, and width and height."""
if getattr(orig_value, "contentType", "") == "image/svg+xml":
# No need to scale, we can simply use the original data,
# but report a different width and height.
if isinstance(orig_data, (str)):
orig_data = safe_encode(orig_data)
if isinstance(orig_data, (bytes)):
orig_data = BytesIO(orig_data)
result = orig_data.read(), "svg+xml", (width, height)
return result
if isinstance(orig_data, bytes):
orig_data = StringIO(safe_text(orig_data))
elif isinstance(orig_data, BytesIO):
orig_data = TextIOWrapper(orig_data, encoding="utf-8")
scaled_data, size = scale_svg_image(orig_data, width, height, direction)
return scaled_data, "svg+xml", size
try:
result = self.create_scale(
orig_data, mode=mode, height=height, width=width, **parameters
Expand Down
35 changes: 29 additions & 6 deletions plone/namedfile/test.pt
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,48 @@
</section>
<hr />
<section id="examples">
<h2>Examples with mode</h2>
<h2>Examples with direction/mode</h2>
<p>
There are three modes to scale an image: <code>scale</code>, <code>cover</code> and <code>contain</code>.
</p>
<p>Scaling methods do never stretch/distort the image in one direction only.</p>

<h3>Mini</h3>
<h3>Mini direction=scale</h3>
<p>
Scales to the requested dimensions without cropping.
The resulting image may have a different size than requested.
This option requires both, width and height, to be specified.
Does not scale up. <small>[from doc-string]</small>
</p>
<p>
Here direction is not explicit set, it uses by default <code>direction="scale"</code>.
</p>
<p>Deprecated spellings: <code>keep</code>, <code>thumbnail</code>.</p>
<figure class="figure"
tal:define="img_tag python:images.tag('image', scale='mini')">
<img tal:replace="structure img_tag" />
<br /><code tal:content="img_tag" />
</figure>

<h3 id="cover">Mini mode=cover</h3>
<h3 id="cover">Mini direction=cover</h3>
<p>
Scales the relatively largest dimension up to the required size.
Despite the alternative spelling, I see no cropping happening. <small>[from doc-string]</small>
</p>
<p>Deprecated spellings: <code>scale-crop-to-fill</code>, <code>up</code>.</p>
<figure class="figure"
tal:define="img_tag python:images.tag('image', scale='mini', mode='cover')">
<img tal:replace="structure img_tag" />
<br /><code tal:content="img_tag" />
</figure>

<h3 id="contain">Mini mode=contain</h3>
<figure class="figure"
tal:define="img_tag python:images.tag('image', scale='mini', mode='contain')">
<h3 id="contain">Mini direction=contain</h3>
<p>
Starts by scaling the relatively smallest dimension to the required size and crops the other dimension if needed. <small>[from doc-string]</small>
</p>
<p>Deprecated spellings: <code>scale-crop-to-fit</code>, <code>down</code>.</p>
<figure class="figure"
tal:define="img_tag python:images.tag('image', scale='mini', direction='contain')">
<img tal:replace="structure img_tag" />
<br /><code tal:content="img_tag" />
</figure>
Expand Down
Loading