2

I'm using the TCPDF PHP library to generate PDF documents that contain photos. For some reason, some of the photos appear correctly on my computer and on the web, but when I place that image on the PDF, it appears to be sideways. This only happens to some images. Most of the images appear correctly.

Here's a sample image that's displaying sideways on the PDF but normally on the web and my computer:

enter image description here

Here's what the image looks like on the web:

enter image description here

Here's what the image looks like on the PDF:

enter image description here

Here's the relevant code:

// 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);

The image will appear correctly if I open the image in MS Paint and save it (without making any changes).

I want to either have the image not appear sideways on the PDF (OR if that's not possible) then I want to display that image sideways on the web so the user knows they need to rotate the image without having to first generate the PDF to see the image is sideways.

user1855093
  • 373
  • 1
  • 2
  • 14
  • 1
    I would be curious if there is [`Orientation`](https://stackoverflow.com/a/3657153/231316) information in the file that, and maybe the PDF writer is obeying that? – Chris Haas Jun 08 '20 at 16:55
  • Chris, you're correct. I used the code sample from your link to correct the orientation when the file is uploaded: https://stackoverflow.com/a/18919355/1855093 If you want to submit the answer, I will mark it as accepted. Thanks – user1855093 Jun 08 '20 at 18:35
  • 1
    As much as I'd like to take credit, I think it might be better if you posted the code that fixed it since I think more people would find that helpful – Chris Haas Jun 08 '20 at 21:34

1 Answers1

2

I fixed this by using the following code (as suggested by Chris Haas). It will detect the orientation from the EXIF data and will rotate it based on the orientation value. This is done when the image is uploaded.

This code came from 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      
}
user1855093
  • 373
  • 1
  • 2
  • 14