Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-12T19:54:38.177Z
Answers.Text: "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?
Answers.UserDisplayName: henry
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-13T00:25:46.373Z
Answers.Text: Try the Imager plugin: https://github.com/aelvan/Imager-Craft/tree/master
Answers.UserDisplayName: Alex Roper
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-13T01:29:25.957Z
Answers.Text: 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.
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-12T18:36:40.467Z
Answers.LastActivityDate: 2018-09-13T19:04:38.810Z
Answers.LastEditDate: 2018-09-13T19:04:38.810Z
Answers.UserDisplayName: Alex Roper
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-12T17:51:46.703Z
Answers.Text: This answer seems to be related to image dimensions, not the image's file size (bytes) as the question's title implies.
Answers.UserDisplayName: Alex Roper
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-12T19:55:58.867Z
Answers.Text: Yup^ but question for you Andrew in my comment on Alex's answer
Answers.UserDisplayName: henry
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-12T21:28:08.920Z
Answers.Text: I guess it'd help if I could read :)
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-12T17:16:23.337Z
Answers.LastActivityDate: 2018-09-12T17:16:23.337Z
Answers.UserDisplayName: andrew.welch
Body: <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>
  
Tags: <image-transforms>
Title: Is it possible to get a transformed image's file size (bytes, not dimensions)?
ThreadText: # 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](https://github.com/aelvan/Imager-Craft/tree/master#imager_imagemodel) 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 username_1's answer  
  > **username_2**: I guess it'd help if I could read :)  
  
  ---
  
  