图像显示使用PHP而不影响HTML代码

时间:2011-08-20 11:59:14

标签: php html image

我对使用函数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参数:

  

库/ image.php?图像= http://www.example.com/image&width=200

2 个答案:

答案 0 :(得分:3)

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

<?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).'" />
  ';

?>

答案 1 :(得分:1)

这基本上是不可能的。 webbrowser请求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();
相关问题