А мне понравился плагин, простой и без лишних деталей.
- Прописал @internal @events OnWebPagePrerender, теперь перед инсталляцией можно закинуть в папку modx-1.0.5\install\assets\plugins\autoResize.tpl
- если для узкой/высокой картинки задать высоту, ширину и прописать class="crop", она смасштабируется по узкому краю, а лишее обрежется:
//<?php
/**
* AutoResizer
*
* @name AutoResizer
* @category plugin
* @version 1.0.1
* @author Armand "bS" Pondman ([email protected]) modifed by Rariteth and iZenin
* @desc Replaces document content images coming from the assets/images/ folder to cache folder. If you need to scale into rect and crop image outside this rect, set class="crop".
* @internal @events OnWebPagePrerender
* @internal @modx_category Manager and Admin
*/
// follow width or height reference:
// Settings
$q = '90';
$cachePath = $modx->config['base_path'].'assets/cache/';
// Do not edit below this line
$pattern = '/<img[^>]*>/';
$e = &$modx->Event;
switch ($e->name) {
case "OnWebPagePrerender":
$o = $modx->documentOutput; // get the output
preg_match_all($pattern, $o, $out); // get all images
$replace = $out;
for($n=0;$n<count($replace[0]);$n++){
if (preg_match('/width="([0-9]+)"/', $replace[0][$n], $match) == 1)
$dst_w = $match[1];
else
$dst_w = -1;
if (preg_match('/height="([0-9]+)"/', $replace[0][$n], $match) == 1)
$dst_h = $match[1];
else
$dst_h = -1;
$panPattern = '/class="[^"]*crop[^"]*"/';
if (preg_match($panPattern, $replace[0][$n], $pan) == 0)
$crop = false;
else
$crop = true;
if(( strpos(strtolower($replace[0][$n]),"assets/images") !== false
|| strpos(strtolower($replace[0][$n]),"assets/galleries") !== false
) && (
strpos(strtolower($replace[0][$n]),".jpg") !== false
|| strpos(strtolower($replace[0][$n]),".gif") !== false
|| strpos(strtolower($replace[0][$n]),".png") !== false)
&& ($dst_w != -1 || $dst_h != -1)
)
{
preg_match('/src="([^"]*)"/', $replace[0][$n], $_tmp);
$_src = $modx->config['base_path'].$_tmp[1];
if (file_exists($_src)) {
$fExt = pathinfo($_src);
$fExt = strtolower($fExt["extension"]);
$cacheimgname = md5($dst_w.$dst_h.$crop."|".$_src."|".filesize($_src)."|".filectime($_src)).".pageCache.".$fExt;
// ---------------------------------------------- START
if(!file_exists($cachePath.$cacheimgname)) {
// echo "resize file ", $_src;
if ($crop) {
$_size = GetImageSize($_src); // Read the size
$file_w = $_size[0];
$file_h = $_size[1];
$ratio = ($dst_w / $dst_h);
$src_h = $file_h;
$src_w = floor($src_h * $ratio);
if ($src_w > $file_w)
{
$src_w = $file_w;
$src_h = floor($src_w / $ratio);
}
$src_x = floor(($file_w - $src_w)/2);
$src_y = floor(($file_h - $src_h)/2);
$dst_x = 0;
$dst_y = 0;
//echo "<!-- ", $_src, " crop: dst_x=", $dst_x, " dst_y=", $dst_y, " src_x=", $src_x, " src_y=", $src_y, " dst_w=", $dst_w, " dst_h=", $dst_h, " src_w=", $src_w, " src_h=", $src_h, " -->\n";
} else {
$_size = GetImageSize($_src); // Read the size
$src_w = $_size[0];
$src_h = $_size[1];
if (($dst_w != -1) && ($dst_h != -1)){ // all dimentions are set
} else if ($dst_w == -1) { // width is unknown
$ratio_y = ($dst_h / $src_h);
$dst_w = ceil($src_w * $ratio_y);
} else if ($dst_h == -1) { // height is unknown
$ratio_x = ($dst_w / $src_w);
$dst_h = ceil($src_h * $ratio_x);
} else { // all dimentions are unknown
$dst_h = $src_h;
$dst_w = $src_w;
}
$dst_x = 0;
$dst_y = 0;
$src_x = 0;
$src_y = 0;
//echo "<!-- ", $_src, " resize: dst_x=", $dst_x, " dst_y=", $dst_y, " src_x=", $src_x, " src_y=", $src_y, " dst_w=", $dst_w, " dst_h=", $dst_h, " src_w=", $src_w, " src_h=", $src_h. " -->";
}
// Increase memory limit to support larger files
ini_set('memory_limit', '24M');
// Create the new image!
if($fExt=="jpg"||$fExt=="jpeg"){
$src = ImageCreateFromJpeg($_src);
$dst = ImageCreateTrueColor($dst_w, $dst_h);
ImageCopyResampled($dst, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Save to cache
ImageJpeg($dst, $cachePath.$cacheimgname, $q);
}else if($fExt=="gif"){
$src = ImageCreateFromGif($_src);
$dst = ImageCreateTrueColor($dst_w, $dst_h);
ImageCopyResampled($dst, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Save to cache
ImageGif($dst, $cachePath.$cacheimgname);
}else if($fExt=="png"){
$src = ImageCreateFromPng($_src);
$dst = ImageCreateTrueColor($dst_w, $dst_h);
ImageCopyResampled($dst, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Save to cache
ImagePng($dst, $cachePath.$cacheimgname);
}
// Destroy the images
@ImageDestroy($src);
@ImageDestroy($dst);
} else {
// echo "get file ", $_src, " from cache";
}
// include IMG
// deleting old width and height
$replace[0][$n] = preg_replace('/width="([^"]*)"/', '' , $replace[0][$n]);
$replace[0][$n] = preg_replace('/height="([^"]*)"/', '' , $replace[0][$n]);
$_size = GetImageSize('assets/cache/'.$cacheimgname); // Read the size
$width = $_size[0];
$height = $_size[1];
$replace[0][$n] = preg_replace('/src="assets\/([^"]*)"/', ' src="'.$modx->config['site_url'].'assets/cache/'.$cacheimgname.'" width="'.$width.'" height="'.$height.'"' , $replace[0][$n]);
$o = str_replace($out[0][$n], $replace[0][$n], $o);
}
}
}
$modx->documentOutput = $o;
break;
default :
return; // stop here - this is very important.
break;
}
?>