The code should be displayed using a picture loop and by clicking the picture opens in a modal window in which you can click on any place in the picture to recognize its RGB and HEX. The problem is that the picture is not transferred to the modal window. For some reason, the variable from the loop is not passed to the variable in javascript.

<?php $dir = 'img/'; // Папка с изобраТСниями $cols = 3; // ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ столбцов Π² Π±ΡƒΠ΄ΡƒΡ‰Π΅ΠΉ Ρ‚Π°Π±Π»ΠΈΡ†Π΅ с ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°ΠΌΠΈ $files = scandir($dir); // Π‘Π΅Ρ€Ρ‘ΠΌ всё содСрТимоС Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ //echo "<div class='row'>"; // $k = 0; // for ($i = 0; $i < count($files); $i++) { // ΠŸΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Π΅ΠΌ всС Ρ„Π°ΠΉΠ»Ρ‹ if (($files[$i] != ".") && ($files[$i] != "..")) { // Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ ΠΈ Ρ€ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ пропускаСм if ($k % $cols == 0) ; // ДобавляСм Π½ΠΎΠ²ΡƒΡŽ строку echo "<div class='col-xs-6 col-sm-4 col-md-3 col-lg-3'>"; echo "<div class='thumbnail'>"; // $path = $dir.$files[$i]; // ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΏΡƒΡ‚ΡŒ ΠΊ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ΅ echo "<a data-toggle='modal' data-target='#myModal$i'>"; // Π”Π΅Π»Π°Π΅ΠΌ ссылку Π½Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ echo "<img src='$path' alt='' width='300' height='240' />"; // Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€Π΅Π²ΡŒΡŽ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ echo "</a>"; // Π—Π°ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ ссылку echo "</div>"; echo "</div>"; echo " <div class='modal fade' id='myModal$i' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button> <h4 class='modal-title' id='myModalLabel'>НазваниС ΠΌΠΎΠ΄Π°Π»ΠΈ</h4> </div> <div class='modal-body'> <canvas width='600' height='440' id='canvas_picker'></canvas> <div id='hex'>HEX: <input type='text'></input></div> <div id='rgb'>RGB: <input type='text'></input></div> </div> <div class='modal-footer'> <button type='button' class='btn btn-default' data-dismiss='modal'>Π—Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ</button> <button type='button' class='btn btn-primary'>Π‘ΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ измСнСния</button> </div> </div> </div> </div>"; echo "<script type='text/javascript'> var canvas = document.getElementById('canvas_picker').getContext('2d'); // create an image object and get it's source var img = new Image(); img.src = '".$path."'; // image credit: http://pixabay.com/en/fishing-boat-denmark-beach-sea-49523/ // copy the image to the canvas $(img).load(function(){ canvas.drawImage(img,0,0); }); // http://www.javascripter.net/faq/rgbtohex.htm function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)} function toHex(n) { n = parseInt(n,10); if (isNaN(n)) return '00'; n = Math.max(0,Math.min(n,255)); return '0123456789ABCDEF'.charAt((nn%16)/16) + '0123456789ABCDEF'.charAt(n%16); } $('#canvas_picker').click(function(event){ // getting user coordinates var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; // getting image data and RGB values var img_data = canvas.getImageData(x, y, 1, 1).data; var R = img_data[0]; var G = img_data[1]; var B = img_data[2]; var rgb = R + ',' + G + ',' + B; // convert RGB to HEX var hex = rgbToHex(R,G,B); // making the color the value of the input $('#rgb input').val(rgb); $('#hex input').val('#' + hex); }); </script>"; // echo "</div>"; // Π—Π°ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ столбСц /* Π—Π°ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ строку, Ссли Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠ΅ количСство Π±Ρ‹Π»ΠΎ Π²Ρ‹Π²Π΅Π΄Π΅Π½ΠΎ, Π»ΠΈΠ±ΠΎ данная итСрация послСдняя */ if ((($k + 1) % $cols == 0) || (($i + 1) == count($files))); $k++; // Π£Π²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ Π²ΡΠΏΠΎΠΌΠΎΠ³Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ счётчик } } ?> 

The console says that the error in

 $(img).load(function(){ canvas.drawImage(img,0,0); }); 

Only xs how to remove it. Help me please.

In html everything seems to be fine and the php variable passes the value, but javascript does not want to work with it. Here is the page code:

 <div class="container"> <div class="panel panel-default"> <div class="panel-body"> <div class="row"> <div class='col-xs-6 col-sm-4 col-md-3 col-lg-3'><div class='thumbnail'><a data-toggle='modal' data-target='#myModal2'><img src='img/006.jpg' alt='' width='300' height='240' /></a></div></div> <div class='modal fade' id='myModal2' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button> <h4 class='modal-title' id='myModalLabel'>НазваниС ΠΌΠΎΠ΄Π°Π»ΠΈ</h4> </div> <div class='modal-body'> <canvas width='600' height='440' id='canvas_picker'></canvas> <div id='hex'>HEX: <input type='text'></input></div> <div id='rgb'>RGB: <input type='text'></input></div> </div> <div class='modal-footer'> <button type='button' class='btn btn-default' data-dismiss='modal'>Π—Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ</button> <button type='button' class='btn btn-primary'>Π‘ΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ измСнСния</button> </div> </div> </div> </div><script type='text/javascript'> var canvas = document.getElementById('canvas_picker').getContext('2d'); // create an image object and get it's source var img = new Image(); img.src = 'img/006.jpg'; // image credit: http://pixabay.com/en/fishing-boat-denmark-beach-sea-49523/ // copy the image to the canvas $(img).load(function(){ canvas.drawImage(img,0,0); }); // http://www.javascripter.net/faq/rgbtohex.htm function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)} function toHex(n) { n = parseInt(n,10); if (isNaN(n)) return '00'; n = Math.max(0,Math.min(n,255)); return '0123456789ABCDEF'.charAt((nn%16)/16) + '0123456789ABCDEF'.charAt(n%16); } $('#canvas_picker').click(function(event){ // getting user coordinates var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; // getting image data and RGB values var img_data = canvas.getImageData(x, y, 1, 1).data; var R = img_data[0]; var G = img_data[1]; var B = img_data[2]; var rgb = R + ',' + G + ',' + B; // convert RGB to HEX var hex = rgbToHex(R,G,B); // making the color the value of the input $('#rgb input').val(rgb); $('#hex input').val('#' + hex); }); </script><div class='col-xs-6 col-sm-4 col-md-3 col-lg-3'><div class='thumbnail'><a data-toggle='modal' data-target='#myModal3'><img src='img/007.jpg' alt='' width='300' height='240' /></a></div></div> <div class='modal fade' id='myModal3' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button> <h4 class='modal-title' id='myModalLabel'>НазваниС ΠΌΠΎΠ΄Π°Π»ΠΈ</h4> </div> <div class='modal-body'> <canvas width='600' height='440' id='canvas_picker'></canvas> <div id='hex'>HEX: <input type='text'></input></div> <div id='rgb'>RGB: <input type='text'></input></div> </div> <div class='modal-footer'> <button type='button' class='btn btn-default' data-dismiss='modal'>Π—Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ</button> <button type='button' class='btn btn-primary'>Π‘ΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ измСнСния</button> </div> </div> </div> </div><script type='text/javascript'> var canvas = document.getElementById('canvas_picker').getContext('2d'); // create an image object and get it's source var img = new Image(); img.src = 'img/007.jpg'; // image credit: http://pixabay.com/en/fishing-boat-denmark-beach-sea-49523/ // copy the image to the canvas $(img).load(function(){ canvas.drawImage(img,0,0); }); // http://www.javascripter.net/faq/rgbtohex.htm function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)} function toHex(n) { n = parseInt(n,10); if (isNaN(n)) return '00'; n = Math.max(0,Math.min(n,255)); return '0123456789ABCDEF'.charAt((nn%16)/16) + '0123456789ABCDEF'.charAt(n%16); } $('#canvas_picker').click(function(event){ // getting user coordinates var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; // getting image data and RGB values var img_data = canvas.getImageData(x, y, 1, 1).data; var R = img_data[0]; var G = img_data[1]; var B = img_data[2]; var rgb = R + ',' + G + ',' + B; // convert RGB to HEX var hex = rgbToHex(R,G,B); // making the color the value of the input $('#rgb input').val(rgb); $('#hex input').val('#' + hex); }); </script><div class='col-xs-6 col-sm-4 col-md-3 col-lg-3'><div class='thumbnail'><a data-toggle='modal' data-target='#myModal4'><img src='img/008.jpg' alt='' width='300' height='240' /></a></div></div> <div class='modal fade' id='myModal4' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button> <h4 class='modal-title' id='myModalLabel'>НазваниС ΠΌΠΎΠ΄Π°Π»ΠΈ</h4> </div> <div class='modal-body'> <canvas width='600' height='440' id='canvas_picker'></canvas> <div id='hex'>HEX: <input type='text'></input></div> <div id='rgb'>RGB: <input type='text'></input></div> </div> <div class='modal-footer'> <button type='button' class='btn btn-default' data-dismiss='modal'>Π—Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ</button> <button type='button' class='btn btn-primary'>Π‘ΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ измСнСния</button> </div> </div> </div> </div><script type='text/javascript'> var canvas = document.getElementById('canvas_picker').getContext('2d'); // create an image object and get it's source var img = new Image(); img.src = 'img/008.jpg'; // image credit: http://pixabay.com/en/fishing-boat-denmark-beach-sea-49523/ // copy the image to the canvas $(img).load(function(){ canvas.drawImage(img,0,0); }); // http://www.javascripter.net/faq/rgbtohex.htm function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)} function toHex(n) { n = parseInt(n,10); if (isNaN(n)) return '00'; n = Math.max(0,Math.min(n,255)); return '0123456789ABCDEF'.charAt((nn%16)/16) + '0123456789ABCDEF'.charAt(n%16); } $('#canvas_picker').click(function(event){ // getting user coordinates var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; // getting image data and RGB values var img_data = canvas.getImageData(x, y, 1, 1).data; var R = img_data[0]; var G = img_data[1]; var B = img_data[2]; var rgb = R + ',' + G + ',' + B; // convert RGB to HEX var hex = rgbToHex(R,G,B); // making the color the value of the input $('#rgb input').val(rgb); $('#hex input').val('#' + hex); }); 
  • Right? Are you sure? 100% sure? And what kind of error gives then? A separate variable echoes? I copied your code - it works. It is terribly scary, but it works - Alexey Shimansky
  • @ Alexey Shimansky, gives out, but javascript either does not accept it or something else. Here is a screen pixs.ru/showimage/qqwwpng_4065835_19895466.png - x_decimo_x
  • The paths to the pictures are relative, perhaps because of this they are not loaded. It is necessary to look at the html code of the page and make sure that the links are working. - Marsel Arduanov

1 answer 1

Try replacing '$path' with " . $path . "