Проблемы с ориентацией при вставке изображения jpeg в pdf с помощью fpdf - PullRequest
0 голосов
/ 09 июля 2020

У меня есть папка с изображениями jpeg внутри. Если я использую следующее для сканирования папки и отображения изображений, все будет выглядеть так, как должно:

//Gets an array of folder contents (excluding directories)
$files = array_filter(scandir($dir, 0), function($item) use ($dir) {
return !is_dir($dir . '/' . $item);
});

$count = sizeof($files);

if ($count > 0) {

    foreach ($files as $filename) {
        <img src="<?php echo "...path.../" . $filename; ?>" >
        <br> <?php                  
    }
} ?>

enter image description here

I then want to create a pdf document with these images inserted and I am using fpdf for this however there are issues with how the images are displayed in the pdf. If I create the pdf and view it on screen within the browser it looks fine, but if I download it and view the pdf on the PC in Acrobat the images are rotated:

введите описание изображения здесь

Итак, теперь я смотрю на данные изображения, используя exif_read_data , чтобы понять, в чем проблема, и имею следующий код при вставке изображения в pdf с помощью fpdf:

$dir = getcwd();
$filename = $dir . ".. path ../" . $value;
$jpgFile = ".. path ../" . $value;
$newjpgFile = ".. path ../new" . $value;

$img = imagecreatefromjpeg($filename); //Read the image file into $img
$exif = exif_read_data($filename);

echo $exif['Orientation']; //For testing - this is printing 6 for both images so rotation needed

if ($img && $exif && isset($exif['Orientation'])) {
    
    $ort = $exif['Orientation'];

    if ($ort == 6 || $ort == 5)
        $newImg = imagerotate($img, 270, null);
    if ($ort == 3 || $ort == 4)
        $newImg = imagerotate($img, 180, null);
    if ($ort == 8 || $ort == 7)
        $newImg = imagerotate($img, 90, null);

    if ($ort == 5 || $ort == 4 || $ort == 7)
        imageflip($newImg, IMG_FLIP_HORIZONTAL);
}

file_put_contents($newjpgFile, $newImg); //Meant to save new image but NO FILE SAVED
$pdf->Image($newjpgFile); //Puts into the pdf

Значения 6 предполагают, что изображения на самом деле альбомные. Если это так, это приводит меня к моим вопросам:

1- Если изображения имеют ориентацию 6, почему image отображает портрет?

2- Почему изображения отображаются правильно в формате PDF в браузере, но поворачиваются при загрузке PDF-файла?

3- Почему строка file_put_contents не работает?

Любая помощь очень ценится.

...