Add a text watermark to an image on its upload

This code displays a form that the user can use to add text to an image. When the form is submitted the image is uploaded to a tempory location where it is then watermarked and saved into the directory set in the code.

Note: If allowing user uploads the data submitted MUST be checked to stop people uploading code that can compramise the server. This code has some basic "user input validation".

<?php

function clean($name$max) {
// Remove everything except letters numbers . and @ in the variable
preg_replace("/[^A-Za-z0-9.\-_@]/","",$name);

// Do not allow excessively long entries - length set in function call
// Could use a word wrap here - add a \n after a certain amount of characters
$name substr($name0$max);
return 
$name;
}

// If the form has been submitted do this
if(isset($_FILES['filename']['tmp_name'])) {
 
// Temporary upload image name 
$original_image $_FILES['filename']['tmp_name'];

// Name to save the image as - in this case the same as the original
// The same folder as the code
$new_image $_FILES['filename']['name'];  

// Cleanup the text.
$text_submitted clean $_POST['text_submitted'], 18 );

// Build the imagemagick command - using rgba so the opacity of the text can be set
$cmd "$original_image -pointsize 50 -font arial.ttf -fill rgba\(0,0,0,0.4\) ".
" -gravity center -annotate +0+0 \"$text_submitted\" ";

// Coment out to display the command - good for debugging
//echo $cmd;

// Setup the array for the error reporting
$array = array();

// The array is to hold any errors you may get
// Coment out the if function after debugging
exec("convert $cmd  $new_image 2>&1"$array);
if ( !empty(
$array) ){
echo 
"<br />There were some errors during the Imagemagick conversion:<br />
<pre><br />"
.print_r($array)."<br>"
echo 
"</pre>";
}
            }                      
else { 
?>

<form method="post" action="<?php echo  $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<label>File to upload</label> 
<input type="file" name="filename"  /> 
<label>Text to add to the photo</label>
<input type="text" name="text_submitted"  /><br />
<input type="Submit" name="Submit" value="Submit" />
</form>
 <?php ?>