Question

I am creating an image with some text on it, stored in variables. And I thought dynamically generated images would be the way to go because I wanted to post the picture in a forum thread. But I quickly found out that the forum community doesn't allow those kind's of images to be displayed, so I need to export it to a .gif or .jpg or something similar so it can be posted.

Here is my code:

<?php

create_image();
exit();

function create_image()
{

include 'codex.php';

$width = 450;
$height = 400;

$Level = "1";
$attack = "4";
$accuracy = "7";
$agility = "0";
$defense = "0";
$evasion = "0";
$intelligence = "0";
$strength = "0";
$spirit = "0";

$image = ImageCreate($width, $height);

$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$bonus = ImageColorAllocate($image, 0, 200, 75);


ImageFill($image, 0, 0, $black);

//Base Stats
ImageString($image, 3, 30, 3, "Level: $Level", $white);
ImageString($image, 3, 40, 20, "Attack: $attack", $white);
ImageString($image, 3, 40, 40, "Accuracy: $accuracy", $white);
ImageString($image, 3, 40, 60, "Agility: $agility", $white);
ImageString($image, 3, 40, 80, "Defense: $defense", $white);
ImageString($image, 3, 40, 100, "Evasion: $evasion", $white);
ImageString($image, 3, 40, 120, "Intelligence: $intelligence", $white);
ImageString($image, 3, 40, 140, "Strength: $strength", $white);
ImageString($image, 3, 40, 160, "Spirit: $spirit", $white);

//Bonuses
ImageString($image, 3, 115, 20, "+ $attb", $bonus);
ImageString($image, 3, 130, 40, "+ $accb", $bonus);
ImageString($image, 3, 120, 60, "+ $agib", $bonus);
ImageString($image, 3, 120, 80, "+ $defb", $bonus);
ImageString($image, 3, 120, 100, "+ $evab", $bonus);
ImageString($image, 3, 155, 120, "+ $intb", $bonus);
ImageString($image, 3, 125, 140, "+ $strb", $bonus);
ImageString($image, 3, 120, 160, "+ $spib", $bonus);
header("Content-Type: image/jpeg");

ImageJpeg($image);

ImageDestroy($image);

}

?>

I tried installing Imagick, but I couldn't get it to work with my host. Is there any way I can have the image be exported as a .gif, and then be updated each time the information changes? Say I change the color of $white, it updated the .php page with the image, but then I would like it to re-export the picture to a directory on the server so I can externally link to it in bbc image tags on a forum thread.

I apologize if I am unclear, please tell me if my question doesn't make sense.

Note: The above code does work and produce the image I want, just not in a forum postable format. Also, the include codex.php just contains some stats variables and isn't crucial to the actual script.

Was it helpful?

Solution

  1. In the folder where you're storing your dynamic image, create a .htaccess file (if there isn't one already).
  2. In the .htaccess file, include the following line:

    AddType application/x-httpd-php .jpg
    

    (You could also potentially modify your configuration file, but that's not commonly available for hosted services, and requires a little bit more work than one line in a .htaccess, especially if you want actual jpg images elsewhere on the server.)

  3. Rename your dynamic image to have a .jpg extension instead of a .php extension.

Files with the .jpg extension in that folder should now be processed as PHP files. If the PHP sets the header to make the output a JPG image, the client browser has no idea there's anything special going on -- the browser sees a .jpg file containing JPG data.

If your host doesn't let you use .htaccess, however, you won't be able to simply use a dynamic image. I can't think of a means off-hand to automatically generate an actual image file which you could link to. (You could save the image file from PHP, but I can't think of a way to generate the new images dynamically when the information changed; you'd have to run the PHP script manually.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top