Wiriting text onto a PNG and rendering

I’m having some problems trying to figure this one out.

I’d like to create .png’s based on data I’ve pulled off a webpage.

I currently have it setup to where it adds the text to the png, but it doesn’t create and upload a new png, its my same base.png just with text positioned over it. I’d like to get it to render that out, or find a different method for the same thing. I’m using GD Library, but am not very familiar with it.

Any ideas?

You must be using the same input and output filename - post your code to get help with it.


```php
<?php
$image = ImageCreateFromPNG($_GET['base1']);
$color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66);
$font = $_GET['font'];
$fontSize = $_GET['fsize'];
$fontRotation = $_GET['frot'];
$str = $_GET['str'];

/* Shadow */
ImageTTFText($image, $fontSize, $fontRotation, 7, 22, $colorShadow, $font, $str);

/* Top Level */
ImageTTFText($image, $fontSize, $fontRotation, 5, 20, $color, $font, $str);

header("Content-Type: image/PNG");
ImagePng ($image);
imagedestroy($image);
?>


That is the code, basically C+P'd off another website, but its not exactly what I'm looking for. It creates the png by just overlapping text and variables on it, but I'd like for it to render out the actual image and upload it to my server. This would make custom .png's for every entry.

This is the area you need to look at and as you say it is displaying the image without saving it.


header("Content-Type: image/PNG");
ImagePng ($image);
imagedestroy($image);

Change it to:


ImagePng ($image, "new_file_name");
// OR
// ImagePng ($image, $new_file_name);
imagedestroy($image);

Alright, I seem to have gotten that all worked out.

Now I’m using GD Library, but need to transfer a variable and value from one file.php to anotherfile.php.

I’m trying to use the $_SESSION like so;

File.php:
$_SESSION['gt'] = $gt
~~~~~~~
AnotherFile.php:
$gt = $_SESSION['gt']

This seems to give an error to the line of code following the AnotherFile.php. And also an error to the line of code following the code listed above in File.php.

Am I using $_SESSION in a wrong manner?

Edit: /SlitWrists

I forgot the semicolon (“;”).

I got that whole problem worked out ^^

But now I’m running into a problem. I ended up including the 2 files in eachother’s, that way I can just call the variables like normal.

However, this gave me an error with the header. It seems to think

header("Content-Type: image/PNG");

AND

<style type="text/css">

Are the same. I closed the <style> brackets and everything. I’m not sure why its giving me the error.

Cannot modify header information - headers already sent by (output started at directory/index.html:6) in directory/script.php on line 20

For experiment remove the line
header(“Content-Type: image/PNG”);
Let PHP to decide the content type.

If you’re going to send a header() call, it must be done before ANY output is sent to the browser.
images should not be included. If you’re using php to generate the image, just use the php file as the SRC of the image tag.

<img src=‘picture.php’> is completely valid as long as picture.php returns data consistent with an image format (has the right header and such.)

That is exactly what I was doing, but in the file called ‘picture.php’ I wanted to access a variable in the original file. I was going to name my png file equal to a $variable.png.

From memory you can access a variable but can not remember how I did it now; I think something like this worked:


<img src='picture.php?variable.png'>
OR
<img src='picture.php?variable=variable.png'>

I’d like to try and keep the url as clean as possible.