0

I am creating an Image from PHP code and it is working fine, but when I place any javascript code above the PHP code, Image creator give error

The image Cannot be displayed because it contains errors

and doesn't create image.

But when I remove header it is not showing any error. I tried to remove Javascript code and it started working again.

Following is my code.

<script>
    var date = new Date();
    date.setTime(date.getTime() + 10000);
    var expires = "; expires=" + date.toGMTString();
    document.cookie = "ctime=" + date.getHours() + expires + "; path = /";
</script>

<?php
include "dbconfig.php";
$city = $_REQUEST['city'];
$query = "SELECT city.cityid, weather.* from city INNER JOIN weather on city.cityid=weather.cityid where weather.date>=curdate() and city.city_name='$city'";
$rs = select($query);
$row = mysqli_fetch_array($rs);
$atmosphere = strtolower($row['atmosphere']);
$minTemp = $row['min_temp'];
$maxTemp = $row['max_temp'];
$temp = $row['temperature'];
$humidity = $row['humidity'];

header('Content-type: image/jpeg');
$font = "Gugi-Regular.ttf";
$fontsize = 30;
if ($atmosphere == 'clear') {
    $atmosphere = 'sunny';
}
$weather = "$atmosphere day.";
$im = imagecreatefromjpeg("$atmosphere.jpg");
$orange = imagecolorallocate($im, 9, 78, 188);
imagettftext($im, 30, 0, 130, 50, $orange, $font, $temp);
imagettftext($im, 16, 0, 130, 70, $orange, $font, $weather);
imagettftext($im, 12, 0, 130, 100, $orange, $font, 'min');
imagettftext($im, 12, 0, 180, 100, $orange, $font, 'max');
imagettftext($im, 12, 0, 230, 100, $orange, $font, 'hum');
imagettftext($im, 12, 0, 130, 120, $orange, $font, $minTemp);
imagettftext($im, 12, 0, 180, 120, $orange, $font, $maxTemp);
imagettftext($im, 12, 0, 230, 120, $orange, $font, $humidity);
imagejpeg($im);
imagedestroy($im);
?>

I want to get current client system time in PHP, that's why I am putting javascript code to get time in PHP from cookie.

Help me in both.

Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
Anand agrawal
  • 492
  • 6
  • 25
  • What was the error? – hungrykoala May 11 '18 at 05:42
  • 6
    You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries. Specially since you're not escaping the user inputs at all! – M. Eriksson May 11 '18 at 05:42
  • 3
    What did you expect? `header('Content-type: image/jpeg');`. JS is not image/jpeg type! – Salim Ibrohimi May 11 '18 at 05:44
  • 3
    ...plus, all headers must be sent before _any_ output. In this case though, you're creating an image and outputs the image data directly, which makes it an odd place to add js. What do you expect the JS to actually do here? – M. Eriksson May 11 '18 at 05:47
  • I am putting JS at above on the page because of getting client system time. That which I will get in my PHP code through cookie. – Anand agrawal May 11 '18 at 05:48
  • 1
    js script and php script are irrelevant, you should separate them. – Salim Ibrohimi May 11 '18 at 05:54
  • This question is not quite a duplicate of https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php but it exposes the same programming error. Read its answers to find out how to solve yours. – axiac May 11 '18 at 08:24
  • I am not getting any Header Already Sent error, Are you guys think that the problem is of header? – Anand agrawal May 11 '18 at 12:24

1 Answers1

0

You can do this

<?php
$date = date("H");
setcookie("ctime", $date, time() + 10000, "/");
?>

Instead of this

<script>
    var date = new Date();
    date.setTime(date.getTime() + 10000);
    var expires = "; expires=" + date.toGMTString();
    document.cookie = "ctime=" + date.getHours() + expires + "; path = /";
</script>
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38