2

this is my first question and I'm planning to hang around in this forum. I'm very new to programming since I'm studying but I'm making great progress. So, with this in mind I'll try to be as detailed as possible.

The project I am working with is about creating a png-image using GD PHP. The script recieves data, calculates image WIDTH/HEIGHT according to these. From this data I then print out pixels on the image in different spots. Everything works great, it displays the image and saves it on the server, there's nothing wrong with that. But when I run the script, it outputs the image to the browser. I don't want that. I just want the script to process the image and save it to the server. I have searched plenty but haven't found anything about it.

Code for creating the image: I have to remove some code though, but it's not needed to answer my question. I suspect it's something with the header and imagecreatetruecolor. This is all the data I can give.

<?php

//Some calculations before

// --- START: CREATE IMAGE ---
$png = imagecreatetruecolor($WIDTH, $HEIGHT);
imagesavealpha($png, true);
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);

//Here's just a loop to print pixels

header("Content-type: image/png");
imagepng($png);

save($png); //Function used for saving

//Erase from memory
imagedestroy($png);
?>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Daniel
  • 71
  • 4
  • 1
    The `header` and `imagepng` lines are *only* useful if you want to display the image in the browser. If you don't want the script to display it, you don't need them. – iainn Dec 11 '17 at 10:35
  • Alrght, thank you for a very quick answer, I'll test it, right away! – Daniel Dec 11 '17 at 10:39
  • Worth noticing is that I needed imagepng($png) in my save function. Otherwise, the image wasn't saved properly. It still doesn't output anything to the browser though which is good. – Daniel Dec 12 '17 at 07:39
  • Yep, that sounds right. `imagepng` has two modes - if you give it one argument, it outputs the image to the browser. If you give it two, the second is used as the filename to save the image. – iainn Dec 12 '17 at 09:30

0 Answers0