{"id":1989,"date":"2012-08-04T08:38:09","date_gmt":"2012-08-04T15:38:09","guid":{"rendered":"http:\/\/www.marcelpatek.com\/blog\/?p=1989"},"modified":"2016-07-10T06:14:00","modified_gmt":"2016-07-10T13:14:00","slug":"random-images-web","status":"publish","type":"post","link":"https:\/\/www.marcelpatek.com\/blog\/2012\/08\/04\/random-images-web\/","title":{"rendered":"Adding random static images to your web-site"},"content":{"rendered":"<p>There are times when one wishes to add just a &#8220;simple&#8221; static photo to improve feel and presentation of our\/client&#8217;s site or photo blog. The idea usually goes like this:<\/p>\n<ul>\n<li>I have a series of images that I want to display on my website.<\/li>\n<li>I do not need a slideshow.<\/li>\n<li>I just want to display one image (on the page load) and I want to grab it at random.<\/li>\n<li>My site is either based on WordPress theme or it is just a plain xhtml site.<\/li>\n<\/ul>\n<p>To accomplish this task, three web components are needed: html, css, and some sort of JavaScript. For the latter, I picked jQuery library which is well suited for this task. In addition, other standard web technologies will be used, such as JSON and Ajax.<br \/>\n[space]<\/p>\n<h3><strong>Prerequisites:<\/strong><\/h3>\n<p>For the following part, we will need to be comfortable with a simple editing of <span style=\"font-style:italic;\">html<\/span> code, preparing <em>JSON<\/em> list of our photos, including <em>jQuery\/Ajax<\/em> script, and with a little bit of <em>css<\/em> styling.<\/p>\n<p>[space]<br \/>\n[space]<\/p>\n<p><!--more--><br \/>\n[space]<br \/>\n[separator]<\/p>\n<p>[space]<\/p>\n<h2><strong>Putting all things together:<\/strong><\/h2>\n<p>First, we start off with creating a structured list that will contain file name and title (description) of our photos. In this example, we have five photos with two data sets for each (title and url). Feel free to add, replace, or remove photos from this file, but always make sure that its structure remains the same. &#8220;Title&#8221; is essentially a header or photo description that will be displayed with it and &#8220;url&#8221; field takes the file name of your image.<br \/>\nI often check integrity of JSON file at: <a href=\"http:\/\/jsonlint.com\/\">Jsonlint<\/a>.<br \/>\n[space]  <\/p>\n<p style=\"padding-left: 20px;\"><span style=\"color: #ff00ff;\">Example of JSON file &#8211; images.json<\/span><\/p>\n<p>[space]<\/p>\n<p>[sourcecode language=&#8221;js&#8221;]<br \/>\n\/\/ Building jason list<br \/>\n{&quot;images&quot;:[<br \/>\n{<br \/>\n\t&quot;title&quot;: &quot;Sunset at the Horshoe Bend, AZ&quot;,<br \/>\n\t&quot;url&quot;: &quot;1.jpg&quot;<br \/>\n\t },<br \/>\n\t {<br \/>\n\t&quot;title&quot;: &quot;Kauai Highlands&quot;,<br \/>\n\t&quot;url&quot;: &quot;2.jpg&quot;<br \/>\n\t },<br \/>\n\t {<br \/>\n\t&quot;title&quot;: &quot;Storm is coming&quot;,<br \/>\n\t&quot;url&quot;: &quot;3.jpg&quot;<br \/>\n\t },<br \/>\n\t {<br \/>\n\t&quot;title&quot;: &quot;Venice &#8211; Grand Canal view from the Rialto Bridge&quot;,<br \/>\n\t&quot;url&quot;: &quot;4.jpg&quot;<br \/>\n\t },<br \/>\n\t {<br \/>\n\t&quot;title&quot;: &quot;Mexican wolf&quot;,<br \/>\n\t&quot;url&quot;: &quot;5.jpg&quot;<br \/>\n\t }<br \/>\n\t ]<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\n[space]<br \/>\nNext, save the above code as images.json and create a new directory on your web server where all images and the just saved .json file will be located. I have chosen <em>images_rand<\/em> to distinguish it from common web-site images. For example, if your web site is at www.yoursite.com, new directory would go to: www.yoursite.com\/images_rand. Again, this is where both images and the above images.json file would go.<br \/>\n[space]<\/p>\n<h6>Editing html page:<\/h6>\n<p>This is a very simple step: just place new <em>div<\/em> element into your html document, somewhere between the <em>body<\/em> tags:<br \/>\n[sourcecode language=&#8221;html&#8221;]<br \/>\n&lt;body&gt;<br \/>\n&#8230;&#8230;<br \/>\n&lt;div id=&quot;singleshot&quot;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&#8230;&#8230;<br \/>\n&lt;\/body&gt;<br \/>\n[\/sourcecode]<br \/>\nI have assigned this element new id and called it &#8220;singleshot&#8221;. This is where the random image and description will be injected by the jQuery call.<br \/>\n[space]<\/p>\n<h6>Styling the image:<\/h6>\n<p>Position and style can be adjusted in <em>css<\/em> stylesheet which gets loaded with your page. You can find its name by viewing the page source from any browser.<br \/>\nThe link that you are looking for is in the <em>head<\/em> sections. For example:<\/p>\n<p style=\"padding-left: 20px;\"><span style=\"color: #ff00ff;\">Part of the html <em>head<\/em> section<\/span><\/p>\n<p>[html 1=&#8221;4&#8243; language=&#8221;highlight:&#8221;]<br \/>\n&lt;head&gt;<br \/>\n&#8230;..<br \/>\n&lt;link rel=&quot;stylesheet&quot; href=&quot;http:\/www.yoursite\/css\/style.css&quot; type=&quot;text\/css&quot; media=&quot;all&quot; \/&gt;<br \/>\n&#8230;..<br \/>\n&lt;script type=&#8217;text\/javascript&#8217; src=&#8217;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.6.2\/jquery.min.js?ver=1.6.2&#8242;&gt;&lt;\/script&gt;<br \/>\n&#8230;..<br \/>\n&lt;\/head&gt;<br \/>\n[\/html]<br \/>\n[space]<\/p>\n<p style=\"padding-left: 20px;\"><span style=\"color: #ff00ff;\">Example of css file<\/span><\/p>\n<p>[sourcecode language=&#8221;css&#8221;]<br \/>\n#singleshot {<br \/>\n \t\tpadding:7px;<br \/>\n \t\tbackground-color: rgb(80,80,80);<br \/>\n \t\t-moz-border-radius: 5px;<br \/>\n \t\t-webkit-border-radius: 5px;<br \/>\n \t      border-radius: 5px;<br \/>\n       \tmargin-bottom: 60px;<br \/>\n       \twidth:665px;<br \/>\n       \theight: 499px;<br \/>\n            }<br \/>\n#singleshot p {<br \/>\n\t       background: rgba(90,90,90,0.3);<br \/>\n\t       color: rgb(20,20,20);<br \/>\n\t       position: relative;<br \/>\n\t       top: -500px;<br \/>\n\t       margin: 0 0 0 0 ;<br \/>\n\t       padding: 5px 0 5px 0;<br \/>\n\t       text-align: center;<br \/>\n\t       font-family: Arial, Helvetica;<br \/>\n\t       font-size: 2.2em;<br \/>\n\t       font-weight: bold;<br \/>\n             }<br \/>\n[\/sourcecode]<br \/>\nI have assigned this element new id called &#8220;singleshot&#8221;. This is where the image and description will be injected by jQuery call.<br \/>\n[space]<\/p>\n<h6>jQuery code:<\/h6>\n<p>First, we need to include jQuery library into our page. To accomplish this, include its source into the <em>head<\/em> section as shown above (part of the html section, line 5). The latest (Aug 2012) source is always at: <a href=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.2\/jquery.min.js\">Google Ajax API CDN<\/a>.<br \/>\nCheck for the most recent version at: <a href=\"http:\/\/docs.jquery.com\/Downloading_jQuery\">jQuery main web site <\/a>.<br \/>\nEnter the following script into the <em>html<\/em> body, just before the &#8220;singleshot&#8221; <em>div<\/em> element at line 3 above (Editing html page).<br \/>\n[space]<br \/>\n[js]<br \/>\n&lt;script type=&quot;text\/javascript&quot;&gt;<br \/>\n  jQuery.getJSON(&#8216;http:\/\/www.yoursite.com\/..\/images_rand\/images.json&#8217;, function(data) { \/\/Ajax call<br \/>\n         var item = data.images[Math.floor(Math.random()*data.images.length)]; \/\/select image at random<br \/>\n         \/\/build the complete &lt;img&gt; tag<br \/>\n         jQuery(&#8216;&lt;img src=&quot;http:\/\/yoursite.com\/..\/images_rand\/&#8217; + item.url + &#8216; &quot; width=&quot;665px&quot; height=&quot;499px&quot;&gt;&#8217;).appendTo(&#8216;#singleshot&#8217;);<br \/>\n         jQuery(&#8216;&lt;p&gt;&#8217; + item.title + &#8216;&lt;\/p&gt;&#8217;).appendTo(&#8216;#singleshot&#8217;); \/\/attach &quot;title&quot; to separate &lt;p&gt; tag<br \/>\n});<br \/>\n&lt;\/script&gt;<br \/>\n[\/js]<br \/>\n[space]<br \/>\n[warning]Since I was adapting this script for a WordPress template, I use jQuery instead of $ sign in the front of jQuery statements (else the script won&#8217;t work in WordPress).[\/warning]<br \/>\nThe script starts with Ajax call to <em>images.json<\/em> file located on your server. Data is read into an array and random entry is assigned to a variable <em>item<\/em>. In the next step, the complete <em>img<\/em> tag is built by adding the image file name (url) and image dimensions, which are constant for the web page. Once done, the <em>img<\/em> tag is appended to <em>#singleshot<\/em> id in the empty <em>div<\/em> element. The last step of our script grabs the <em>title<\/em> and places it into a separate <em>p<\/em> tag.<br \/>\nThat&#8217;s all. Change the path to your images.json file and image dimensions to fit your page.<br \/>\n[space]<br \/>\nFinal result is at the lower part of my <a href=\"http:\/\/www.marcelpatek.com\/blog\">Photography web site<\/a>.<br \/>\n[space]<\/p>\n<div class=\"my_blog\" style=\"border: 1px solid gray\">\n<table>\n<tr>\n<td>\n<div id=\"attachment_1753\" style=\"width: 705px\" class=\"wp-caption alignmiddle\"><a href=\"http:\/\/www.marcelpatek.com\/blog\/wp-content\/uploads\/2012\/08\/blog_ajax.png\" rel=\"shadowbox[sbpost-1989];player=img;\" title=\"Example of working script\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1753\" src=\"http:\/\/www.marcelpatek.com\/blog\/wp-content\/uploads\/2012\/08\/blog_ajax.png\" alt=\"\" title=\"Example of working script\" width=\"695\" height=\"430\" class=\"size-full wp-image-1753\"\/><\/a><p id=\"caption-attachment-1753\" class=\"wp-caption-text\">Example of working script at photo.marcelpatek.com<\/p><\/div>\n<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>[space]<br \/>\n[tip]While the code above works just fine, color of the description text sometimes does not match the image lightness. If we were just able to change the text color depending on image tones &#8230; Read below.<br \/>\n[\/tip]<br \/>\n[space]<\/p>\n<h2><strong>Further improving our script:<\/strong><\/h2>\n<p>In order to match the image density with color of the description header, we have to modify two parts. Namely, the <em>json<\/em> file and the <em>jQuery\/Ajax<\/em> script.<br \/>\nBelow  is the new <em>json<\/em> list with additional tag, called <em>&#8220;scene&#8221;<\/em>. This is where we define the image category (light, dark, normal). Each category will then be used to change the <em>css<\/em> style of our description text.<br \/>\n[space]<br \/>\n[sourcecode language=&#8221;js&#8221;]<br \/>\n\/\/ Modified jason list (images1.json)<br \/>\n{&quot;images&quot;:[<br \/>\n{<br \/>\n\t&quot;title&quot;: &quot;Sunset at the Horshoe Bend, AZ&quot;,<br \/>\n\t&quot;url&quot;: &quot;1.jpg&quot;,<br \/>\n\t&quot;scene&quot;: &quot;light&quot;<br \/>\n\t },<br \/>\n\t {<br \/>\n\t&quot;title&quot;: &quot;Kauai Highlands&quot;,<br \/>\n\t&quot;url&quot;: &quot;2.jpg&quot;,<br \/>\n\t&quot;scene&quot;: &quot;dark&quot;<br \/>\n\t },<br \/>\n\t {<br \/>\n\t&quot;title&quot;: &quot;Gathering before the storm&quot;,<br \/>\n\t&quot;url&quot;: &quot;3.jpg&quot;,<br \/>\n\t&quot;scene&quot;: &quot;light&quot;<br \/>\n\t },<br \/>\n\t {<br \/>\n\t&quot;title&quot;: &quot;Venice &#8211; Grand Canal view from the Rialto Bridge&quot;,<br \/>\n\t&quot;url&quot;: &quot;4.jpg&quot;,<br \/>\n\t&quot;scene&quot;: &quot;normal&quot;<br \/>\n\t },<br \/>\n\t {<br \/>\n\t&quot;title&quot;: &quot;Mexican wolf in Sonora Desert Museum in Tucson, AZ&quot;,<br \/>\n\t&quot;url&quot;: &quot;5.jpg&quot;,<br \/>\n\t&quot;scene&quot;: &quot;normal&quot;<br \/>\n\t }<br \/>\n\t ]<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\n[space]<br \/>\nSave the list as <em>images1.json<\/em> and upload it into the same directory as the <em>images.json<\/em> file above.<br \/>\n[space]<br \/>\nNow, the jQuery part. We define several additional variables to describe the color and the text background. Then, depending on the image category, empty variable <em>scene<\/em> is assigned the corresponding values and it is passed as a style into the <em>p<\/em> element at line 15.<br \/>\n[space]<br \/>\n[js]<br \/>\n&lt;script type=&quot;text\/javascript&quot;&gt;<br \/>\n  jQuery.getJSON(&#8216;http:\/\/www.yoursite.com\/..\/images_rand\/images1.json&#8217;, function(data) {<br \/>\n       var item = data.images[Math.floor(Math.random()*data.images.length)];<br \/>\n       \/\/change rgb values below for the best color-match<br \/>\n       var light = &quot;color: rgb(20,20,100); background: rgba(190,190,190,0.25)&quot;;<br \/>\n       var dark =  &quot;color: rgb(255,206,0); background: rgba(90,90,90,0.25)&quot;;<br \/>\n       var normal = &quot;color: rgb(230,184,0); background: rgba(90,90,90,0.25)&quot;;<br \/>\n       var scene = &quot;&quot;; \/\/keep the original style<\/p>\n<p>\t\tif (item.scene == &#8216;dark&#8217;) {scene = dark}<br \/>\n\t\telse if (item.scene == &quot;light&quot;) {scene = light}<br \/>\n\t      else {scene = normal}<\/p>\n<p>       jQuery(&#8216;&lt;img src=&quot;http:\/\/www.yoursite.com\/..\/images_rand\/&#8217; + item.url + &#8216; &quot; width=&quot;665px&quot; height=&quot;499px&quot; title=&quot;Random image&quot; alt=&quot;&quot; \/&gt;&#8217;).appendTo(&#8216;#singleshot&#8217;);<br \/>\n       jQuery(&#8216;&lt;p style=&quot;&#8217; + scene + &#8216;&quot;&gt;&#8217; + item.title + &#8216;&lt;\/p&gt;&#8217;).appendTo(&#8216;#singleshot&#8217;);<br \/>\n});<br \/>\n&lt;\/script&gt;<br \/>\n[\/js]<br \/>\n[space]<br \/>\nThat&#8217;s it. Change the jQuery script on your html page and tweak the color settings between lines 5-7.<br \/>\n[space]<br \/>\n[separator]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times when one wishes to add just a &#8220;simple&#8221; static photo to improve feel and presentation of our\/client&#8217;s site or photo blog. The idea usually goes like this: I have a series of images that I want to &hellip; <a href=\"https:\/\/www.marcelpatek.com\/blog\/2012\/08\/04\/random-images-web\/\">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":[13,8],"tags":[24,23,25],"class_list":["post-1989","post","type-post","status-publish","format-standard","hentry","category-code-software","category-software","tag-jquery-ajax","tag-json","tag-web-technologies"],"_links":{"self":[{"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/posts\/1989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/comments?post=1989"}],"version-history":[{"count":99,"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/posts\/1989\/revisions"}],"predecessor-version":[{"id":2199,"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/posts\/1989\/revisions\/2199"}],"wp:attachment":[{"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/media?parent=1989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/categories?post=1989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marcelpatek.com\/blog\/wp-json\/wp\/v2\/tags?post=1989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}