{"id":42008,"date":"2018-11-23T03:01:47","date_gmt":"2018-11-22T17:01:47","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=42008"},"modified":"2018-11-25T07:08:04","modified_gmt":"2018-11-24T21:08:04","slug":"php-gd-image-colour-count-browse-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-gd-image-colour-count-browse-tutorial\/","title":{"rendered":"PHP GD Image Colour Count Browse Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of.php\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"PHP GD Image Colour Count Browse Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of_browse.jpg\" title=\"PHP GD Image Colour Count Browse Tutorial\"   \/><\/a><p class=\"wp-caption-text\">PHP GD Image Colour Count Browse Tutorial<\/p><\/div>\n<p>The recent <a title='PHP GD Image Colour Count Primer Tutorial' href='#phpgdiccpt'>PHP GD Image Colour Count Primer Tutorial<\/a> took as its input &#8230;<\/p>\n<ul>\n<li>a single image URL (at a time) &#8230; but today we extend that to &#8230;<\/li>\n<li>local image files browsed for<\/li>\n<\/ul>\n<p> &#8230; and use <a target=_blank title='XMLHttpRequest object information' href='https:\/\/www.w3schools.com\/js\/js_ajax_http.asp'>Ajax<\/a> <a target=_blank title='FormData object information' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/FormData'>FormData<\/a> techniques, to do this.  The difference with the last lot of Ajax FormData &#8220;Media Capture Email&#8221; web application blog postings is that today, we have an interest in the <b>Ajax response<\/b> (via the definition of Javascript, in the PHP) &#8230;<\/p>\n<p><code><br \/>\n$jscr=\"&lt;scri\" . \"pt type='text\/javascript'&gt;<br \/>\n<br \/>\nvar form=null;<br \/>\nvar xhr=null;<br \/>\nvar cnt=0;<br \/>\nvar aclicked=new Array();<br \/>\n<br \/>\nvar ourmimetype='';<br \/>\nvar types = ['video\/mp4','image\/svg','audio\/wav','audio\/x-wav','audio\/x-pn-realaudio','audio\/x-mpegurl','audio\/x-aiff','audio\/mpeg','audio\/mid',<br \/>\n      'audio\/basic','audio\/ogg','video\/x-sgi-movie','video\/x-msvideo','video\/quicktime','audio\/mp3','video\/mp4','video\/mpeg',<br \/>\n      'video\/x-la-asf','video\/ogg','video\/webm','audio\/mp4', 'image\/jpeg', 'image\/jpeg', 'image\/png', 'image\/gif', 'image\/bmp', 'image\/tif',<br \/>\n      'text\/html', 'text\/html', 'text\/html', 'text\/javascript', 'text\/css', 'text\/plain', 'text\/xml', 'text\/csv',<br \/>\n      'application\/vnd.ms-word', 'application\/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application\/x-php', 'application\/pdf',<br \/>\n      'application\/vnd.openxmlformats-officedocument.presentationml.presentation', 'application\/vnd.ms-powerpoint',<br \/>\n      'application\/vnd.ms-excel', 'application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];<br \/>\nvar exts = ['.mp4','.svg','.wav','.wav','.ram','.m3u','.aiff','.mp3','.rmi',<br \/>\n      '.snd','.ogg','.movie','.avi','.mov','.mp3','.m4v','.mpeg',<br \/>\n      '.lsx','.ogv','.webm','.m4a', '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tif',<br \/>\n      '.htm', '.html', '.htmls', '.js', '.css', '.txt', '.xml', '.csv',<br \/>\n      '.doc', '.docx', '.php', '.pdf',<br \/>\n      '.pptx', '.ppt',<br \/>\n      '.xls', '.xlsx'];<br \/>\n<br \/>\nfunction onl() {<br \/>\nvar input = document.getElementById('cimage'); \/\/ .querySelector('input[type=file]'); \/\/ .getElementById('cimage'); \/\/ see Example 4<br \/>\n<br \/>\ninput.onchange = function () {<br \/>\n  var file = input.files; \/\/[0];<br \/>\n  for (var ii=0; ii&lt;file.length; ii++) {<br \/>\n    if (ii == 0) { upload(file); }<br \/>\n  }<br \/>\n};<br \/>\n}<br \/>\n<br \/>\nfunction upload(file) {<br \/>\n  var suffix='';<br \/>\n  aclicked=new Array();<br \/>\n  form = new FormData();<br \/>\n  xhr = new XMLHttpRequest();<br \/>\n  <b>xhr.onreadystatechange = function() {<br \/>\n    if (this.readyState == 4 && this.status == 200) {<br \/>\n      if (1 == 2) {<br \/>\n      var wo=window.open('','_blank','top=50,left=50,width=600,height=600');<br \/>\n      wo.document.write('&lt;h' + '1' + this.responseText.split('&lt;\/bo' + 'dy&gt;')[0].split('&lt;h' + '1')[1]);<br \/>\n      } else {<br \/>\n      document.body.innerHTML='&lt;h' + '1' + this.responseText.split('&lt;\/bo' + 'dy&gt;')[0].split('&lt;h' + '1')[1];<br \/>\n      }<br \/>\n    }<br \/>\n  };<\/b><br \/>\n<br \/>\n  for (var i=0; i&lt;file.length; i++) {<br \/>\n  aclicked.push(false);<br \/>\n ourmimetype='';<br \/>\n for (var ji=0; ji&lt;exts.length; ji++) {<br \/>\n      if (('.' + file[i].name.split('.')[eval(-1 + file[i].name.split('.').length)]).toLowerCase() == exts[ji].toLowerCase()) {<br \/>\n        ourmimetype=types[ji];<br \/>\n      }<br \/>\n }<br \/>\n    form.append('filename' + suffix, file[i].name);<br \/>\n    form.append('types' + suffix, ourmimetype);<br \/>\n    form.append('images' + suffix, file[i]);<br \/>\n    suffix='' + eval(1 + i);<br \/>\n  }<br \/>\n<br \/>\n  xhr.open('post', 'image_colours_of.php', true);<br \/>\n  xhr.setRequestHeader('Cache-Control', 'no-cache');<br \/>\n  xhr.send(form);<br \/>\n}<br \/>\n&lt;\/scr\" . \"ipt&gt;<br \/>\n\";<br \/>\n<\/code><\/p>\n<p> &#8230; the reason it is of interest this time being that it affects webpage display, whereas that last project it was like a dead end functionality of sending an email.<\/p>\n<p>You can see <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of.php-GETME\" title=\"image_colours_of.php\">today&#8217;s changed<\/a> <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of.php-GETME\" title=\"image_colours_of.php\">image_colours_of.php<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of.php\" title=\"Click picture\">live run<\/a> for your perusal.<\/p>\n<p>You can also see this play out at WordPress 4.1.1&#8217;s <a target=_blank  href='\/\/www.rjmprogramming.com.au\/ITblog\/php-gd-image-colour-count-browse-tutorial\/'>PHP GD Image Colour Count Browse Tutorial<\/a>.<\/p>\n<hr>\n<p id='phpgdiccpt'>Previous relevant <a target=_blank title='PHP GD Image Colour Count Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/php-gd-image-colour-count-primer-tutorial\/'>PHP GD Image Colour Count Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of.php\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"PHP GD Image Colour Count Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of.jpg\" title=\"PHP GD Image Colour Count Primer Tutorial\"   \/><\/a><p class=\"wp-caption-text\">PHP GD Image Colour Count Primer Tutorial<\/p><\/div>\n<p>We&#8217;ve long been interested in the idea of cataloguing the colour makeup of an image, and knew, even with *.bmp images (which are one to one pixel in their makeup) you still have to formulate a method to quickly scan the image pixel by pixel to get anywhere with an overall &#8220;Image Colour Report&#8221; concept.  Even though there is, incredibly enough a &#8230;<\/p>\n<ul>\n<li>PHP GD (image library) <a target=_blank href='http:\/\/php.net\/manual\/en\/function.imagecolorstotal.php'>imagecolorstotal<\/a> method to do just that, it doesn&#8217;t work for every type of image, so we Googled away and got to &#8230;<\/li>\n<li><a target=_blank title='Great coding here, thanks' href='\/\/stackoverflow.com\/questions\/25400805\/how-to-know-howmany-colors-are-exist-in-the-image'>this very useful link<\/a> got us onto some great and fast code to do the job, once the user enters an image URL, to get the ball rolling<\/li>\n<\/ul>\n<p>Also coming into play with this work was the discovery of linear gradient CSS means by which a document.body (or other element, for that matter) background image can be given some transparency independent of the foreground (presumably more opaque) transparency, as we outlined with <a target=_blank href='https:\/\/www.rjmprogramming.com.au\/ITblog\/rainbow-games-background-image-tutorial\/' title='Rainbow Games Background Image Tutorial'>Rainbow Games Background Image Tutorial<\/a>.  Via this technique we can keep all of &#8230;<\/p>\n<ol>\n<li>user defined imagery<\/li>\n<li>webpage header elements<\/li>\n<li>number of colours reporting<\/li>\n<li>prompt for the user for a follow up entry<\/li>\n<\/ol>\n<p> &#8230; all above the fold rather than having to scroll down past an &lt;img&gt;&lt;\/img&gt; element to get to that last idea above.  We also squeeze in an opacity in between the other opacities to a &lt;div&gt;&lt;\/div&gt; to give a nuanced different lighting to the div background encasing the heading and report and prompt parts above.  The woooooorrrrllllddd&#8217;s your oyster!<\/p>\n<p>Just be ready with an image URL of interest and you can try <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of.php_GETME\" title=\"image_colours_of.php\">image_colours_of.php<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/image_colours_of.php\" title=\"Click picture\">live run<\/a> link to see what we mean here.  We hope it is of interest for you.<\/p>\n<p><p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d41910' onclick='var dv=document.getElementById(\"d41910\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/gd\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d41910' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d42008' onclick='var dv=document.getElementById(\"d42008\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/ajax\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d42008' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The recent PHP GD Image Colour Count Primer Tutorial took as its input &#8230; a single image URL (at a time) &#8230; but today we extend that to &#8230; local image files browsed for &#8230; and use Ajax FormData techniques, &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/php-gd-image-colour-count-browse-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[1993,224,418,1550,853,932,997,1321,1343],"class_list":["post-42008","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-browse","tag-colour","tag-file","tag-gd","tag-ob_start","tag-php","tag-programming","tag-tutorials","tag-upload"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42008"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=42008"}],"version-history":[{"count":3,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42008\/revisions"}],"predecessor-version":[{"id":42016,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/42008\/revisions\/42016"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=42008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=42008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=42008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}