【问题标题】:Create and then directly load an image from a folder inline with HTML创建并直接从与 HTML 内联的文件夹中加载图像
【发布时间】:2015-04-18 01:18:15
【问题描述】:

我想在网站中调用转储文件夹中的图像

我的image.php 页面如下所示:

<?php
    header('Content-type: image/jpeg');
    $jpg_image = imagecreatefromjpeg('Desert.jpg');
    $white = imagecolorallocate($jpg_image, 73, 41, 236);
    $font_path = 'OpenSans-Italic.TTF';
    $text = $_GET['name'] ;
    imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
    $image_url="dump/".rawurlencode(trim($text)).".jpg";
    imagejpeg($jpg_image,$image_url);
    readfile($image_url);
    imagedestroy($jpg_image);
?>

我在主页中使用了 Javascript,将我重定向到带有图像的result.php

我的result.php 页面如下所示:

<html>
    <body>
        <img src="image.php?name=<?php echo $_GET['name']; ?>" />
    </body>
</html>

现在我从image.php 调用图像,我想从转储文件夹调用它。有什么帮助吗?

【问题讨论】:

  • 为什么使用 $image_url 作为输出文件?无论如何,您的代码每次都会从头开始重建图像,所以您真正需要的是imagejpeg($jpg_image),它将直接输出 jpg 二进制数据。这消除了写入文件,然后再读回文件 - 非常浪费的操作。
  • 我希望每个创建的图像都应该存储在转储中,并从页面上的转储调用是否有任何可能的方法
  • 啊,这很清楚。就在这里。答案即将...
  • 请告诉我解决方案
  • 你明白了!请在下面查看我的答案。

标签: php html image


【解决方案1】:

这是你的results.php。首先,它创建图像并将其保存到 /dump 文件夹,然后呈现 HTML。此时图像将直接从 /dump 文件夹加载。

<html>
    <body>
        <?php 

        $text = $_GET['name'] ;
        $image_url="dump/".rawurlencode(trim($text)).".jpg";

        // Check if the file doesn't exist, create it
        if (!file_exists($image_url)) {   
            $jpg_image = imagecreatefromjpeg('Desert.jpg');
            $white = imagecolorallocate($jpg_image, 73, 41, 236);
            $font_path = 'OpenSans-Italic.TTF';
            imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
            imagejpeg($jpg_image,$image_url);
            imagedestroy($jpg_image);
         }

        ?>
        <img src="<?php echo $image_url; ?>" />
    </body>
</html>

一旦这个原型开始工作,你就可以增加更多的安全性。

【讨论】:

  • 我想添加 &lt;img src="dump/&lt;?php $_GET['name']?&gt;.jpg"/&gt; 我想在 result.php 中显示一个转储 url,它显示从 image.php 创建的图像
  • 并告诉我 result.php 的编码以从转储中获取图像,图像将使用用户提供的名称保存,并将显示在包含转储的 result.php 页面中文件夹网址
  • 如果我理解正确的话,这就是您的要求。
  • 我们可以用这个create image php直接添加html到页面
  • 是的,图片创建好了,然后渲染HTML,图片url就有效了。
【解决方案2】:

我认为你不应该那样做。

1.) 您直接在图像标签中使用$_GET['name']。您不应该信任用户输入,因此如果您在图像中使用它,请先过滤您的变量。

http://php.net/manual/en/function.filter-input.php

2.) 在上传时生成该图像以节省性能会更好。每次进行渲染时都会浪费大量 CPU。

所以最好保存原始图像并构建缓存并在上传后转换图像,或者您在方法中使用 if 来检查图像是否被缓存。

<?php
    header('Content-type: image/jpeg');
    $filename = rawurlencode(trim($text)).".jpg";
    if(!is_file(__DIR__.'dump_cache/'.$filename)) {
        $jpg_image = imagecreatefromjpeg('Desert.jpg');
        $white = imagecolorallocate($jpg_image, 73, 41, 236);
        $font_path = 'OpenSans-Italic.TTF';
        $text = $_GET['name'] ;
        imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
        $image_url="dump/".rawurlencode(trim($text)).".jpg";
        imagejpeg($jpg_image,$image_url);
        readfile($image_url);
        .... here write file to your cache folder .....
        imagedestroy($jpg_image);
    } else {
        echo file_get_contents(__DIR__.'dump_cache/'.$filename);
    }
?>

类似的东西。当您的图像未从您的转储文件夹加载时,请尝试在 __DIR__ 之前添加完整路径。

类似这样的:

$image_url= __DIR__."/dump/".rawurlencode(trim($text)).".jpg";

如果它是同一个文件夹。

【讨论】:

  • 我想从转储文件夹中调用 result.php 中的图像以及用户根据给定文本创建的图像
  • 是的,但我认为没有问题 ;) 这样做但看看安全性!
  • 你记得我说过我使用了一个将我重定向到 result.php 文件夹的 java 脚本,所以我想要对 result.php 进行编码,因为我无法在 image.php 文件中添加 html
  • 从我的主页到 result.php 但它应该从运行时创建的转储文件夹生成图像
猜你喜欢
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 2016-09-03
  • 2021-11-07
  • 2015-08-02
相关资源
最近更新 更多