We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6699
    • 11 Posts
    Hi guys,

    Has anyone had an experience or success getting PHX or another snippet to read and output the filesize of a file/image specified in a TV? Idea is to do this at render-time, but other suggestions would be apprecaited too!

    Cheers,

    Dave
      • 3749
      • 24,544 Posts
      You could do it with a custom PHX modifier, but since you’d have to write your own snippet for that, you might just as well just do it with a snippet. You can send the file path as a property to the snippet and have the snippet get the file size in PHP and return it:

      return filesize($filePath);  


      The size will be returned in bytes, but you could change it to whatever you want in the snippet.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 6699
        • 11 Posts
        Thanks BobRay - much appreciated. Processing time is probably going to be our next hurdle as there are a number of files being read...
          • 3749
          • 24,544 Posts
          Quote from: Dave at Jul 27, 2011, 05:53 AM

          Thanks BobRay - much appreciated. Processing time is probably going to be our next hurdle as there are a number of files being read...

          The snippet would definitely be faster than PHX here. PHX would search through all the other modifiers before exhausting the list and looking for your custom one.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 6699
            • 11 Posts
            Quote from: BobRay at Jul 28, 2011, 05:24 AM

            The snippet would definitely be faster than PHX here.

            Thanks BobRay - we’ll give it a shot!

            DB
              • 19369
              • 1,098 Posts
              Try this for the image size:

              Snippet Name:
              imageinfo

              Snippet Code:
              <?php
              /**
               * Snippet Name: imageinfo
               * Short Desc: Get Image Information
               * Author: Lorenzo Stanco <[email protected]>
               * Version: 1.0.0
               * Last Edited: 21-04-2011
               */
              
                // ---------------------------------------------------
                // Config
                // ---------------------------------------------------
                // $filename: [string] name of original file, default directory: 'assets/images/'
                // $type:     [string] one of resolution|resolutionx|resolutiony|resolutionlabel|filesize
                //-----------------------------------------------------
                //
                // 
                // PARAMETERS
                // resolution: 4000 x 3000 px
                // resolutionx: 4000 
                // resolutiony: 3000 
                // resolutionlabel: XLarge
                // filesize: 5,45 MB
                // 
                //   
                // EXAMPLES
                // [[imageinfo? &filename=`[*img1*]` &type=`resolution`]]
                // [[imageinfo? &filename=`[*img1*]` &type=`resolutionlabel`]]
                // [[imageinfo? &filename=`[*img1*]` &type=`resolutionx`]]
                // [[imageinfo? &filename=`[*img1*]` &type=`resolutiony`]]
                // [[imageinfo? &filename=`[*img1*]` &type=`filesize`]]  
                //-----------------------------------------------------
              
              $pathImg = ((substr($filename, 0, 7) == 'http://') ? $filename : 'assets/images/' . str_replace('assets/images/', '', $filename));
              
              if ($type != 'filesize') {
              	$imageinfo = getimagesize($pathImg);
              	$x = $imageinfo[0];
              	$y = $imageinfo[1];
              }
              
              switch ($type) {
              	case 'filesize':
              		$val = filesize($pathImg);
              		$round = 2;
              		$unit = array('','K','M','G','T','P','E','Z','Y');
              		do { $val /= 1024; array_shift($unit); } while ($val >= 1000);
              		return str_replace('.', ',', sprintf('%.' . intval($round) . 'f', $val)) . ' ' . array_shift($unit) . 'B';
              	case 'resolution': 	
              		return $x . ' x ' . $y . ' px';
              	case 'resolutionx': 
              		return $x;
              	case 'resolutiony': 
              		return $y;
              	case 'resolutionlabel': 
              		$sqp = $x * $y;
              		if ($sqp >= 12000000) return 'XLarge';
              		if ($sqp >= 4000000) return 'Large';
              		if ($sqp >= 1800000) return 'Medium';
              		if ($sqp >= 450000) return 'Small';
              		return 'XSmall';
              }
              
              return '';
              ?>