It was quite simple actually, which is what I like most about MODx, embedding classes takes minutes.
This is the code I am using it resizes the image to 150px, and also duplicates it to give it one of those "I’m sitting on a shiny surface"
affairs.
It even allows me to use the errors in MODx too.
<?php
function onBeforeMailSentFunction(&$fields){
//this is where the class is
include('assets/snippets/upload/class.upload.php');
//name of my upload field
$handle = new Upload($_FILES['recipe_image']);
if ($handle->uploaded) {
// yes, the file is on the server
// below are some example settings which can be used if the uploaded file is an image.
$handle->image_resize = true;
$handle->image_ratio_y = true;
$handle->image_x = 150;
// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the wanted location
// It could be something like $handle->Process('/home/www/my_uploads/');
//where to save the files to
$handle->Process('assets/images/recipes/');
// we check if everything went OK
if ($handle->processed) {
// everything was fine !
echo '<fieldset>';
echo ' <legend>File uploaded and resized with success</legend>';
echo ' <img src="assets/images/recipes/' . $handle->file_dst_name . '" />';
$info = getimagesize($handle->file_dst_pathname);
echo ' <p>' . $info['mime'] . ' - ' . $info[0] . ' x ' . $info[1] .' - ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
echo ' link to the file just uploaded: <a href="assets/images/recipes/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a><br/>';
echo '</fieldset>';
} else {
// one error occured
echo '<fieldset>';
echo ' <legend>file not uploaded to the wanted location</legend>';
echo ' Error: ' . $handle->error . '';
echo '</fieldset>';
return false;
}
// we now process the image a second time, with some other settings
$handle->image_resize = true;
$handle->image_ratio_y = true;
$handle->image_x = 150;
$handle->image_reflection_height = '25%';
$handle->image_contrast = 10;
$handle->Process('assets/images/recipes/');
// we check if everything went OK
if ($handle->processed) {
// everything was fine !
echo '<p>Want more magic?</p>';
echo '<fieldset>';
echo ' <legend>increasing contrast and adding a reflection to make it look more tasty</legend>';
echo ' <img src="assets/images/recipes/' . $handle->file_dst_name . '" />';
$info = getimagesize($handle->file_dst_pathname);
echo ' <p>' . $info['mime'] . ' - ' . $info[0] . ' x ' . $info[1] .' - ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
echo ' link to the file just uploaded: <a href="assets/images/recipes/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a><br/>';
echo '</fieldset>';
$fields['recipe_image'] = "assets/images/recipes/".$handle->file_dst_name;
return true;
} else {
// one error occured
echo '<fieldset>';
echo ' <legend>file not uploaded to the required location</legend>';
echo ' Error: ' . $handle->error . '';
echo '</fieldset>';
return false;
}
// we delete the temporary files
$handle-> Clean();
} else {
// if we're here, the upload file failed for some reasons
// i.e. the server didn't receive the file
echo '<fieldset>';
echo ' <legend>file not uploaded on the server</legend>';
echo ' Error: ' . $handle->error . '';
echo '</fieldset>';
return false;
}
}
?>
The docs at the page are very good actually to make use of some of the other cool features. It may be overkill, but I like it.
You can see it in action at
http://greenmountaingringo.us/contact-us.html
The admin also gets the image and its location mailed to them thanks to the awesome eForm $fields array.
I will probably do away with one of those calls, once I know what exactly the client wants.
Hth,
Taff