【问题标题】:Image display using php without affecting the html code图片显示使用php不影响html代码
【发布时间】:2011-08-20 11:59:13
【问题描述】:

我有一个关于使用函数 getImage_w($image,$dst_w) 显示图像的问题,该函数获取图像 URL ($image) 和该图像的目标宽度 ($size)。然后它重新绘制图像,根据目标宽度改变其高度。

这就是函数(在 libs/image.php 文件中):

function getImage_w($image,$w){
       /*** get The extension of the image****/
      $ext= strrchr($image, ".");
       /***check if the extesion is a jpeg***/
        if (strtolower($ext)=='.jpg' || strtolower($ext)=='.jpeg'){
       /***send the headers****/
        header('Content-type: image/jpeg');
        /**get the source image***/
        $src_im_jpg=imagecreatefromjpeg($image);
        /****get the source image size***/
        $size=getimagesize($image);
        $src_w=$size[0];
        $src_h=$size[1];
        /****calculate the distination height based on the destination width****/
        $dst_w=$w;
        $dst_h=round(($dst_w/$src_w)*$src_h);
        $dst_im=imagecreatetruecolor($dst_w,$dst_h);
        /**** create a jpeg image with the new dimensions***/
        imagecopyresampled($dst_im,$src_im_jpg,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
        imagejpeg($dst_im);
}

在一个文件 imagetest.php 我有这个代码部分:

<?php
require 'libs/image.php';
echo '<h1>HELLO WORLD : some html</h1>
<img src="'.displayImg_w('http://www.sanctius.net/wp-content/uploads/2010/05/Avatar-20.jpg',200).'">
';

过去,我使用 $_GET 参数来编写 URL 来定义图像。但是现在,我想直接在我的代码中使用这个函数。

问题是图像显示正确,但是

Hello World

浏览器不翻译 HTML 代码(我知道第一个代码已经发送了标头)但是我想知道如何正确显示图像而不影响 html 代码。并且也没有使用将图像的 URL 更改为这种不需要的形式的 get 参数:

libs/image.php?image=http://www.example.com/image&width=200

【问题讨论】:

  • 输入您的代码,让您的眼睛更轻松!!!

标签: php html image


【解决方案1】:

在我之前完全错误的答案之后,我希望以此来弥补它。试试这个代码:

<?php

  function getImage_w($image,$w){
    // Get the extension of the file
    $file = explode('.',basename($image));
    $ext = array_pop($file);
    // These operations are the same regardless of file-type
    $size = getimagesize($image);
    $src_w = $size[0];
    $src_h = $size[1];
    $dst_w = $w;
    $dst_h = round(($dst_w/$src_w)*$src_h);
    $dst_im = imagecreatetruecolor($dst_w,$dst_h);
    // These operations are file-type specific
    switch (strtolower($ext)) {
      case 'jpg': case 'jpeg':
        $ctype = 'image/jpeg';;
        $src_im = imagecreatefromjpeg($image);
        $outfunc = 'imagejpeg';
        break;
      case 'png':
        $ctype = 'image/png';;
        $src_im = imagecreatefrompng($image);
        $outfunc = 'imagepng';
        break;
      case 'gif':
        $ctype = 'image/gif';;
        $src_im = imagecreatefromgif($image);
        $outfunc = 'imagegif';
        break;
    }
    // Do the resample
    imagecopyresampled($dst_im,$src_im,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
    // Get the image data into a base64_encoded string
    ob_start();
    $outfunc($dst_im);
    $imgdata = base64_encode(ob_get_contents()); // Don't use ob_get_clean() in case we're ever running on some ancient PHP build
    ob_end_clean();
    // Return the data so it can be used inline in HTML
    return "data:$ctype;base64,$imgdata";
  }

  echo '<h1>HELLO WORLD : some html</h1>
  <img src="'.getImage_w('http://www.sanctius.net/wp-content/uploads/2010/05/Avatar-20.jpg',200).'" />
  ';

?>

【讨论】:

  • 操作人员知道这一点,但要求提供不使用它的解决方案。
  • [facepalm] @Shi 抱歉我还没睡。你是绝对正确的,你的解决方案就是答案......
  • @Simo TAQI 您可以按照 DaveRandom 的建议使用 urlencode(),即使 URI 仍然存在,只是“编码”,或者您使用某种数据库然后只分发标识符,比如image.phpimage=1&width=200`。根据请求,在数据库中查找标识符,从那里获取 URI,然后游戏继续执行。
  • 现在为此 +1。不过,请注意您的图像类型检测存在缺陷。很容易拥有一个名为…/image.jpeg 的URI,然后返回带有正确Content-Type: image/png 的PNG 数据。所以更好的方法是使用get_headers() 来检测Content-Type 并发送结果。 (或直接破解 gd 使其自动执行此操作。)
  • 当然,如果您打算这样做,您不妨选择一种格式并使用该格式。由于您从远程位置获取图像,将它们搞砸并将它们转发到浏览器,因此没有什么可以说明它们离开时的格式必须与它们到达时的格式相同。我就是这样做的,因为它一开始就是这样做的:-)
【解决方案2】:

这基本上是不可能的。网络浏览器请求 HTML 页面并期望 HTML。或者它请求图像并期望图像。您不能在一个请求中混合使用两者,因为只有一个 Content-Type 可以是有效的。

但是,您可以使用 data URI 将图像嵌入 HTML:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

请注意,base64 编码非常无效,因此请确保在浏览器支持的情况下明确压缩输出,例如使用 gzip

所以对你来说,它可能如下所示:

echo '<img src="data:image/jpeg;base64,' . base64_encode(displayImg_w('http://www.sanctius.net/wp-content/uploads/2010/05/Avatar-20.jpg',200)) . '">';

并确保displayimg_w() 不再输出标题。

另外,displayimg_w()最后需要调整,将图片数据作为字符串返回,而不是直接输出:

ob_start();
imagejpeg($dst_im);
return ob_get_flush();

【讨论】:

  • 谢谢你的朋友,但是这个呢???(iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==) 是图片的网址吗??
  • 代码是图片的base64编码的DATA。该字符串代表一个小红点(阅读链接,它解释了很多!)。不过请注意,这种方法不支持某些浏览器,包括 IE7 和以前的版本。
  • 不,它不是 URI,它是实际的编码数据 - 它是图像本身。
  • 我向displayimg_w() 添加了所需的调整,正如 DaveRandom 所指出的那样。
  • @Simo TAQI 我刚刚编辑了我的答案,所以它应该正是你所需要的,以弥补我之前的尴尬......
猜你喜欢
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2017-11-02
  • 2013-05-28
相关资源
最近更新 更多