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 vallidation".
<?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
$name = substr($name, 0, $max);
return $name;
}
// If the form has been submitted do this
if ( $Submit ) {
// Temporary upload image name
$original_image = $_FILES['filename']['tmp_name'];
// Name to save the image as - in this case the same as the original
$new_image = $_FILES['filename']['name'];
$text_submitted = $_POST['text_submitted'];
$text_submitted = clean ( $text_submitted, 18 );
$text = preg_split('//', $text_submitted, -1, PREG_SPLIT_NO_EMPTY);
$total = count ( $text );
for ( $i=0; $i < $total; $i++ )
{
if ( $text[$i] == " "){$text[$i] = 88 ;}
$text[$i] = $text[$i].".gif ";
$text_array = $text_array.$text[$i];
}
exec("convert $original_image -pointsize 50 -font Arial -fill rgba\(0,0,0,0.4\) -gravity center -annotate +0+0 $text_array $new_image");
}
else { ?>
<form method="post" action="<?php echo $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 } ?>