How to create text watermark using php?This is describes how to create simple text watermark using php.Tested in many browsers.Uploaded file type is same to final image type.Only you have to call to the function and get output.View demo and download source code.
In the another post is describes how to create simple image watermark using php.But this watermark text is very simply an smiler to it.First you get a font that you want to apply to this(Ex: TIMES.TTF for Time New Roman font).
Everything I’ll describes here simply by commenting.
This is the php function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php $fontPath = "MTCORSVA.TTF"; $fontSize = 20; // in pixels $watermarkText = "Watermark text here"; function watermarkProcess($oldImageName, $newImageName){ global $fontPath, $fontSize, $watermarkText; list($oldImageWidth,$oldImageHeight) = getimagesize($oldImageName); $width = 300; $height = 300; // CREATING TEMPORARY IMAGE $imageTmp = imagecreatetruecolor($width, $height) or die('Cannot Initialize new GD image stream'); // GET TEMPORARY IMAGE INFO(WIDTH & HEIGHT) $info = getimagesize($oldImageName); // Get image path from jpg/jpeg types if($info[2] == IMAGETYPE_JPEG){ $imageTmpPath = imagecreatefromjpeg($oldImageName) or die('JPEG/JPEG Image type is open failed'); } // Get image path from gif type if($info[2] == IMAGETYPE_PNG){ $imageTmpPath = imagecreatefrompng($oldImageName) or die('PNG Image type is open failed'); } // Get image path from png type if($info[2] == IMAGETYPE_GIF){ $imageTmpPath = imagecreatefromgif($oldImageName) or die('GIF Image type is open failed'); } // Copy and resize part of an image with resampling imagecopyresampled($imageTmp, $imageTmpPath, 0, 0, 0, 0, $width, $height, $oldImageWidth, $oldImageHeight); // Decides text color by RGB value $color = imagecolorallocate($imageTmp, 59, 186, 10); // Create temp image text by inserting text to positions imagettftext($imageTmp, $fontSize, 0, 70, 270, $color, $fontPath, $watermarkText); // Create image for jpg/jpeg types if($info[2] == IMAGETYPE_JPEG){ imagejpeg($imageTmp, $newImageName, 100); } // Create image for gif type if($info[2] == IMAGETYPE_PNG){ imagepng($imageTmp, $newImageName, 0); } // Create image for png type if($info[2] == IMAGETYPE_GIF){ imagegif($imageTmp, $newImageName, 100); } // delete temporary image imagedestroy($imageTmp); // delete uploaded original image unlink($oldImageName); return true; } ?> |
[stextbox id=”info”]
- 0 – text rotation
- 140 – The x-cordinate.From left-to-right distance.simply like left padding of the image
- 270 – The y-cordinate. From top-to-bottom distance.simply like top padding of the image
[/stextbox]
This is the html form.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<form name="imageUpload" id="imageUpload" method="post" enctype="multipart/form-data" > <fieldset> <legend>Upload Image</legend> Image :<input type="file" name="imagefile" id="imagefile"/><br /> <input type="submit" name="uploadedImage" id="uploadedImage" value="Submit" /> </fieldset> <?php if(!empty($textWithWatermark)) { echo '<br/><center><img src="'.$textWithWatermark.'" /></center><br/>'; echo 'Image Name- '.$textWithWatermarkName; } else echo '<h3>'.$msg.'</h3>'; ?> </form> |
Now see, how to call to this function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php $textWithWatermark = ""; $imageWithWatermarkName = ""; if(isset($_POST['uploadedImage']) and $_POST['uploadedImage'] == "Submit"){ $path = "uploads/"; // get valid image formats $valid_formats = array('jpg','jpeg','png','gif'); $uploadedImageName = $_FILES['imagefile']['name']; if(strlen($uploadedImageName)) { list($txt, $extension) = explode(".", $uploadedImageName); $fileSize = round( ($_FILES['imagefile']['size']/1024)/1024, 2 ); // size by mb if(in_array($extension,$valid_formats) && $_FILES['imagefile']['size'] <= 5242880) // limit to 5mb size { $upload_status = move_uploaded_file($_FILES['imagefile']['tmp_name'], $path.$_FILES['imagefile']['name']); if($upload_status) { $imgName = time().'.'.$extension; $newNamePath = $path.$imgName; // Call to the function if(watermarkProcess($path.$_FILES['imagefile']['name'], $newNamePath)) $textWithWatermark = $newNamePath; $textWithWatermarkName = $imgName; } } else { $msg = "File size Max 5mb or Invalid file format supports jpg, jpeg, png, gif.<br/>You uploaded file size is ".$fileSize.'MB'; } } } ?> |
Now put this all together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
<?php $fontPath = "MTCORSVA.TTF"; $fontSize = 20; // in pixels $watermarkText = "Watermark text here"; function watermarkProcess($oldImageName, $newImageName){ global $fontPath, $fontSize, $watermarkText; list($oldImageWidth,$oldImageHeight) = getimagesize($oldImageName); $width = 300; $height = 300; // CREATING TEMPORARY IMAGE $imageTmp = imagecreatetruecolor($width, $height) or die('Cannot Initialize new GD image stream'); // GET TEMPORARY IMAGE INFO(WIDTH & HEIGHT) $info = getimagesize($oldImageName); // Get image path from jpg/jpeg types if($info[2] == IMAGETYPE_JPEG){ $imageTmpPath = imagecreatefromjpeg($oldImageName) or die('JPEG/JPEG Image type is open failed'); } // Get image path from gif type if($info[2] == IMAGETYPE_PNG){ $imageTmpPath = imagecreatefrompng($oldImageName) or die('PNG Image type is open failed'); } // Get image path from png type if($info[2] == IMAGETYPE_GIF){ $imageTmpPath = imagecreatefromgif($oldImageName) or die('GIF Image type is open failed'); } // Copy and resize part of an image with resampling imagecopyresampled($imageTmp, $imageTmpPath, 0, 0, 0, 0, $width, $height, $oldImageWidth, $oldImageHeight); // Decides text color by RGB value $color = imagecolorallocate($imageTmp, 59, 186, 10); // Create temp image text by inserting text to positions imagettftext($imageTmp, $fontSize, 0, 70, 270, $color, $fontPath, $watermarkText); // Create image for jpg/jpeg types if($info[2] == IMAGETYPE_JPEG){ imagejpeg($imageTmp, $newImageName, 100); } // Create image for gif type if($info[2] == IMAGETYPE_PNG){ imagepng($imageTmp, $newImageName, 0); } // Create image for png type if($info[2] == IMAGETYPE_GIF){ imagegif($imageTmp, $newImageName, 100); } // delete temporary image imagedestroy($imageTmp); // delete uploaded original image unlink($oldImageName); return true; } $textWithWatermark = ""; $imageWithWatermarkName = ""; if(isset($_POST['uploadedImage']) and $_POST['uploadedImage'] == "Submit"){ $path = "uploads/"; // get valid image formats $valid_formats = array('jpg','jpeg','png','gif'); $uploadedImageName = $_FILES['imagefile']['name']; if(strlen($uploadedImageName)) { list($txt, $extension) = explode(".", $uploadedImageName); $fileSize = round( ($_FILES['imagefile']['size']/1024)/1024, 2 ); // size by mb if(in_array($extension,$valid_formats) && $_FILES['imagefile']['size'] <= 5242880) // limit to 5mb size { $upload_status = move_uploaded_file($_FILES['imagefile']['tmp_name'], $path.$_FILES['imagefile']['name']); if($upload_status) { $imgName = time().'.'.$extension; $newNamePath = $path.$imgName; // Call to the function if(watermarkProcess($path.$_FILES['imagefile']['name'], $newNamePath)) $textWithWatermark = $newNamePath; $textWithWatermarkName = $imgName; } } else { $msg = "File size Max 5mb or Invalid file format supports jpg, jpeg, png, gif.<br/>You uploaded file size is ".$fileSize.'MB'; } } } ?> <form name="imageUpload" id="imageUpload" method="post" enctype="multipart/form-data" > <fieldset> <legend>Upload Image</legend> Image :<input type="file" name="imagefile" id="imagefile"/><br /> <input type="submit" name="uploadedImage" id="uploadedImage" value="Submit" /> </fieldset> <?php if(!empty($textWithWatermark)) { echo '<br/><center><img src="'.$textWithWatermark.'" /></center><br/>'; echo 'Image Name- '.$textWithWatermarkName; } else echo '<h3>'.$msg.'</h3>'; ?> </form> |
That’s only.
View Demo