【问题标题】:Image showing sideways with TCPDF PHP library使用 TCPDF PHP 库横向显示的图像
【发布时间】:2020-09-27 16:37:45
【问题描述】:

我正在使用 TCPDF PHP 库来生成包含照片的 PDF 文档。出于某种原因,一些照片在我的计算机和网络上正确显示,但是当我将该图像放在 PDF 上时,它似乎是横向的。这只发生在某些图像上。大多数图像显示正确。

这是一个在 PDF 上横向显示但通常在网络和我的计算机上显示的示例图像:

这是图片在网络上的样子:

以下是 PDF 上的图像:

下面是相关代码:

// create new PDF document
$photoPDF = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$photoPDF->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$photoPDF->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$photoPDF->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$photoPDF->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$photoPDF->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$photoPDF->SetHeaderMargin(PDF_MARGIN_HEADER);
$photoPDF->SetFooterMargin(PDF_MARGIN_FOOTER);
$photoPDF->SetAutoPageBreak(FALSE, PDF_MARGIN_BOTTOM);
$photoPDF->setImageScale(PDF_IMAGE_SCALE_RATIO);
$photoPDF->setJPEGQuality(100);
$photoPDF->SetFont('helvetica', '', 10, '', true);

$xoffset = 58;
$width = 100;
$height = 100;
$filetype = "JPEG";
$imageid = "552556832.jpeg";
$url = "https://example.com/".$imageid;

$photoPDF->Image('D:\ReportPhotos\\'.$imageid, $xoffset, 35, $width, $height, $filetype, $url, '', true, 150, '', false, false, 1, false, false, false);
$photoPDF->writeHTMLCell($w=$width, $h=0, $x=$xoffset, $y=35+$height, $desc, $border=0, $ln=1, $fill=0, $reseth=true, $align='C', $autopadding=true);

如果我在 MS Paint 中打开图像并保存(不做任何更改),图像将正确显示。

我想让图像不横向显示在 PDF 上(或者如果这不可能)然后我想在网络上横向显示该图像,以便用户知道他们需要旋转图像而不必先生成PDF 看图像是横着看的。

【问题讨论】:

  • 我会很好奇文件中是否有Orientation 的信息,也许 PDF 编写者会遵守?
  • 克里斯,你是对的。我使用您链接中的代码示例来更正文件上传时的方向:stackoverflow.com/a/18919355/1855093 如果您想提交答案,我会将其标记为已接受。谢谢
  • 尽管我很想称赞,但我认为如果您发布修复它的代码可能会更好,因为我认为更多人会发现这很有帮助

标签: php image pdf tcpdf


【解决方案1】:

我使用以下代码解决了这个问题(如 Chris Haas 所建议的那样)。它将从 EXIF 数据中检测方向,并根据方向值旋转它。这是在上传图像时完成的。

此代码来自stackoverflow.com/a/18919355/1855093

move_uploaded_file($uploadedFile, $destinationFilename);
correctImageOrientation($destinationFilename);


function correctImageOrientation($filename) {
  if (function_exists('exif_read_data')) {
    $exif = exif_read_data($filename);
    if($exif && isset($exif['Orientation'])) {
      $orientation = $exif['Orientation'];
      if($orientation != 1){
        $img = imagecreatefromjpeg($filename);
        $deg = 0;
        switch ($orientation) {
          case 3:
            $deg = 180;
            break;
          case 6:
            $deg = 270;
            break;
          case 8:
            $deg = 90;
            break;
        }
        if ($deg) {
          $img = imagerotate($img, $deg, 0);        
        }
        // then rewrite the rotated image back to the disk as $filename 
        imagejpeg($img, $filename, 95);
      } // if there is some rotation necessary
    } // if have the exif orientation info
  } // if function exists      
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 2012-01-14
    • 2012-10-15
    • 2014-02-07
    • 1970-01-01
    • 2011-07-15
    • 2013-10-07
    • 2013-04-11
    相关资源
    最近更新 更多