【问题标题】:PHP Safari PDF to Image ProblemPHP Safari PDF 转图像问题
【发布时间】:2011-03-29 16:29:15
【问题描述】:

我正在尝试使用标题和 Imagick 将 PDF 显示为图像。我让它在其他浏览器中工作,除了在 Safari 中,图像带有黑色背景。这里有一个示例,生成示例的代码如下:

http://www.iptlock.com/pdf_test.php

<?php
header("Pragma: no-cache");
header("Expires: -1");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

$file_location='/media/files/bFdIdcxsOKtkBjzayEBD.pdf';

$im = new imagick($file_location."[0]");
$im->setImageFormat( "jpg" );
header( "Content-Type: image/jpeg" );
echo $im;
?>

所以我不确定出了什么问题或如何解决问题。

【问题讨论】:

  • 嗯,看来将其更改为 PNG 可以解决问题,但这仍然不能回答为什么 JPEGS 不起作用。
  • 在 Safari 5.0.3 for mac 上运行良好 :-)

标签: php css pdf safari imagick


【解决方案1】:

当我制作从 PDF 到 JPG/JPEG 的缩略图时,我也会遇到同样的情况。

这是我为此实现的功能:

function writeAttachmentsToDisk($mbox,$uid,$part,$dir,$subtype,$subject,$attachments,$readonly=false,$dataInici=''){
  if(!file_exists($dir)){
    mkdir($dir);
  }
  if(!empty($dataInici)){
    $subject = trim(str_replace($dataInici,'',$subject));
  }
  $filename = setNomArxiu(@iconv_mime_decode($subject,0,'UTF-8').(count($attachments)>0?'-'.strtoupper(alpha(count($attachments))):'')).'.'.$subtype;
  if(!$readonly || !file_exists($dir.'/'.$filename)){
      $temp_filename = 'tmp.eml';
      $email_file = $dir."/".$temp_filename;
      @imap_savebody($mbox,$email_file,$uid,$part,FT_UID | FT_PEEK);
      if(file_exists($email_file)){
          $tb = false;
          for($a=0;$a<count($attachments);$a++){
            if(files_identical($email_file,$attachments[$a])){
                $tb = true;
                break;
            }
          }
          if(!$tb){
              $command = "php -r '\$fh = fopen(\$argv[1], \"r\"); \$theData=fread(\$fh, filesize(\$argv[1])); fclose(\$fh); echo base64_decode(\$theData);' '$email_file' > $dir/$filename";
              exec($command,$output,$result);
              if($result!=0){
                unlink($email_file);
                @rmdir($dir);
                return false;
              } else {
                unlink($email_file);
                return $dir.'/'.$filename;
              }
          }else{
            unlink($email_file);
            @rmdir($dir);
            return false;
          }
      }else{
        unlink($email_file);
        @rmdir($dir);
        return false;
      }
  }else{
    return $dir.'/'.$filename;
  }
}

上述函数使用了与当前问题无关的其他函数(alpha、setNomArxiu、files_identical)。

我想问题一定出在这里:

$command = "php -r '\$fh = fopen(\$argv[1], \"r\"); \$theData=fread(\$fh, filesize(\$argv[1])); fclose(\$fh); echo base64_decode(\$theData);' '$email_file' > $dir/$filename";
exec($command,$output,$result);

生成的图像 can be seen here in most browsers 但在 Safari 中和在 Mac 中保存时显得较暗,几乎是黑色。

对这里发生的事情有什么想法吗?

谢谢!

【讨论】:

  • 有人知道这个问题是否与 Safari 有关吗?我会感谢你的 cmets。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
相关资源
最近更新 更多