使用 PHP 显示图像资源


Displaying an image resource with PHP

我试图简单地获取一个测试图像,稍微裁剪一下,然后显示它。问题是,我是从 PHP 开始这样做的,到目前为止我所做的每一次尝试都让我更加沮丧;/* 互联网告诉我做我已经做过的事情。*/

好的,那你做了什么??

我已经尝试了各种方法来获得$image本身。我尝试了以下方法:

  1. 使用 imagecreatefromstring($url);/*,其中$url是图像的位置(我从随机站点中提取,这是我的项目所必需的 */
  2. 使用 imagecreatefromstring(file_get_contents($url));,然后在 <div> 标记中回显图像
  3. 使用 imagecreatefromstring(file_get_contents($url));,然后在 <img> 标记中回显图像
  4. 做3.除了使用imagejpeg($image)
  5. 做4.,除了,这次,把header('Content-Type: image/jpeg');

好吧,发生了什么事?

第一次尝试返回了一个很好的错误,指出无法识别$image。第二次尝试似乎有效,但我得到的不是图像,而是以下文本:Resource id #5第三次尝试给了我一堆胡言乱语。第四次尝试也给了我一堆胡言乱语。第五次尝试给我一个黑屏,并显示以下错误消息:The image “http://localhost/ResearchProject/sampleImageDisplay.php” cannot be displayed because it contains errors.

这是最终的代码(和标题.php只包含显示网页所需的所有HTML标签(DOCTYPE标签,html标签,元标签...):

<?php
    include "header.php";
    $url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
    $image = imagecreatefromstring(file_get_contents($url));
?>
        <img>
            <?php header('Content-Type: image/jpeg');  imagejpeg($image); ?>
        </img>
        <?php imagedestroy($image); ?>
    </body>
</html>

为什么我不能显示这个简单的图像?

这段代码对我有用。只需创建空的 php 文件并粘贴此代码即可。它应该返回一个半大胆的人的照片。

$url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
$im = imagecreatefromstring(file_get_contents($url));
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}

首先...

你必须确保在php.ini中启用了"allow_url_fopen"。

如果您有权访问此文件,只需更改

;allow_url_fopen

allow_url_fopen

但是.ini某些主机会禁用对 php 的访问。

那么你所要做的就是这个....

$url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
$image = file_get_contents($url);

然后,您可以临时存储它以调整其大小或其他任何内容...

//Store in the filesystem.
$fp = fopen("/location/to/save/tempimage.jpg", "w");
fwrite($fp, $image);
fclose($fp);

你的主要规划问题是: 你试图将你的图像包含在 html 代码中。为此,您需要 2 个 phps:

  1. 一个包含带有img标签的html代码的php文件(src必须是第二个php文件)
  2. 一个php文件,用于读取您的基本图像并输出二进制图像数据

your_htmlpage.php

<html>
   <?php
        include "header.php";
   ?>
   <body>
        <img src="your_image.php">
   </body>
</html>

your_image.php

<?php
  $url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
  $image = imagecreatefromstring(file_get_contents($url));
  if($image !== false) {
    header('Content-Type: image/jpeg'); 
    imagejpeg($image);
    imagedestroy($image);
  } else {
    header("X-Error", true, 404);
    echo "Error loading remote image";
  }
  ?>

另一种选择是将图像作为base64编码字符串发送到浏览器。但这意味着整个图像数据是html代码的一部分。例如,喜欢这个:

<html>
   <?php
        include "header.php";

        $url = "http://byebyedoctor.com/wp-content/uploads/2010/12/alopecia-areata-3.jpg";
        $image = imagecreatefromstring(file_get_contents($url));
        if($image !== false) {
          // start buffering and catch image output
          ob_start();
          imagejpg($image);
          $contents =  ob_get_contents();
          ob_end_clean();
          imagedestroy($image);
          $img_data = "data:image/jpg;base64,".base64_encode($contents);
        } else {
          $img_data = "";
        }
   ?>
   <body>
        <img src="<?php echo $img_data; ?>">
   </body>
</html>