{"id":2211,"date":"2015-09-03T08:19:52","date_gmt":"2015-09-03T08:19:52","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2211"},"modified":"2015-09-03T08:23:41","modified_gmt":"2015-09-03T08:23:41","slug":"watermark-an-image-in-php","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/03\/watermark-an-image-in-php\/","title":{"rendered":"Watermark an image in PHP"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p>How to add a line of text as a watermark to any image (jpg\/png\/gif) using the built-in GD functions. The code given here should run on any version of PHP &gt;= 4.0.<\/p>\n<p>The code creates the watermarked image on the server, based on the path provided. Note that animated gifs do not remain animated once they have been modified in code. Be sure to give correct folder locations when calling the function and make sure the target folder has write permissions otherwise the images wont be written. You will also need to put the font file COURIER.TTF on the server where it is accessible to the code. You can use any TTF font file, just change the name of the font file in the code.<\/p>\n<h4>Example output of jpeg image<\/h4>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2212\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test.jpg\" alt=\"test\" width=\"586\" height=\"440\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test.jpg 586w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test-300x225.jpg 300w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2213\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output.jpg\" alt=\"output\" width=\"586\" height=\"440\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output.jpg 586w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output-300x225.jpg 300w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>Example output of png image<\/h4>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2214\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test1.jpg\" alt=\"test\" width=\"500\" height=\"340\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test1.jpg 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test1-300x204.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2215\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output1.jpg\" alt=\"output\" width=\"500\" height=\"340\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output1.jpg 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output1-300x204.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>Example output of gif image<\/h4>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2216\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/test.gif\" alt=\"test\" width=\"436\" height=\"334\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2217\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/output.gif\" alt=\"output\" width=\"436\" height=\"334\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The code follows:<br \/>\n<pre class=\"brush: php\">&lt;?php\r\nerror_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATION);\r\n\r\n\/\/\/\r\n\/\/ Create a watermark on an image\r\n\/\/ Amit Sengupta, Sep 2015\r\n\/\/\/\/\r\n\r\n\r\n\r\n\/**\r\n * Add a line of text as watermark to an image file\r\n * @param string $sourceImage path to source image \r\n * @param string $destImage path to desination image which will be created\r\n * @param string text which will be added to the image\r\n * @param string font location of font file\r\n * @return None\r\n *\/\r\nfunction watermark($sourceImage, $destImage, $text, $font) {\r\n    \r\n    \/\/get image size\r\n    $arrSize = getImageSize($sourceImage);\r\n\r\n    \/\/ get type of image and then use the relevant function\r\n    \/\/ only handling gif, jpg and png here.\r\n    $imageType = exif_imagetype($sourceImage);\r\n    $pSource = null;\r\n    $pDest = null;\r\n    if ($imageType == 1) { \/\/GIF\r\n\t$pSource = imagecreatefromgif($sourceImage);\r\n\t$pDest = imagecreatefromgif($sourceImage);\r\n    }\r\n    else if ($imageType == 2) { \/\/ JPEG\r\n\t$pSource = imagecreatefromjpeg($sourceImage);\r\n\t$pDest = imagecreatefromjpeg($sourceImage);\r\n    }\r\n    else if ($imageType == 3) { \/\/ PNG\r\n\t$pSource = imagecreatefrompng($sourceImage);\r\n\t$pDest = imagecreatefrompng($sourceImage);\r\n    }\r\n    else\r\n\texit(&quot;Unsupported image type&quot;);\r\n\r\n    \/\/ copy source image to dest image object - not really reqd IMHO because the dest is same as source\r\n    $retval = imagecopyresampled($pDest, $pSource, 0,0,0,0, $arrSize[&quot;width&quot;], $arrSize[&quot;height&quot;],\r\n\t\t\t$arrSize[&quot;width&quot;], $arrSize[&quot;height&quot;]);\r\n    if (!$retval)\r\n\texit(&quot;Imagecopyresampled failed&quot;);\r\n\t\r\n    \/\/ superimpose text on image\r\n    $blackcolor = imagecolorallocate($pDest, 0,0,0,0);\r\n    $fontSize=10;\r\n    $result = imagettftext($pDest, $fontSize, 0, 10, 20, $blackcolor, $font, $text);\r\n    if (!$result)\r\n\texit(&quot;imagettftext failed&quot;);\r\n\r\n    \/\/ write destination file\r\n    if ($imageType == 1) { \/\/GIF\r\n\timagegif($pDest, $destImage);\r\n    }\r\n    else if ($imageType == 2) { \/\/ JPEG\r\n\tif (!imagejpeg($pDest, $destImage, 100))\r\n\t    exit(&quot;imagejpg failed for &quot; . $destImage);\r\n    }\r\n    else if ($imageType == 3) { \/\/ PNG\r\n\tif (!imagepng($pDest, $destImage))\r\n\t    exit(&quot;imagepng failed for &quot; . $destImage);\r\n    }\r\n    \r\n    \/\/ destroy image resources\r\n    imagedestroy($pSource);\r\n    imagedestroy($pDest);\r\n    \r\n} \r\n\r\n$source = $_SERVER[&#039;DOCUMENT_ROOT&#039;] . &quot;\/dump\/test.png&quot;;\r\n$dest = $_SERVER[&quot;DOCUMENT_ROOT&quot;] . &quot;\/dump\/output.png&quot;;\r\n$font = $_SERVER[&quot;DOCUMENT_ROOT&quot;] . &quot;\/COURIER.TTF&quot;;\r\n\r\nif (!is_writable($_SERVER[&quot;DOCUMENT_ROOT&quot;] . &quot;\/dump&quot;))\r\n    exit (&quot;destination is not writable&quot;);\r\n\r\nwatermark($source, $dest, &quot;Test Watermark&quot;, $font);\r\n\r\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to add a line of text as a watermark to any image (jpg\/png\/gif) using the built-in GD functions. The code given here should run <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/03\/watermark-an-image-in-php\/\" title=\"Watermark an image in PHP\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2219,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,10],"tags":[],"class_list":["post-2211","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apachephp","category-programming"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2211"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2211\/revisions"}],"predecessor-version":[{"id":2221,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2211\/revisions\/2221"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2219"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}