@Mark H: Is this [[++setting_phpthumb_nohotlink_enabled]] ?
if ($dimensions = getimagesize($config['base_path'].$filename, $info))
[2011-06-03 18:00:54] (ERROR @ /myweb/core/cache/includes/elements/modplugin/2.include.cache.php : 36) PHP warning: getimagesize(/myweb/http://www.adobe.com/images/shared/download_buttons/get_adobe_flash_player.png) [<a href='function.getimagesize'>function.getimagesize</a>]:
// find all img elements with a src attribute
preg_match_all('|\<img.*?src=[",\'](.*?)[",\'].*?[^>]+\>|i', $str, $filenames);
I suggest to rethink about line 30:
if ($dimensions = getimagesize($config['base_path'].$filename, $info))
...because it throws errors in MODX error log, if there are external images. For instance:
[2011-06-03 18:00:54] (ERROR @ /myweb/core/cache/includes/elements/modplugin/2.include.cache.php : 36) PHP warning: getimagesize(/myweb/http://www.adobe.com/images/shared/download_buttons/get_adobe_flash_player.png) [<a href='function.getimagesize'>function.getimagesize</a>]:
Maybe the regex in line 21 could be enhancend?
// find all img elements with a src attribute preg_match_all('|\<img.*?src=[",\'](.*?)[",\'].*?[^>]+\>|i', $str, $filenames);
I think it would be better if every src with preceeding http or https is considered external and isn’t put into the array $filenames. I would like to help, but I’m not sure if I’m capable to find the appropriate regex for this...
<?php /** * @name AutoFixImageSize * @version 1.0.0 rc2 * @author Gerrit van Aaken <[email protected]> April–June 2011 * * @license GPLv2 * * Fixes img elements with wrong width/height attributes. * Uses phpThumbOf for generating correctly sized physical image files. * * Must be executed at "OnWebPagePrerender" */ // get parsed document as string $str = $modx->resource->_output; // get configuration from global object $config = $modx->getConfig(); // find all img elements with a src attribute preg_match_all('|\<img.*?src=[",\'](.*?)[",\'].*?[^>]+\>|i', $str, $filenames); // loop through all found img elements foreach($filenames[1] as $i => $filename) { $img_old = $filenames[0][$i]; $allowcaching = false; // pessimistic // is file already cached? if (strpos($filename,"connector.php?") == false) { // check if external caching is allowed if (substr($filename,0,7) == "http://" || substr($filename,0,8) == "https://") { $pre = ""; if ($config['setting_phpthumb_nohotlink_enabled']) { foreach (explode(",", $config['setting_phpthumb_nohotlink_valid_domains']) as $alldomain) { if ( strpos(strtolower($filename), strtolower(trim($alldomain))) != false) { $allowcaching = true; } } } else { $allowcaching = true; } } else { $pre = $config['base_path']; $allowcaching = true; } } // do we have physical access to the file? if ($allowcaching && $dimensions = getimagesize($pre.$filename, $info)) { // find width and height attribut and save value preg_match_all('|width=[",\']([0-9]+?)[",\']|i', $filenames[0][$i], $widths); $width = $widths[1][0]; preg_match_all('|height=[",\']([0-9]+?)[",\']|i', $filenames[0][$i], $heights); $height = $heights[1][0]; // if resizing needed... if (($width && $width != $dimensions[0]) || ($height && $height != $dimensions[1])) { // prepare resizing metadata $filetype = strtolower(substr($filename, strrpos($filename,".")+1)); $image = array(); $image['input'] = $filename; $image['options'] = "f=".$filetype."&h=".$height."&w=".$width; // perform physical resizing and caching via phpthumbof $cacheurl = $modx->runSnippet('phpthumbof',$image); // set freshly cached image file location into old src attribute $img_new = str_replace($filename, $cacheurl, $img_old); // replace old image element with new one on whole page content $str = str_replace($img_old, $img_new, $str); } } } // exchange the output string with the replaced one $modx->resource->_output = $str;
Gerrit, you’re the fastest developer on earth
I added some barriers for external images to be cached, so could you please test this new version of the plugin? Would like to hear your opinion before I realease this as an update:

[2011-06-04 00:44:09] (ERROR @ /myweb/core/cache/includes/elements/modplugin/2.include.cache.php : 57) PHP warning: getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: URL file-access is disabled in the server configuration [2011-06-04 00:44:09] (ERROR @ /myweb/core/cache/includes/elements/modplugin/2.include.cache.php : 57) PHP warning: getimagesize(http://www.adobe.com/images/shared/download_buttons/get_adobe_flash_player.png) [<a href='function.getimagesize'>function.getimagesize</a>]: failed to open stream: no suitable wrapper could be found
if ($allowcaching && $dimensions = getimagesize($pre.$filename, $info)) {if ($config['setting_phpthumb_nohotlink_enabled']) {
// NOT TRUE
} else {
$allowcaching = true; // BECOMES TRUE
}<?php /** * @name AutoFixImageSize * @version 1.0.0 rc2 * @author Gerrit van Aaken <[email protected]> April–June 2011 * * @license GPLv2 * * Fixes img elements with wrong width/height attributes. * Uses phpThumbOf for generating correctly sized physical image files. * * Must be executed at "OnWebPagePrerender" */ // $modx->setDebug(E_USER_ERROR); // $modx->setLogLevel(modX::LOG_LEVEL_DEBUG); // get parsed document as string $str = $modx->resource->_output; // get configuration from global object $config = $modx->getConfig(); // find all img elements with a src attribute preg_match_all('|\<img.*?src=[",\'](.*?)[",\'].*?[^>]+\>|i', $str, $filenames); // loop through all found img elements foreach($filenames[1] as $i => $filename) { $img_old = $filenames[0][$i]; $allowcaching = false; // pessimistic // $modx->log(modX::LOG_LEVEL_DEBUG, 'Prevent hotlinking: '.$config['phpthumb_nohotlink_enabled']); // is file already cached? if (strpos($filename,"phpthumb") == false) { // check if external caching is allowed if (substr($filename,0,7) == "http://" || substr($filename,0,8) == "https://") { $pre = ""; if ($config['phpthumb_nohotlink_enabled']) { foreach (explode(",", $config['phpthumb_nohotlink_valid_domains']) as $alldomain) { if ( strpos(strtolower($filename), strtolower(trim($alldomain))) != false) { $allowcaching = true; } } } else { $allowcaching = true; } } else { $pre = $config['base_path']; $allowcaching = true; } } // do we have physical access to the file? if ($allowcaching && $dimensions = getimagesize($pre.$filename, $info)) { // find width and height attribut and save value preg_match_all('|width=[",\']([0-9]+?)[",\']|i', $filenames[0][$i], $widths); $width = $widths[1][0]; preg_match_all('|height=[",\']([0-9]+?)[",\']|i', $filenames[0][$i], $heights); $height = $heights[1][0]; // if resizing needed... if (($width && $width != $dimensions[0]) || ($height && $height != $dimensions[1])) { // prepare resizing metadata $filetype = strtolower(substr($filename, strrpos($filename,".")+1)); $image = array(); $image['input'] = $filename; $image['options'] = "f=".$filetype."&h=".$height."&w=".$width; // perform physical resizing and caching via phpthumbof $cacheurl = $modx->runSnippet('phpthumbof',$image); // set freshly cached image file location into old src attribute $img_new = str_replace($filename, $cacheurl, $img_old); // replace old image element with new one on whole page content $str = str_replace($img_old, $img_new, $str); } } } // exchange the output string with the replaced one $modx->resource->_output = $str;
if ($allowcaching && $dimensions = getimagesize($pre.$filename, $info)) {$filename = str_replace('%20', ' ', $filename);$filename = str_replace(' ', '%20', $filename);
The newest version rc3 tolerates spaces in filenames. Check it out!
$image['options'] = "f=".$filetype."&h=".$height."&w=".$width;