【问题标题】:PHP-Imagemagick image displayPHP-Imagemagick 图片展示
【发布时间】:2011-08-30 13:30:10
【问题描述】:

我有如下创建pdf缩略图的php代码;

<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
?>

效果很好。但是如果我想在网页中显示图像,我必须使用&lt;img src=""&gt; 标签。有没有办法使用&lt;img src=""&gt;..从语法和回显图像中删除header("Content-Type: image/jpeg"); ?或者有人告诉我如何使用语法在网页中显示图像。

我在我的 Windows Vista PC 中使用 php5 运行 apache..

【问题讨论】:

    标签: php imagemagick imagick


    【解决方案1】:

    就我而言,我找到了这样的解决方案:

                $im = new Imagick("http://www.yourserver.com/upload/file_name.pdf");
                $im->setResolution(300, 300);     // if higher image will be good to read
                $im->setIteratorIndex(0); // read first page
                $im->setImageFormat('jpg');
                header('Content-Type: image/jpeg');
    
                ob_start();
                print $im->getImageBlob(); 
                $contents =  ob_get_contents();
                ob_end_clean();
    
                echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />"; //output as image
    

    祝你好运。

    【讨论】:

      【解决方案2】:

      我可以看到有太多的答案不够准确,所以这里是我的:

      这将像您现在一样打印图像(在提出这个问题时)。作为@Vasil Dakov 回答的替代方法,您应该像这样修改我给您的 sn-p:

      <?php
      // ... Image generation goes here
      header("Content-Type: image/jpeg"); 
      ob_start();
      print $im->getImageBlob();
      $the_outputted_image = ob_get_flush();
      ?>
      // Assuming that you use MVC approach and you are storing $the_outputted_image in a object and passing it to the view(ie. index.html or the HTML below the code).
      //... Html code of index.html
      <img src="data:image/jpg;base64 <?php print $the_outputted_image; ?>" alt="image" title="IMagick Generated Image" />
      

      另一种方法是创建一个脚本来生成图像,将其保存在某个文件夹中(假设 img/ 是文件夹)并仅返回文件的路径+文件名+扩展名:

      <?php
      // ... Image generation goes here
      header("Content-Type: image/jpeg"); 
      $filename = 'img/' . md5(microtime()) . '.jpg'// Microtime is just as an example, you should use your own method.
      $fp = fopen($filename, "x"); //Creating and opening the file for write-only  
      $im->writeImageFile($fp); //Writing the image to the file pointer (I would recommend writing it using, fwrite(), because it is binary-safe writing method)
      fclose($fp);
      ?>
      
      // Html
      <img src="<?php print $filename; ?>" alt="image" title="IMagick Generated Image" />
      

      documentation for Imagick::writeImageFile

      【讨论】:

        【解决方案3】:

        使用 Imagick,您可以使用 base64 编码:

        echo '<img src="data:image/jpg;base64,'.base64_encode($img->getImageBlob()).'" alt="" />';`
        

        但是,这种方法有点慢,因此我建议早点生成并保存图像$img-&gt;writeImage($path)

        【讨论】:

          【解决方案4】:

          使用 base64 嵌入图像是解决问题的完全错误的方法,尤其是。像 php web 脚本这样无状态的东西。

          您应该使用 http 参数来拥有一个可以执行两个任务的 php 文件 - 默认将发送 html ,该参数将指示 php 文件打印图像。以下是执行此操作的“标准”方式-

          <?php
          if (!array_key_exists('display',$_GET))
          {
              print('<html><head></head><body><img src="'.$_SERVER['PHP_SELF'].'?display=image"></body></html>');
          } else
          {
              // The display key exists which means we want to display an image
              $file ="test.pdf";
              $im = new imagick(realpath($file).'[0]');
              $im->setImageFormat("png");
              $im->resizeImage(200,200,1,0);
              header("Content-Type: image/jpeg");
              $thumbnail = $im->getImageBlob();
              echo $thumbnail;
          }
          ?>
          

          【讨论】:

            【解决方案5】:

            唯一的解决方案是将您的图像转换为 base64 并将其包含为嵌入式 base64 图像 (data:image/png;base64,)。 Further reference.

            但这在 IE 6 和 7 中不受支持。

            【讨论】:

              【解决方案6】:

              您可以在页面中嵌入原始图像,请参阅下面的博客条目以获取页面语法示例。

              http://www.sveinbjorn.org/news/2005-11-28-02-39-23

              但我认为将缩略图保存在文件系统上并将其作为普通文件提供会更有效率。否则,您将在每次访问页面时生成缩略图。可能有人上传了这个PDF文件,所以你也可以在上传时生成缩略图。

              【讨论】:

                【解决方案7】:

                您可以尝试通过这种方式显示图像:

                // start buffering
                ob_start();
                $thumbnail = $im->getImageBlob();
                $contents =  ob_get_contents();
                ob_end_clean();
                
                echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />";
                

                【讨论】:

                • 我觉得应该是echo "&lt;img src='data:image/jpg;base64,".base64_encode($thumbnail)."' /&gt;";
                • 我同意@ashein 的观点,最好的解决方案是生成缩略图并使用它们。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2020-10-14
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多