Here the code for you.
The following code uses free text to add watermark. You can use your own font by specifying the font file. Only thing you need to make sure is, when deploy the code, make sure that you have the font file in the server.
AddWatermark method accepts 3 parameters, image file path, watermark text to be printed and font file path respectively. You can call this method whenever the image file is uploaded to your server. Since it uses the watermark text which have passed to this method is printed on the image, you can configure your watermark text. To change the size of the watermark text based on the image size, watermark text will be taking the 5% of the original image area. Then the font size will be calculated to fill that 5% area and then Watermark text will be converted to image. After that, it will find the potion at which the watermark should be printed. Finally, watermark image will be merged into the original image.
public static function AddWatermark($originalFilePath, $TextToPrint, $fontFilePath) {
//Creating image from file path
$original = self::CreateImage($originalFilePath);
//To find the width and height of the image
list($originalWidth, $originalHeight) = getimagesize($originalFilePath);
//5% of the whole image area will be used for watermark text area
$acceptableWatermarkArea = ($originalWidth * $originalHeight) * 0.05;
$shouldDisplayTextVertical = false;
if ($originalWidth < $originalHeight) {
$shouldDisplayTextVertical = true;
}
//Minimum font size
$fontSize = 12; $fontAreaTotal = 0;
//Find the required font size to fill the acceptable watermark area. This will change based on the original image size
while ($acceptableWatermarkArea > $fontAreaTotal) {
$fontArea = self::CalcFontArea($fontFilePath, $fontSize, $TextToPrint, $shouldDisplayTextVertical);
$fontAreaTotal = $fontArea['area'];
if ($fontSize > 90) {//Max Font Size is 90px for Watermark
break;
}
$fontSize = $fontSize + 2;
}
$fontAreaDetail = self::CalcFontArea($fontFilePath, $fontSize - 2, $TextToPrint, $shouldDisplayTextVertical);
$im = imagecreatetruecolor($fontAreaDetail['width'], $fontAreaDetail['height']);
imageSaveAlpha($im, true);
imagealphablending($im, false);
$bg = imagecolorallocatealpha($im, 175, 175, 175, 127);
imagefilledrectangle($im, 0, 0, $fontAreaDetail['width'], $fontAreaDetail['height'], $bg);
$text = imagecolorallocatealpha($im, 255, 255, 255, 75);
$textXPos = 0;
$textYPos = 0;
$angle = 0;
$watermarkXPos = 0;
$watermarkYPos = 0;
//To find the position at which, the watermark should be printed
if ($shouldDisplayTextVertical) {
$textXPos = $fontAreaDetail['width'];
$textYPos = $fontAreaDetail['height'] - 10;
$angle = 90;
$watermarkXPos = 10;
$watermarkYPos = ($originalHeight / 2) - ($fontAreaDetail['height'] / 2);
} else {
$textXPos = 10;
$textYPos = $fontAreaDetail['height'] - 10;
$angle = 0;
$watermarkXPos = ($originalWidth / 2) - ($fontAreaDetail['width'] / 2);
$watermarkYPos = $originalHeight - $fontAreaDetail['height'];
}
//Converts watermark text to image
imagettftext($im, $fontAreaDetail['fontsize'] - 2, $angle, $textXPos, $textYPos, $text, $fontFilePath, $TextToPrint);
imagecopy($original, $im, $watermarkXPos, $watermarkYPos, 0, 0, $fontAreaDetail['width'], $fontAreaDetail['height']);
$destinationFolderPath = dirname($originalFilePath);
//To save image
self::SaveImageAsJPEG($original, $destinationFolderPath, basename($originalFilePath));
imagedestroy($im);
}
private static function CalcFontArea($fontFilePath, $fontSize, $text, $isVertical = false) {
$angle = 0;
if ($isVertical) {
$angle = 90;
}
$fontBox = imageftbbox($fontSize, $angle, $fontFilePath, $text);
$fontArea = array();
$fontArea['width'] = abs($fontBox[4] - $fontBox[0]) + 10;
$fontArea['height'] = abs($fontBox[5] - $fontBox[1]) + 10;
$fontArea['area'] = $fontArea['width'] * $fontArea['height'];
$fontArea['fontsize'] = $fontSize;
return $fontArea;
}
private static function CreateImage($imageFilePath) {
$filePathInfo = pathinfo($imageFilePath);
$extn = strtolower($filePathInfo['extension']);
$image = null;
switch ($extn) {
case 'jpg':
return imagecreatefromjpeg($imageFilePath);
}
}
private static function SaveImageAsJPEG($imageResource, $destinationFolderPath, $fileNameWithoutExtn) {
$destinationFilePath = $destinationFolderPath . DIRECTORY_SEPARATOR . $fileNameWithoutExtn . ".jpg";
if (!file_exists($destinationFolderPath)) {
mkdir($destinationFolderPath, 0777, true);
}
header('Content-Type: image/jpeg');
imagejpeg($imageResource, $destinationFilePath, 90);
imagedestroy($imageResource);
}