Writing values from JSON on image and display image This is a script I made that opens img.png, downloads the JSON data from the API, writes it on image and displays it. It currently is a community ad over Arqade.

&lt;?php
header('Content-type: image/png'); //setting header to png so it will display as an image
$donationImage = imagecreatefrompng("img.png"); //loading the donation image's base from the file img.png
$white = imagecolorallocate($donationImage, 255, 255, 255);

$json = json_decode(utf8_encode(file_get_contents("http://www.mariomarathon.com/rest/partners/arqade.com")));
$total = round($json-&gt;total); //Rounding the total amount because it uses floats
$byus = $json-&gt;domainTotal;

imagettftext($donationImage,30,0,60,450,$white,'arial.ttf',"$".$total); //writing the total count to x:60 y:450
imagettftext($donationImage,30,0,370,450,$white,'arial.ttf',"$".$byus); //writing the donations by arqade.com to x:370 y:450
imagepng($donationImage); //displaying image
imagedestroy($donationImage); //removing image from memory
?&gt;


Code and additional resources are available at GitHub. This file is the one named mariobeta.php.

How can I improve this code? I'm not that good with PHP but I think that the code doesn't have any major issues.
header('Content-type: image/png');
imagepng($donationImage);


No real point in specifying the content type prior to actually outputting the image.

As mentioned in the other answer, json_decode(utf8_encode(file_get_contents("URI"))) can error out in multiple ways. If error reporting is active, this will result in a mismatch between content type and the response body. Even if it isn't, you end up sending a wrong content type for an empty body.



I hope you have placed a cache in front of that script. You are stalling the script with a REST query, and the memory footprint isn't negligible either.

If the web server isn't configured to cache, you want to fix that.

Caching the output with plain PHP is slightly more complex than that, with regard to acquiring a mutex on the cache during updates.



?&gt;


Eventually you will end up with a newline following the closing code tag (be it the IDE or the VCS software being "smart"). This will add to the output, resulting in an invalid response.

Avoid using the closing code tag if there isn't any content following. This actually applies to all PHP files.