{"id":447,"date":"2011-06-27T10:44:44","date_gmt":"2011-06-27T09:44:44","guid":{"rendered":"http:\/\/blog.stratus.org.uk\/?page_id=447"},"modified":"2011-06-27T10:53:09","modified_gmt":"2011-06-27T09:53:09","slug":"php-webcam-add-a-title-and-datetime","status":"publish","type":"page","link":"https:\/\/blog.stratus.org.uk\/?page_id=447","title":{"rendered":"PHP: Webcam &#8211; Add a Title and Date\/Time"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>This article is part of a larger discussion on using webcam images for a live website. In particular, it discusses how to take a static image and add a title and date\/time. All very useful if you can\u2019t do this on the camera itself, or need to show other information.<\/p>\n<p>For this article, here\u2019s what we assume we uploaded (this is reduced slightly, but is 640 by 480 pixels \u2013 click to see):<\/p>\n<div id=\"attachment_441\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.stratus.org.uk\/wp-content\/uploads\/2011\/02\/cloud.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-441\" class=\"size-full wp-image-441\" title=\"cloud-example-640-480\" src=\"http:\/\/blog.stratus.org.uk\/wp-content\/uploads\/2011\/02\/cloud.jpg\" alt=\"\" width=\"600\" height=\"400\" \/><\/a><p id=\"caption-attachment-441\" class=\"wp-caption-text\">Example 640 by 480 JPEG image<\/p><\/div>\n<h2>Adding the Date, Time and Title<\/h2>\n<p>This step will describe how to overlay text and graphics on your uploaded image using PHP (the PHP code is at the bottom of this article).<\/p>\n<p>We could embed the filenames and dimensions, but here we make the PHP  script parameter driven so we can change the inputs and not the script.  For this script, we assume the following parameters from the command line:<\/p>\n<ul>\n<li> The filename of the original (static) image from the webcam \u2013 somewhere on the same file system<\/li>\n<li> The filename we will create<\/li>\n<li>The text we will add on the end of the footer<\/li>\n<\/ul>\n<p>The following explains what the PHP code is doing, so read the code and text in parallel. Here we go \u2026<\/p>\n<p>Having given useful names to the input parameters, the next thing is to read the file and and work out how big it is.<\/p>\n<p>After that, create a local \u201cin memory\u201d image, then create some local  colours to use. Note these are all \u201cGD\u201d graphics functions, understood  in PHP if your system has GD (or ImageMagik).<\/p>\n<p>Right, we now have a copy of the image and some colours. Next we find  out the date and time of \u201cright now\u201d as a string, and then concatenate  the title message to that. We are assuming that we are using an image of  640 by 480 pixels (the script could be made more clever to be flexible \u2013  have a go if you like). After that we put a black band at the bottom of  the image (a box).<\/p>\n<p>Then we can actually write the text on top of the band using a  function to centre the text (this could be done manually, but by using a  function, if the text changes, it is still in the centre).<\/p>\n<p>Once the text is on the footer, write the file out as the target JPEG file. Easy.<\/p>\n<p>But before we finish, lets take a look at the function <em>imagecenteredtext<\/em>.  This function has the location the text has to go (X and Y  coordinates), the text to print, the size of the text and the colour to  use. In simple terms it works out the current font in use, works out the  number of pixels long that will be, divides it by two and then goes  leftwards that number of pixels before printing it put on the <em>img<\/em> global (which is the internal copy of the image we are using).<\/p>\n<p>And we are done. Note that this script assumes you have the GD image  library installed and working properly. To use the full script below,  copy and paste into a file, then get and image that is 640 by 480  pixels. Lastly use this at the command line:<\/p>\n<pre>php <your filename>.php <src file> <dest file> 'Hello World'<\/pre>\n<p>And you should get something like this:<\/p>\n<div id=\"attachment_443\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.stratus.org.uk\/wp-content\/uploads\/2011\/02\/cloud-foot.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-443\" class=\"size-full wp-image-443\" title=\"cloud-foot\" src=\"http:\/\/blog.stratus.org.uk\/wp-content\/uploads\/2011\/02\/cloud-foot.jpg\" alt=\"Now with a footer\" width=\"600\" height=\"400\" \/><\/a><p id=\"caption-attachment-443\" class=\"wp-caption-text\">Image with a footer<\/p><\/div>\n<p>Other things of note:<\/p>\n<ul>\n<li>The  strip panel can be any colour you like \u2013 black works here because the  source is light coloured. Select a different RGB colour value if you  like.<\/li>\n<li>The strip can go elsewhere \u2013 we have used <em>0,465 <\/em>and <em>640,478<\/em> which is left hand edge and up a bit from the bottom and right hand edge and nearly at the bottom (of 640 by 480 pixels).<\/li>\n<li>The text doesn\u2019t have to be white. Again, chose a different RGB value or just use one of those listed in the code below.<\/li>\n<li>The date and time are when the image was processed is discovered on the image using the <em>filemtime <\/em>PHP function on the source image when we create the text message. Hence this is when the image was  uploaded (which is important if you need to know when the image was  created by the webcam). If it stops, the date\/time will remain the same, which means the webcam is not uploading if the time shown is a long time ago.<\/li>\n<li>We have a few print statements to help us if debugging \u2013 this doesn\u2019t affect the output.<\/li>\n<\/ul>\n<pre>\r\n<pre><?php \r\n\r\n  \/\/ remove first argument\r\n  array_shift($argv);\r\n\r\n  \/\/ create the base image\r\n  global $image_format, $baseimg, $img;\r\n\r\n  $localfilename = $argv[0];\r\n  $outputfilename = $argv[1];\r\n  $localtextmsg = $argv[2];\r\n  $image_format = 'jpeg';\r\n\r\n  \/\/ create truecolor image to deal avoid any pallete problems\r\n  print \"\\nReading $localfilename\";\r\n  $baseimg = imagecreatefromjpeg($localfilename);\r\n  $basex = imagesx($baseimg);\r\n  $basey = imagesy($baseimg);\r\n  print \"\\nSize is $basex by $basey\\n\";\r\n\r\n  $img = imagecreatetruecolor($basex,$basey);\r\n  imagecopy($img,$baseimg,0,0,0,0,$basex,$basey);\r\n\r\n\/\/ define the colors\r\n\/\/ this has to be done after the image is created\r\n  $color1 = imagecolorallocate($img,255,0,0);      \/\/ RED\r\n  $color2 = imagecolorallocate($img,0,128,0);      \/\/ GREEN\r\n  $color3 = imagecolorallocate($img,0,0,255);      \/\/ BLUE\r\n  $color4 = imagecolorallocate($img,0,0,0);        \/\/ BLACK\r\n  $color5 = imagecolorallocate($img,255,255,255);  \/\/ WHITE  \r\n\r\n  $today = date (\"D M j G:i:s Y\", filemtime($localfilename));\r\n\r\n  ImageFilledRectangle( $img,0,465,640,478, $color4);\r\n  $sFooterText = \"$localtextmsg - $today\";\r\n  print \"\\nOutput text is: $sFooterText\\n\";\r\n  imagecenteredtext(315, 475, $sFooterText, 2, 10, $color5, 0);\r\n  imagejpeg($img, \"$outputfilename\");\r\n\r\n\/\/ get rid of the image since not needed in memory any more.\r\n\/\/ don't need any memory leaks taking down the server\r\nimagedestroy($img);\r\nimagedestroy($baseimg);\r\n\/************ Function Definitions *************\/\r\nfunction imagecenteredtext(\r\n             $x, $y, $text, $size, $ttfsize, $color, $angle) {\r\n\r\n  global $img;\r\n\r\n  $x0 = $x;\r\n  $y0 = $y;\r\n  $x -= (imagefontwidth($size) * strlen($text)) \/ 2;\r\n  $y -= (imagefontheight($size)) \/ 2;\r\n  #$x1 = $x-\r\n  \/\/ do a box first - x is something we cal to the left,\r\n  \/\/ y is unchanged\r\n  imagestring($img, $size, $x, $y - 3, $text, $color);\r\n} \/\/ end function imagecenteredtext\r\n\/\/******************************************************\r\n\r\n?><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Overview This article is part of a larger discussion on using webcam images for a live website. In particular, it discusses how to take a static image and add a title and date\/time. All very useful if you can\u2019t do &hellip; <a href=\"https:\/\/blog.stratus.org.uk\/?page_id=447\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":84,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-447","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=447"}],"version-history":[{"count":9,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/447\/revisions"}],"predecessor-version":[{"id":459,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/447\/revisions\/459"}],"up":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/84"}],"wp:attachment":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}