【问题标题】:Image as a PHP File图像作为 PHP 文件
【发布时间】:2014-05-08 18:05:37
【问题描述】:

我对 PHP 还很陌生,我需要知道如何将文件显示为图像。比如打开http://example.com/script.php会显示一张图片。

我的理由是我需要把它放在<img>src 属性中。我希望图像根据现在的时间而改变。

我目前有 3 张图片可供循环使用。

我目前拥有的:

<?php

    $w = date('W');         # week
    $d = date('N');         # day
    $t = date('G');             # time

    dealWithTime($d);

    function dealWithTime(day) {
        if (day == 1) {
            # Monday
            if ($w == 13) {
                # Week 13

            } else if ($w == 14) {
                # Week 14
                if ($t >= 0 && $t <= 6) {
                    # Image = 1.png
                } else if ($t > 6 && $t <= 10) {
                    # Image = 2.png
                } else if ($t > 10 && $t <= 14) {
                    # Image = 3.png
                } else if ($t > 14 && $t <= 18) {
                    # Image = 1.png
                } else if ($t > 18) {
                    # Image = 2.png
                }
            }
        } else if (day == 2) {
            # Tuesday
        } else if (day == 3) {
            # Wednesday
        } else if (day == 4) {
            # Thursday
        } else if (day == 5) {
            # Friday
        } else if (day == 6) {
            # Saturday
        } else if (day == 7) {
            # Sunday
        }
    }

?>

【问题讨论】:

标签: php image


【解决方案1】:

使用 PHP 的 GD 库。 PHP 的网站有一些示例可以帮助您入门:

http://www.php.net/manual/en/book.image.php

【讨论】:

    【解决方案2】:

    首先,src 部分是正确的。您可以简单地使用 .php 文件作为图像的 src 并显示漂亮的图像。因此,一旦您确定了要向用户展示的图片,您可以通过以下几种方式来做到这一点:

    1. readfile - 你读取图像的内容,然后用proper headers 输出。
    2. redirect 到图像文件。但这里有一个警告:你必须send a 302 Found header 而不是301 Moved Permanently,因为现在的浏览器缓存永久重定向!

    【讨论】:

      【解决方案3】:

      完成逻辑后,让 PHP 设置 script.php 的内容类型。

      目前 script.php 的内容类型是默认的 text/html - 但您希望它显示图像,因此您需要执行以下操作:

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

      然后,无论你从逻辑中得到什么文件,我都会使用file_get_contents() 函数和echoprint() 图像。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 2018-02-07
        • 2014-11-25
        • 2015-07-15
        相关资源
        最近更新 更多