Last active
February 4, 2017 18:57
-
-
Save pepebe/12aa000110da10e5334a21e89caa8e94 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 "<!-- -->"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment