pepebe
2/4/2017 - 4:48 PM

Return useful image properties as placeholders. Available properties are height, width, type, attribute, orientation and file size. Can be u

Return useful image properties as placeholders. Available properties are height, width, type, attribute, orientation and file size. Can be used as an output filter or as standard snippet.

<?php

/*
ppb_ImageInfo
v0.1
2017-02-04 19:45

by info@pepebe.de

This is a MODx snippet. Use it as an output filter or as a standard snippet:

[[+tv.thumb:ppb_ImageInfo=`img.`]]
or
[[ppb_ImageInfo? &input=`[[+tv.thumb]] &options=`img.]]

&input=`add_filename_and_path_here`
&options=`add_tv_prefix_here`

If a valid image was found you can use the following placeholders in your chunks:

[[+img.height]]
[[+img.width]]
[[+img.type]]
[[+img.attr]]
[[+img.orientation]]
[[+img.filesize]
*/

<?php

if(!function_exists('human_filesize')){
    function human_filesize($bytes, $decimals = 2) {
        /* http://jeffreysambells.com/2012/10/25/human-readable-filesize-php */
        $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
        $factor = floor((strlen($bytes) - 1) / 3);
        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
    }
}

$prefix = !empty($options) ? $options : 'image.';

$basepath = $modx->getOption('base_path');
$file = $basepath.$input;

if(!file_exists($file)){
    return;
}

/* Basic image properties */

list($width, $height, $type, $attr) = getimagesize($file);

$ph = array();
$ph['width'] = $width;
$ph['height'] = $height;
$ph['type'] = $type;
$ph['attr'] = $attr;

/* Image orientation */

if($width > $height){
    $ph['orientation'] = 'landscape';
}
elseif($width < $height){
    $ph['orientation'] = 'portrait';
}
else {
    $ph['orientation'] = 'square';
}

/* Filesize */

$ph['filesize'] = human_filesize(filesize($file));


foreach($ph as $key => $value){
    $modx->setPlaceholder($prefix.$key, $value);
}

return "<!-- -->";