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.