douzhanshen0657 2014-01-06 17:00
浏览 43
已采纳

将生成的PHP映像导出为论坛可发布格式

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.

  • 写回答

1条回答 默认 最新

  • dongle0396 2014-01-06 17:19
    关注
    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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像