{"id":136,"date":"2018-03-17T21:36:19","date_gmt":"2018-03-17T21:36:19","guid":{"rendered":"https:\/\/blog.phdev.fr\/?p=136"},"modified":"2018-03-17T21:49:25","modified_gmt":"2018-03-17T21:49:25","slug":"watermark-script-in-php","status":"publish","type":"post","link":"https:\/\/blog.phdev.fr\/?p=136&lang=en","title":{"rendered":"Watermark script in php"},"content":{"rendered":"<p>How to tag your published photos on the Internet? There are many ways to do this through image processing software. I needed a simple automatic system: I put my photos in a directory (In), I run a script, the marked photos are created in an output directory (Out) with the same name. I do not have to size anything for each photo.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.phdev.fr\/wp-content\/uploads\/2018\/03\/Folder-300x214.png\" alt=\"\" width=\"300\" height=\"214\" class=\"alignnone size-medium wp-image-127\" srcset=\"https:\/\/blog.phdev.fr\/wp-content\/uploads\/2018\/03\/Folder-300x214.png 300w, https:\/\/blog.phdev.fr\/wp-content\/uploads\/2018\/03\/Folder.png 622w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/center><\/p>\n<p>In practice, we have an \u00ab\u00a0In\u00a0\u00bb directory for photos to be processed, an \u00ab\u00a0Out\u00a0\u00bb directory for the processed photos, the Logo to embed and the PHP script. In addition to PHP, you need ImageMagick installed. The incrustation is light at 5%, it is possible to modify this value to obtain a more or less marked encrustation.<\/p>\n<pre lang=\"php\">\r\n<?php\r\n$dirIn = \"In\";\r\n$dirOut = \"Out\";\r\n$logo = \"Logo.png\";\r\n$tmpFile = \"tmp.png\";\r\n\r\nif ($dh = opendir($dirIn)) {\r\n  while (($file = readdir($dh)) !== false) {\r\n    $fileName = $dirIn.\"\/\".$file;\r\n    if (!is_file($fileName)) {\r\n      continue;\r\n    }\r\n    echo \"File : $file : type : \" . filetype($fileName) . \"\\n\";\r\n    list($width, $height, $type, $attr) = getimagesize($fileName);\r\n\r\n    \/\/ Resize logo\r\n    $cmd = \"convert $logo -geometry '$width\".\"x\".\"$height' $tmpFile\";\r\n    exec($cmd);\r\n    echo \"> $cmd\\n\";\r\n\r\n    \/\/ Put the watermark\r\n    $cmd = \"composite -watermark 5% -gravity center $tmpFile $fileName $dirOut\/$file\";\r\n    exec($cmd);\r\n    echo \"> $cmd\\n\";\r\n  }\r\n  closedir($dh);\r\n}\r\n@unlink($tmpFile);\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.phdev.fr\/wp-content\/uploads\/2018\/03\/Screenshot-300x112.png\" alt=\"\" width=\"300\" height=\"112\" class=\"alignnone size-medium wp-image-130\" srcset=\"https:\/\/blog.phdev.fr\/wp-content\/uploads\/2018\/03\/Screenshot-300x112.png 300w, https:\/\/blog.phdev.fr\/wp-content\/uploads\/2018\/03\/Screenshot.png 734w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/center><br \/>\nI did not try this script on Windows, but it should not be a problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to tag your published photos on the Internet? There are many ways to do this through image processing software. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":143,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,54],"tags":[70,72,74],"_links":{"self":[{"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=\/wp\/v2\/posts\/136"}],"collection":[{"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=136"}],"version-history":[{"count":2,"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=\/wp\/v2\/posts\/136\/revisions\/138"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=\/wp\/v2\/media\/143"}],"wp:attachment":[{"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.phdev.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}