Rubblewebs

THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.

I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.

IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.

Add an effect to an image on its upload

This code displays a form that the user can use to modify images. When the form is submitted the image is uploaded to a tempory location where it is then modified and saved into the directory set in the code.
The different modifications are set with "switch" command and other ImageMagick modifications can be added.

Note: If allowing user uploads the data submitted MUST be checked to stop people uploading code that can compramise the server.

		
// If the form has been submitted do this
if ( isset( $_POST['Submit'] ) ){

// Temporary upload image name
$original_image = $_FILES['filename']['tmp_name'];

// Get the image dimensions
$size=GetImageSize( $original_image );

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

// Maximum image width
$max_width = '200';

// Maximum image height
$max_height = '90';

switch ($modification){

	// Resize the image
	case "thumb":
	$cmd = " $original_image -thumbnail {$max_width}x{$max_height} ";
	exec("convert $cmd $new_image");
	echo "File uploaded";
	break;

	//Add a charcol effect
	case "charcol":
	exec("convert -charcoal 1 $original_image $new_image");
	echo "File uploaded";
	break;

	// Add a frame around the image
	case "frame":
	exec("convert -mattecolor black -frame 5x5+2+2 $original_image $new_image");
	echo "File uploaded";
	break;
	
	default: echo "Error!";
	break; 

}}
else { ?>

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<label>File to upload:</label>
<input type="file" name="filename"><br>
<label>Effect to add</label>
<select name="modification">
	<option value="thumb"> Thumbnail
	<option value="charcol"> Charcol effect
	<option value="frame"> Add frame
</select>
<input type="Submit" name="Submit" value="Submit" >
</form>
<?php } ?>