How can I get the width or height of an image field image on Octobercms











up vote
0
down vote

favorite












I try this method to get image dimensions:



{% set image_width = fields.sec_port_image.width %}
{% set image_height = fields.sec_port_image.height %}

<a href="{{ fields.sec_port_image|media }}" width="{{ image_width }}" height="{{ image_height }}">


but without success. I Also tried:



{% set image_width = fields.sec_port_image.width|media %}


and



{% set image_width = fields.sec_port_image|media.width %}


but also without success.



Is there a way on Octobercms to get image dimensions using twig?










share|improve this question






















  • What kind of object is sec_port_image? Does it have the public properties width and height or the public getter getWidth() or getHeight()?
    – DarkBee
    Nov 8 at 14:13












  • sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
    – Charis
    Nov 8 at 15:46










  • As seen in the source the properties are called imageWidth and imageHeight
    – DarkBee
    Nov 8 at 15:49










  • Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
    – Charis
    Nov 8 at 15:59










  • Ah you should mention you are looking for the original measurements. I'd suggest adding a Twig_Filter or something that does this for you then as you can't retrieve the originals one straight from that object
    – DarkBee
    Nov 8 at 16:03

















up vote
0
down vote

favorite












I try this method to get image dimensions:



{% set image_width = fields.sec_port_image.width %}
{% set image_height = fields.sec_port_image.height %}

<a href="{{ fields.sec_port_image|media }}" width="{{ image_width }}" height="{{ image_height }}">


but without success. I Also tried:



{% set image_width = fields.sec_port_image.width|media %}


and



{% set image_width = fields.sec_port_image|media.width %}


but also without success.



Is there a way on Octobercms to get image dimensions using twig?










share|improve this question






















  • What kind of object is sec_port_image? Does it have the public properties width and height or the public getter getWidth() or getHeight()?
    – DarkBee
    Nov 8 at 14:13












  • sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
    – Charis
    Nov 8 at 15:46










  • As seen in the source the properties are called imageWidth and imageHeight
    – DarkBee
    Nov 8 at 15:49










  • Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
    – Charis
    Nov 8 at 15:59










  • Ah you should mention you are looking for the original measurements. I'd suggest adding a Twig_Filter or something that does this for you then as you can't retrieve the originals one straight from that object
    – DarkBee
    Nov 8 at 16:03















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I try this method to get image dimensions:



{% set image_width = fields.sec_port_image.width %}
{% set image_height = fields.sec_port_image.height %}

<a href="{{ fields.sec_port_image|media }}" width="{{ image_width }}" height="{{ image_height }}">


but without success. I Also tried:



{% set image_width = fields.sec_port_image.width|media %}


and



{% set image_width = fields.sec_port_image|media.width %}


but also without success.



Is there a way on Octobercms to get image dimensions using twig?










share|improve this question













I try this method to get image dimensions:



{% set image_width = fields.sec_port_image.width %}
{% set image_height = fields.sec_port_image.height %}

<a href="{{ fields.sec_port_image|media }}" width="{{ image_width }}" height="{{ image_height }}">


but without success. I Also tried:



{% set image_width = fields.sec_port_image.width|media %}


and



{% set image_width = fields.sec_port_image|media.width %}


but also without success.



Is there a way on Octobercms to get image dimensions using twig?







image twig octobercms twig-filter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 14:07









Charis

4228




4228












  • What kind of object is sec_port_image? Does it have the public properties width and height or the public getter getWidth() or getHeight()?
    – DarkBee
    Nov 8 at 14:13












  • sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
    – Charis
    Nov 8 at 15:46










  • As seen in the source the properties are called imageWidth and imageHeight
    – DarkBee
    Nov 8 at 15:49










  • Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
    – Charis
    Nov 8 at 15:59










  • Ah you should mention you are looking for the original measurements. I'd suggest adding a Twig_Filter or something that does this for you then as you can't retrieve the originals one straight from that object
    – DarkBee
    Nov 8 at 16:03




















  • What kind of object is sec_port_image? Does it have the public properties width and height or the public getter getWidth() or getHeight()?
    – DarkBee
    Nov 8 at 14:13












  • sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
    – Charis
    Nov 8 at 15:46










  • As seen in the source the properties are called imageWidth and imageHeight
    – DarkBee
    Nov 8 at 15:49










  • Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
    – Charis
    Nov 8 at 15:59










  • Ah you should mention you are looking for the original measurements. I'd suggest adding a Twig_Filter or something that does this for you then as you can't retrieve the originals one straight from that object
    – DarkBee
    Nov 8 at 16:03


















What kind of object is sec_port_image? Does it have the public properties width and height or the public getter getWidth() or getHeight()?
– DarkBee
Nov 8 at 14:13






What kind of object is sec_port_image? Does it have the public properties width and height or the public getter getWidth() or getHeight()?
– DarkBee
Nov 8 at 14:13














sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
– Charis
Nov 8 at 15:46




sec_port_image is a Octobercms mediafinder widget field (octobercms.com/docs/api/backend/formwidgets/mediafinder).
– Charis
Nov 8 at 15:46












As seen in the source the properties are called imageWidth and imageHeight
– DarkBee
Nov 8 at 15:49




As seen in the source the properties are called imageWidth and imageHeight
– DarkBee
Nov 8 at 15:49












Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
– Charis
Nov 8 at 15:59




Yes, i also try {% set image_width = fields.sec_port_image.imageWidth %} without success. imageWidth points to image preview on Octobercms backend and not to original image uploaded.
– Charis
Nov 8 at 15:59












Ah you should mention you are looking for the original measurements. I'd suggest adding a Twig_Filter or something that does this for you then as you can't retrieve the originals one straight from that object
– DarkBee
Nov 8 at 16:03






Ah you should mention you are looking for the original measurements. I'd suggest adding a Twig_Filter or something that does this for you then as you can't retrieve the originals one straight from that object
– DarkBee
Nov 8 at 16:03














1 Answer
1






active

oldest

votes

















up vote
0
down vote













Extend twig in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)



First create a registration class, see here for a reference.



<?php
class Registration {
public function registerMarkupTags()
{
return [
'filters' => [
// A global function, i.e str_plural()
'image_width' => [$this, 'getImageWidth'],
'image_height' => [$this, 'getImageHeight'],
],
];
}

private $images = ;

public function getImageWidth($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
}

public function getImageHeight($url) {
return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
}

private function getImageSize($url) {
if (!isset($this->images[$url])) {
$data = @getimagesize($url);
if ($data) {
$this->images[$url] = [
'width' => $data[0],
'height' => $data[1],
];
}else{
$this->images[$url] = false;
}
}
}
return $this->images[$url];
}


If done correctly you then could use the new filters inside your template like this



{% set source = fields.sec_port_image|media %}
<a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209420%2fhow-can-i-get-the-width-or-height-of-an-image-field-image-on-octobercms%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Extend twig in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)



    First create a registration class, see here for a reference.



    <?php
    class Registration {
    public function registerMarkupTags()
    {
    return [
    'filters' => [
    // A global function, i.e str_plural()
    'image_width' => [$this, 'getImageWidth'],
    'image_height' => [$this, 'getImageHeight'],
    ],
    ];
    }

    private $images = ;

    public function getImageWidth($url) {
    return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
    }

    public function getImageHeight($url) {
    return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
    }

    private function getImageSize($url) {
    if (!isset($this->images[$url])) {
    $data = @getimagesize($url);
    if ($data) {
    $this->images[$url] = [
    'width' => $data[0],
    'height' => $data[1],
    ];
    }else{
    $this->images[$url] = false;
    }
    }
    }
    return $this->images[$url];
    }


    If done correctly you then could use the new filters inside your template like this



    {% set source = fields.sec_port_image|media %}
    <a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">





    share|improve this answer

























      up vote
      0
      down vote













      Extend twig in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)



      First create a registration class, see here for a reference.



      <?php
      class Registration {
      public function registerMarkupTags()
      {
      return [
      'filters' => [
      // A global function, i.e str_plural()
      'image_width' => [$this, 'getImageWidth'],
      'image_height' => [$this, 'getImageHeight'],
      ],
      ];
      }

      private $images = ;

      public function getImageWidth($url) {
      return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
      }

      public function getImageHeight($url) {
      return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
      }

      private function getImageSize($url) {
      if (!isset($this->images[$url])) {
      $data = @getimagesize($url);
      if ($data) {
      $this->images[$url] = [
      'width' => $data[0],
      'height' => $data[1],
      ];
      }else{
      $this->images[$url] = false;
      }
      }
      }
      return $this->images[$url];
      }


      If done correctly you then could use the new filters inside your template like this



      {% set source = fields.sec_port_image|media %}
      <a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Extend twig in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)



        First create a registration class, see here for a reference.



        <?php
        class Registration {
        public function registerMarkupTags()
        {
        return [
        'filters' => [
        // A global function, i.e str_plural()
        'image_width' => [$this, 'getImageWidth'],
        'image_height' => [$this, 'getImageHeight'],
        ],
        ];
        }

        private $images = ;

        public function getImageWidth($url) {
        return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
        }

        public function getImageHeight($url) {
        return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
        }

        private function getImageSize($url) {
        if (!isset($this->images[$url])) {
        $data = @getimagesize($url);
        if ($data) {
        $this->images[$url] = [
        'width' => $data[0],
        'height' => $data[1],
        ];
        }else{
        $this->images[$url] = false;
        }
        }
        }
        return $this->images[$url];
        }


        If done correctly you then could use the new filters inside your template like this



        {% set source = fields.sec_port_image|media %}
        <a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">





        share|improve this answer












        Extend twig in order to get the original size of your image. Something like this should work (not tested but you'll get the idea)



        First create a registration class, see here for a reference.



        <?php
        class Registration {
        public function registerMarkupTags()
        {
        return [
        'filters' => [
        // A global function, i.e str_plural()
        'image_width' => [$this, 'getImageWidth'],
        'image_height' => [$this, 'getImageHeight'],
        ],
        ];
        }

        private $images = ;

        public function getImageWidth($url) {
        return $this->getImageSize($url) ? $this->getImageSize($url)['width'] : null;
        }

        public function getImageHeight($url) {
        return $this->getImageSize($url) ? $this->getImageSize($url)['height'] : null;
        }

        private function getImageSize($url) {
        if (!isset($this->images[$url])) {
        $data = @getimagesize($url);
        if ($data) {
        $this->images[$url] = [
        'width' => $data[0],
        'height' => $data[1],
        ];
        }else{
        $this->images[$url] = false;
        }
        }
        }
        return $this->images[$url];
        }


        If done correctly you then could use the new filters inside your template like this



        {% set source = fields.sec_port_image|media %}
        <a href="{{ source }}" width="{{ source|image_width }}" height="{{ source|image_height }}">






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 7:00









        DarkBee

        8,51942741




        8,51942741






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53209420%2fhow-can-i-get-the-width-or-height-of-an-image-field-image-on-octobercms%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()