Thank you for this great Plugin!!
I made a small modification. The Plugin now both resizes and crops an image to get a thumbnail which exactly matches the specified dimensions.
Code:
//<?php
//***********************************
//TVimageResizer plugin v1.6 for MODx 1.0
//Used EasyPhpThumbnail class by JF Nutbroek
//***********************************
//Andchir http://wdevblog.net.ru
//***********************************
//Modified 17. August 2009
//When using dimensions for Thumbnails
//the plugin now automatically crops the image
//to exactly fit the dimensions.
//***********************************
// Copy folder to assets/plugins/
// Description: Resizing images of Template Variables. Rounding the corners, adding watermarks.
// Configuration: &th_width=Width;int;150 &th_height=Height;int;110 &prefix=Prefix;string;t_ &rcorner=Corners percentage of clipping;int;0 &watermark=Watermark image path (png);string;0 ©righttext=Copyright text;string;0 &quality=Quality;int;85
// System Events: OnDocFormRender, OnBeforeDocFormSave
//***********************************
if(!isset($prefix)) $prefix="t_";
if(!isset($th_width)) $th_width=100;
if(!isset($th_height)) $th_height=100;
if(!isset($rcorner)) $rcorner=false;
if(!isset($watermark)) $watermark=false;
if(!isset($watermarkPos)) $watermarkPos="90% 90%";
if(!isset($copyrighttext)) $copyrighttext=false;
if(!isset($quality)) $quality=85;
if(!isset($square)) $square=true;
if(!isset($bg_color)) $bg_color="#ffffff";
global $content;
global $tmplvars;
$doc_id = $content['id'];
$template = $content['template'];
$fullpath = $modx->config['base_path'];
define(IMAGE_CLASS,"GD");
$e = &$modx->Event;
$output = "";
$tb_tmplvars = $modx->getFullTableName('site_tmplvars');
$tb_tmplvar_values = $modx->getFullTableName('site_tmplvar_contentvalues');
require_once('../assets/plugins/tvimageresizer/easyphpthumbnail.class.php');
if ($e->name == 'OnDocFormRender') {
$result = $modx->db->select('id,name', $tb_tmplvars, "type='image'");
$tvs = '';
while ($row = $modx->db->getRow($result))
$tvs .= ",'" . $row['id'] . "'";
$tvs = substr($tvs, 1); // removes starting comma
$output = <<< EOT
<!-- TVimageResizer -->
<script type="text/javascript">
var imageNames = [$tvs];
window.onDomReady(function() {
for (var i = 0; i < imageNames.length; i++) {
var elem = $('tv' + imageNames[i])
if (elem) {
var field1 = new Element('input',{type:'checkbox', name:'rsz[]', id:'rsz'+(i+1), value:imageNames[i]});
var label = new Element('label',{for:'rsz'+(i+1)});
var field2 = new Element('input',{type:'text', size:'5', name:'rszwidth[]', value:$th_width, 'styles': {'width':'20px'}});
var field3 = new Element('input',{type:'text', size:'5', name:'rszheight[]', value:$th_height, 'styles': {'width':'20px'}});
elem.getParent().adopt(field1).adopt(label.appendText('resize')).adopt(field2).appendText('x','after').adopt(field3);
}
}
});
</script>
<!-- /TVimageResizer -->
EOT;
$e->output($output);
}
if ($e->name == 'OnBeforeDocFormSave') {
if(isset($_POST['rsz'])){
echo $override_tv;
foreach ($_POST['rsz'] as $k=>$v) {
if(!empty($_POST['tv'.$v])){
$s_value = $_POST['tv'.$v];
$location = str_replace(basename($s_value),'',$s_value);
$new_value = $location.$prefix.basename($s_value);
if($s_value!=$new_value && !empty($s_value)){
if(file_exists("../$new_value")) unlink("../$new_value");
$th_width = $_POST['rszwidth'][$k] ? $_POST['rszwidth'][$k] : $th_width;
$th_height = $_POST['rszheight'][$k] ? $_POST['rszheight'][$k] : $th_height;
//***
//Start modification
$th_size = ($th_width<=$th_height) ? $th_height : $th_width;
$thumb = new easyphpthumbnail;
$thumb -> Thumbsize = $th_size;
$thumb -> Thumbheight = $th_size;
$thumb -> Thumbwidth = $th_size;
$thumb -> Backgroundcolor = $bg_color;
$thumb -> Quality = 100;
$thumb -> Thumblocation = "../".$location;
$thumb -> Thumbprefix = $prefix;
$thumb -> Createthumb("../$s_value","file");
$s_value = $new_value;
$prefix = ""; // overwrite temp-thumb
$thumb -> Thumbheight = $th_height;
$thumb -> Thumbwidth = $th_width;
// Croppring images to exactly math specified dimensions
$thumb -> Cropimage[0] = 2;
$thumb -> Cropimage[1] = 1;
$thumb -> Cropimage[2] = floor($th_width/2); // left from center
$thumb -> Cropimage[3] = floor($th_width/2); // right from center
$thumb -> Cropimage[4] = floor($th_height/2); // top from center
$thumb -> Cropimage[5] = floor($th_height/2); // bottom from center
if($th_width == $th_height) {
// settings for squared Thumbnails
$thumb -> Square = $square;
}
//End modification
//***
if($rcorner)
$thumb -> Clipcorner = array(2,$rcorner,0,1,1,1,1);
if($watermark){
$thumb -> Watermarkpng = $watermark;
$thumb -> Watermarktransparency = 60;
$thumb -> Watermarkposition = $watermarkPos;
}
if($copyrighttext)
$thumb -> Copyrighttext = $copyrighttext;
$thumb -> Quality = $quality;
$thumb -> Thumblocation = "../".$location;
$thumb -> Thumbprefix = "$prefix";
$thumb -> Createthumb("../$s_value","file");
$tmplvars[$v][1] = $new_value;
}
}
}
}
}
//?>
I hope I could help someone with it.