Leandro
Leandro

Reputation: 85

Generate image in a folder from Highchart in PHP

what i'm trying to do is to execute a function every day, generating highcharts graphs and saving as image in a folder in the server.

What I can't figured how to do is to generate a highchart from php and save as image.

I find this code

$url = 'http://export.highcharts.com/';
$data = array('filename' => 'chart' , 'type' => 'image/jpeg' , 'svg' => '<svg xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" xmlns="http://www.w3.org/2000/svg" width="600" height="400"><desc>Created with Highcharts 3.0.4</desc><defs><linearGradient x1="0" y1="0" x2="1" y2="1" id="highcharts-13"><stop offset="0" stop-color="rgb(255, 255, 255)" stop-opacity="1"></stop><stop offset="1" stop-color="rgb(240, 240, 255)" stop-opacity="1"></stop></linearGradient><clipPath id="highcharts-14"><rect fill="none" x="1" y="1" width="550" height="304"></rect></clipPath></defs><rect rx="0" ry="0" fill="url(#highcharts-13)" x="1" y="1" width="598" height="398" stroke="#4572A7" stroke-width="2"></rect><rect rx="0" ry="0" fill="none" x="38" y="40" width="552" height="306" fill-opacity="0.9"  stroke="black" stroke-opacity="0.049999999999999996" stroke-width="5" transform="translate(1, 1)"></rect><rect rx="0" ry="0" fill="none" x="38" y="40" width="552" height="306" fill-opacity="0.9"  stroke="black" stroke-opacity="0.09999999999999999" stroke-width="3" transform="translate(1, 1)"></rect><rect rx="0" ry="0" fill="none" x="38" y="40" width="552" height="306" fill-opacity="0.9"  stroke="black" stroke-opacity="0.15" stroke-width="1" transform="translate(1, 1)"></rect><rect rx="0" ry="0" fill="rgb(255,255,255)" x="38" y="40" width="552" height="306" fill-opacity="0.9"></rect><g class="highcharts-grid" ></g><g class="highcharts-grid" ></g><rect rx="0" ry="0" fill="none" x="38.5" y="40.5" width="551" height="305" stroke="#C0C0C0" stroke-width="1" ></rect><g class="highcharts-axis" ><path fill="none" d="M 38 345.5 L 590 345.5" stroke="#000" stroke-width="1"  visibility="visible"></path></g><g class="highcharts-axis" ><text x="28" y="193" style="font-family:trebuchet ms, verdana, sans-serif;font-size:12px;color:#333;font-weight:bold;fill:#333;"  text-anchor="middle" transform="translate(0,0) rotate(270 28 193)" visibility="visible"><tspan x="28">Temperatura °C</tspan></text><path fill="none" d="M 38.5 40 L 38.5 346" stroke="#000" stroke-width="1"  visibility="visible"></path></g><g class="highcharts-series-group" ><g class="highcharts-series" visibility="visible"  transform="translate(38,40) scale(1 1)" clip-path="url(#highcharts-14)"></g><g class="highcharts-markers" visibility="visible"  transform="translate(38,40) scale(1 1)" clip-path="none"><path fill="none" d="M 0 0" class="highcharts-tracker" stroke-linejoin="round" visibility="visible" stroke-opacity="0.0001" stroke="rgb(192,192,192)" stroke-width="22"  style=""></path></g><g class="highcharts-series" visibility="visible"  transform="translate(38,40) scale(1 1)" clip-path="url(#highcharts-14)"></g><g class="highcharts-markers" visibility="visible"  transform="translate(38,40) scale(1 1)" clip-path="none"><path fill="none" d="M 0 0" class="highcharts-tracker" stroke-linejoin="round" visibility="visible" stroke-opacity="0.0001" stroke="rgb(192,192,192)" stroke-width="22"  style=""></path></g><g class="highcharts-series" visibility="visible"  transform="translate(38,40) scale(1 1)" clip-path="url(#highcharts-14)"></g><g class="highcharts-markers" visibility="visible"  transform="translate(38,40) scale(1 1)" clip-path="none"><path fill="none" d="M 0 0" class="highcharts-tracker" stroke-linejoin="round" visibility="visible" stroke-opacity="0.0001" stroke="rgb(192,192,192)" stroke-width="22"  style=""></path></g></g><text x="300" y="25" style="font-family:\'lucida grande\', \'lucida sans unicode\', verdana, arial, helvetica, sans-serif;font-size:16px;color:#000;font:bold 16px \'trebuchet ms\', verdana, sans-serif;fill:#000;width:536px;" text-anchor="middle" class="highcharts-title" ><tspan x="300">Grafica en tiempo real del equipo "Equipo 1" de la última hora</tspan></text><g class="highcharts-legend"  transform="translate(182,356)"><rect rx="5" ry="5" fill="none" x="0.5" y="0.5" width="235" height="28" stroke="#909090" stroke-width="1" visibility="visible"></rect><g ><g><g class="highcharts-legend-item"  transform="translate(8,3)"><path fill="none" d="M 0 11 L 16 11" stroke="#058DC7" stroke-width="2"></path><path fill="#058DC7" d="M 8 7 C 13.328 7 13.328 15 8 15 C 2.6719999999999997 15 2.6719999999999997 7 8 7 Z"></path><text x="21" y="15" style="font-family:\'lucida grande\', \'lucida sans unicode\', verdana, arial, helvetica, sans-serif;font-size:12px;cursor:pointer;color:black;font:9pt trebuchet ms, verdana, sans-serif;fill:black;" text-anchor="start" ><tspan x="21">Sensor 1</tspan></text></g><g class="highcharts-legend-item"  transform="translate(84,3)"><path fill="none" d="M 0 11 L 16 11" stroke="#50B432" stroke-width="2"></path><path fill="#50B432" d="M 8 7 L 12 11 8 15 4 11 Z"></path><text x="21" y="15" style="font-family:\'lucida grande\', \'lucida sans unicode\', verdana, arial, helvetica, sans-serif;font-size:12px;cursor:pointer;color:black;font:9pt trebuchet ms, verdana, sans-serif;fill:black;" text-anchor="start" ><tspan x="21">Sensor 2</tspan></text></g><g class="highcharts-legend-item"  transform="translate(160,3)"><path fill="none" d="M 0 11 L 16 11" stroke="#ED561B" stroke-width="2"></path><path fill="#ED561B" d="M 4 7 L 12 7 12 15 4 15 Z"></path><text x="21" y="15" style="font-family:\'lucida grande\', \'lucida sans unicode\', verdana, arial, helvetica, sans-serif;font-size:12px;cursor:pointer;color:black;font:9pt trebuchet ms, verdana, sans-serif;fill:black;" text-anchor="start" ><tspan x="21">Sensor 3</tspan></text></g></g></g></g><g class="highcharts-axis-labels" ></g><g class="highcharts-axis-labels" ></g><g class="highcharts-tooltip"  style="cursor:default;padding:0;white-space:nowrap;" visibility="hidden" transform="translate(0,0)"><rect rx="3" ry="3" fill="none" x="0.5" y="0.5" width="16" height="16" fill-opacity="0.85"  stroke="black" stroke-opacity="0.049999999999999996" stroke-width="5" transform="translate(1, 1)"></rect><rect rx="3" ry="3" fill="none" x="0.5" y="0.5" width="16" height="16" fill-opacity="0.85"  stroke="black" stroke-opacity="0.09999999999999999" stroke-width="3" transform="translate(1, 1)"></rect><rect rx="3" ry="3" fill="none" x="0.5" y="0.5" width="16" height="16" fill-opacity="0.85"  stroke="black" stroke-opacity="0.15" stroke-width="1" transform="translate(1, 1)"></rect><rect rx="3" ry="3" fill="rgb(255,255,255)" x="0.5" y="0.5" width="16" height="16" fill-opacity="0.85"></rect><text x="8" y="21" style="font-family:\'lucida grande\', \'lucida sans unicode\', verdana, arial, helvetica, sans-serif;font-size:12px;color:#333333;fill:#333333;" ></text></g><text x="590" y="395" style="font-family:\'lucida grande\', \'lucida sans unicode\', verdana, arial, helvetica, sans-serif;font-size:9px;cursor:pointer;color:#909090;fill:#909090;" text-anchor="end" ><tspan x="590">Highcharts.com</tspan></text></svg>');

$options = array(
      'http' => array(
      'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
      'method'  => 'POST',
      'content' => http_build_query($data),
));

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

But when i do that i receive

"string(20221) "ÿØÿàJFIFHHÿÛC $.' ",#(7),01444'9=82<.342ÿÛC
2!!22222222222222222222222222222222222222222222222222ÿÀX"ÿÄ
ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ?ï¼!á Tðµíí›q&ýïç:çÀp€Vçü ÿ gþG—ÿŠ£Àò$ißöÓÿF5tµ½Z³Si7¹:ppZ×ü ÿ gþG—ÿŠ£þÐ3ÿ#ËÿÅWKIQíj3/Ùò9¿ø@<1ÿ@Ïü/ÿIÿ†?èÿ‘åÿâ«¥®{Å^4Ñüonúœ“<×/²ÞÖÞ?2i›ÑWñqÔzÑíj3gÈþÐ3ÿ#ËÿÅQÿ†?èÿ‘åÿâ©<5ã[_ÞMfºV±¦ÜÅšbÔìÌ—8ÈäƒÉ®škSù˜{8vG5ÿ†?èÿ‘åÿâ¨ÿ„ÃôÿÈòÿñUÒ×)¨øÿM°ñ¿…c±ÔïuB4†ÎßÌKtf¾CBŒ‚N£ÚÔþfΑ7ü ÿ gþG—ÿŠ£þÐ3ÿ#ËÿÅV‡‡õ¿øH4϶ÿeêzoÎSÈÔ­ü™xï·'ƒžµ«Gµ©üÌ=œ;#šÿ„ÃôÿÈòÿñTÂáúäyøªéi(öµ?™‡³‡dsð€øcþŸù_þ*ø@|1ÿ@Ïü'ÿ]%{ZŸÌÃÙò9¿ø@|1ÿ@Ïü'ÿGü

ÿ gþG“ÿŠ­ë»¨ìl§»˜‘´®@ÉÚ£'ùV4±½ðRø§M²Ôµ7ÿWoio¾âLIå©‘œIç &ŸµŸó0öpìƒþÐ3ÿ#ÉÿÅQÿ†?èÿ‘äÿâ«~Ú´ÚÃ?•$^j+ùr®×LŒá‡b;Š–kSù˜{8vG7ÿ†?èÿ‘äÿâ¨ÿ„ÃôÿÈòñUÒQGµ©üÌ=œ;#›ÿ„ÃôÿÈòñTŸð€øcþŸùOþ*ºZJ=­OæaìáÙßü ÿ gþG“ÿŠ£þÿÐ3ÿ#ÉÿÅWIEÖ"

Upvotes: 2

Views: 1521

Answers (1)

zeflex
zeflex

Reputation: 1527

$fp = fopen('your_image.jpg', 'w');
fwrite($fp, $result);
fclose($fp);

Don't forget to set the right path where your image needs to be saved.

Upvotes: 2

Related Questions