<p>I don't think it's possible to get the file size of an image after it's been transformed with Craft's out-of-the-box image transforms.</p>

<p>It looks like the <code>.size</code> property only works on the original asset file.</p>

<p>Even if you did something like this...</p>

<pre><code>{% set image = entry.myAssetField.one() %}

{% set thumb = {
    mode: 'crop',
    width: 100,
    height: 100,
    quality: 75,
    position: 'top-center'
} %}

{% if image %}

  {% do image.setTransform(thumb) %}

  {{ image.width }} x {{ image.height }} = {{ image.size }} bytes

{% endif %}
</code></pre>

<p><code>.height</code> and <code>.width</code> properties will show the transformed dimensions (100 x 100), but <code>.size</code> outputs the original file size, not the transformed size.</p>

<p>I'm guessing this might be a problem for Craft because the transformed image may not have been generated before the template is rendered.</p>

<h2>Update</h2>

<p>The <a href="https://github.com/aelvan/Imager-Craft/tree/master#imager_imagemodel" rel="nofollow noreferrer">Imager plugin</a> has a <code>getSize</code> method that should work. Imager transforms are created before the template is rendered.</p>

<p><strong>getSize($unit='b', $precision='2') [float]</strong></p>

<pre><code>{{ image.getSize() }} B 
{{ image.getSize('k') }} KB 
{{ image.getSize('m', 10) }} MB 
{{ image.getSize('g', 10) }} GB
</code></pre>

CC BY-SA 4.0
2018-09-12T19:54:38.177Z
"the transformed image may not have been generated before the template is rendered" good point. Makes me think that on Craft 3 you might be able to use ImageOptimize to define the transform, since those images are generated in advance… but this site is Craft 2. Thoughts @andrew.welch?
henry
CC BY-SA 4.0
2018-09-13T00:25:46.373Z
Try the Imager plugin: https://github.com/aelvan/Imager-Craft/tree/master
Alex Roper
CC BY-SA 4.0
2018-09-13T01:29:25.957Z
Small correction, Imager creates the transforms at template render time. So the size will be available, but the template rendering will also block the first time image transforms are made. Ways around this are to use the Preparse field on the backend, or use a service like Imgix.
andrew.welch
CC BY-SA 4.0
2018-09-12T18:36:40.467Z
2018-09-13T19:04:38.810Z
2018-09-13T19:04:38.810Z
Alex Roper
<p>You can do this via:</p>

<p>Named transforms:</p>

<pre><code>{% someAsset = entry.someAssetField.one() %}
{% if someAsset | length %}
    {{ someAsset.getHeight('transformName') }}
    {{ someAsset.getWidth('transformName') }}
{% endif %}
</code></pre>

<p>Template transforms:</p>

<pre><code>{% someAsset = entry.someAssetField.one() %}
{% if someAsset | length %}
    {{ someAsset.getHeight(transformConfig) }}
    {{ someAsset.getWidth(transformConfig) }}
{% endif %}
</code></pre>

CC BY-SA 4.0
2018-09-12T17:51:46.703Z
This answer seems to be related to image dimensions, not the image's file size (bytes) as the question's title implies.
Alex Roper
CC BY-SA 4.0
2018-09-12T19:55:58.867Z
Yup^ but question for you Andrew in my comment on Alex's answer
henry
CC BY-SA 4.0
2018-09-12T21:28:08.920Z
I guess it'd help if I could read :)
andrew.welch
CC BY-SA 4.0
2018-09-12T17:16:23.337Z
2018-09-12T17:16:23.337Z
andrew.welch
<p>I'm using a transformed image in a context where there's a maximum file size. Is there a way to determine the transformed image's file size in the templates? <code>.size</code> can't run on the url string</p>

<image-transforms>
Is it possible to get a transformed image's file size (bytes, not dimensions)?
# Is it possible to get a transformed image's file size (bytes, not dimensions)?

(Asked by: username_0 on 2018-09-12)

I'm using a transformed image in a context where there's a maximum file size. Is there a way to determine the transformed image's file size in the templates? `.size` can't run on the url string

**Tags:** image-transforms


---

## 2 answers

### Answer by username_1 on 2018-09-12. Score: 5 | Accepted answer

I don't think it's possible to get the file size of an image after it's been transformed with Craft's out-of-the-box image transforms.
It looks like the .size property only works on the original asset file.
Even if you did something like this...
{% set image = entry.myAssetField.one() %}

{% set thumb = {
    mode: 'crop',
    width: 100,
    height: 100,
    quality: 75,
    position: 'top-center'
} %}

{% if image %}

  {% do image.setTransform(thumb) %}

  {{ image.width }} x {{ image.height }} = {{ image.size }} bytes

{% endif %}

.height and .width properties will show the transformed dimensions (100 x 100), but .size outputs the original file size, not the transformed size.
I'm guessing this might be a problem for Craft because the transformed image may not have been generated before the template is rendered.
Update
The Imager plugin has a getSize method that should work. Imager transforms are created before the template is rendered.
getSize($unit='b', $precision='2') [float]
{{ image.getSize() }} B 
{{ image.getSize('k') }} KB 
{{ image.getSize('m', 10) }} MB 
{{ image.getSize('g', 10) }} GB

> **Comments:**
> **username_0**: "the transformed image may not have been generated before the template is rendered" good point. Makes me think that on Craft 3 you might be able to use ImageOptimize to define the transform, since those images are generated in advance… but this site is Craft 2. Thoughts @username_2?  
> **username_1**: Try the Imager plugin: https://github.com/aelvan/Imager-Craft/tree/master  
> **username_2**: Small correction, Imager creates the transforms at template render time. So the size will be available, but the template rendering will also block the first time image transforms are made. Ways around this are to use the Preparse field on the backend, or use a service like Imgix.  

---

### Answer by username_2 on 2018-09-12. Score: 0

You can do this via:
Named transforms:
{% someAsset = entry.someAssetField.one() %}
{% if someAsset | length %}
    {{ someAsset.getHeight('transformName') }}
    {{ someAsset.getWidth('transformName') }}
{% endif %}

Template transforms:
{% someAsset = entry.someAssetField.one() %}
{% if someAsset | length %}
    {{ someAsset.getHeight(transformConfig) }}
    {{ someAsset.getWidth(transformConfig) }}
{% endif %}

> **Comments:**
> **username_1**: This answer seems to be related to image dimensions, not the image's file size (bytes) as the question's title implies.  
> **username_0**: Yup^ but question for you Andrew in my comment on Alex's answer  
> **username_2**: I guess it'd help if I could read :)  

---

