- Messages
- 5,255
- Reaction score
- 192
- Points
- 63
This tutorial shows you how you can add a watermark image over images that are uploaded though a form.
The following is a html code as simple as it can be with an image type input and a Submit button:
This is the function that applies the watermark over the uploaded picture and uploads the picture on the server.
Having all these functions, here is how you verify if the form was submitted and call the copy_marked() function to upload the file and place the watermark:
The following is a html code as simple as it can be with an image type input and a Submit button:
Code:
<form name="image" method="post" enctype="multipart/form-data" action="">
<input type="file" name="image">
<input name="Submit" type="submit" value="Upload ">
</form>
Code:
function copy_marked($img_name,$newname,$type)
{
// check image type. The image type is sent as parameter 3 ($type). Note that for every image type a different function is used to create an image in memory from the uploaded file.
if(!strcmp("image/jpg",$type) || !strcmp("image/jpeg",$type) || !strcmp("image/pjpeg",$type))
$src_img=imagecreatefromjpeg($img_name);
if(!strcmp("image/png",$type))
$src_img=imagecreatefrompng($img_name);
if(!strcmp("image/gif",$type))
$src_img=imagecreatefromgif($img_name);
// get image size, we'll need it later
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
// create destination image
$dst_img=ImageCreateTrueColor($old_x,$old_y);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$old_x,$old_y,$old_x,$old_y);
// on this demo, the watermark image will be named watermark.gif and will be located in images folder. If you intend to use other file, you will have to change this
$watermark_file='images/watermark.gif';
// you can setup the transparency used for watermark image.
$transparency=50;
// just in case you don't know what extension the watermark file has. This function is listed lower
$wext=getFileExtension('images/watermark.gif');
// create an image from watermark file
if(!strcmp("jpg",$wext) || !strcmp("jpeg",$wext)) $watermark=imagecreatefromjpeg($watermark_file);
if(!strcmp("png",$wext)) $watermark=imagecreatefrompng($watermark_file);
if(!strcmp("gif",$wext)) $watermark=imagecreatefromgif($watermark_file);
// get watermark width
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// place watermark image on the right left of the main image
$dest_x = $old_x - $watermark_width - 5;
$dest_y = $old_y - $watermark_height - 5;
// uncomment these lines and comment the ones above if you want to place the watermark in the very center of the image
// $dest_x = (($thumb_w - $watermark_width)/2);
// $dest_y = (($thumb_h - $watermark_height)/2);
// merge the two images
imagecopymerge($dst_img, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $transparency);
// copy the new created image to the destination file, in the images folder
if(!strcmp("image/png",$type)) imagepng($dst_img,$newname);
else if(!strcmp("image/gif",$type)) imagegif($dst_img,$newname);
else imagejpeg($dst_img,$newname);
// delete the images from memory
imagedestroy($dst_img);
imagedestroy($src_img);
}
Code:
Function getFileExtension(), needed to get the file extension:
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
Code:
if($_SERVER['REQUEST_METHOD']=='POST')
{
if ($_FILES['image']['name'])
{
$image=$_FILES['image']['name'];
$newname="http://forums.x10hosting.com/images/".$image;
copy_marked($_FILES['image']['tmp_name'],$newname,$_FILES['image']['type']);
}
}
Last edited: