We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    From what I can tel I can get an images dimmenssions with the getimagesize() function. In the docs it reads:
    Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag
    But i was unable to figure out what to do with this. I found a tutorial with this code but I don’t understand it:
    <?php list($w, $h) = getimagesize("Images/Sunset.jpg") ?>
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don&#39;t know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 7923
      • 4,213 Posts
      <?php
      $imagesize = getimagesize($modx->config['base_path'].'Images/Sunset.jpg');
      $width = $imagesize[0]; // width
      $height = $imagesize[1]; // height
      $type = $imagesize[2]; // One of the IMAGETYPE_XXX constants indicating the type of the image.
      $size_str = $imagesize[3]; // A text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.
      $mime_type = $imagesize['mime']; // The correspondant MIME type of the image. This information can be used to deliver images with correct the HTTP Content-type header.
      $channels = $imagesize['channels']; // 3 for RGB pictures and 4 for CMYK pictures.
      $bits = $imagesize['bits']; // The number of bits for each color. 
      ?>
      


      You could of course find out those yourself by doing a test and printing the array from the getimagesize function, eg:

      <?php
      print_r(getimagesize($modx->config['base_path'].'Images/Sunset.jpg'));
      ?>
      


      That usually get’s you going when you don’t know exactly what kind of data the function outputs or cannot understand from the docs. You can also print_r objects in PHP.

      EDIT: Also great thing about this function is that it doesn’t require GD to work.


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 7231
        • 4,205 Posts
        Thanks, that is a good tip (the worst thing is that I knew that, but did not think..can’t forget to think). grin

        So $imagesize[3] actually outputs a formatted string - height="yyy" width="xxx". That is interesting. So we can simply add $imagesize[3] directly in the img tag:
        <img srg="file/path.jpd" <?php echo $imagesize[3]; ?>/>
        and the output will be
        <img srg="file/path.jpd" height="yyy" width="xxx"/>

        Not needing GD is great. I am working on an application where the image width/height need to be displayed with the image. Would it be better to store the h/w information in the database or to get it dynamically?
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don&#39;t know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 7923
          • 4,213 Posts
          It would be better to store the size to a DB and retrieve from there (unless if your DB server is very slow due to shared environments or something). The getimagesize function needs to access file system and parse the image file header, so obviously it would be faster to just query the size from DB.

          That being said, MaxiGallery snippet doesn’t save the sizes of the pictures to a DB, it get’s them by using getimagesize on each page load. It’s bad, I know, but it’s not so slow that I would fix it anytime soon.

          But I’d definitely suggest to save the sizes to DB.


            "He can have a lollipop any time he wants to. That's what it means to be a programmer."
            • 7231
            • 4,205 Posts
            But I’d definitely suggest to save the sizes to DB.
            Thanks for the tip. I will get the size with getimagesize() and then store to the db.
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 33372
              • 1,611 Posts
              Quote from: doze at Oct 12, 2008, 03:23 PM

              But I’d definitely suggest to save the sizes to DB.
              Or call your snippet cached so that it only needs to check the image sizes the first time it’s executed.
                "Things are not what they appear to be; nor are they otherwise." - Buddha

                "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options