Skip to content

Instantly share code, notes, and snippets.

@jonleverrier
Last active December 12, 2018 17:19
Show Gist options
  • Save jonleverrier/7a79b2bdb678f230f66ad5e1bec2ee71 to your computer and use it in GitHub Desktop.
Save jonleverrier/7a79b2bdb678f230f66ad5e1bec2ee71 to your computer and use it in GitHub Desktop.
getImgSizeFromUrl snippet for MODX. Retrieves the height and width of an image from a remote url.
<?php
/**
* getImgSizeFromUrl snippet for MODX
* Retrieves the height and width of an image from a remote url
*
* example usage: [[!getImgSizeFromUrl? &url='https://path/to/myimg.jpg' &tpl=`myTpl`]]
* placeholders to use in the myTpl chunk are [[+height]]] and [[+width]]
*
* @author Jon Leverrier (jon@youandmedigital.com)
* @version 1.0
* @since 10th December 2017
*/
// if the url or tpl is empty
if (empty($url && $tpl)) {
return;
}
else {
// fetch image from remote url
// take width and height values from image
list($width, $height) = getimagesize($url);
// route width and height values to placeholders in tpl chunk
$output = $modx->getChunk($tpl,array(
'width' => $width,
'height' => $height,
));
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment