2017-10-10 216 views
0

我使用TCPDF將各種圖像包含在PDF中。圖像大小不同。幾乎沒有圖像是非常日誌,因爲它們是完整的HTML頁面轉換爲jpgs。當我試圖包含這些圖像時,他們沒有移到其他頁面,而是將其餘部分修剪掉。這是我在做什麼:TCPDF:將大圖分割成多個頁面

class MYPDF extends TCPDF { 
    public function Footer() { 
     $this->SetY(-15); 
     $this->SetFont('helvetica', 'I', 8); 
     $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); 
    } 
} 

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false); 
$pdf->setHeaderData('', 0, '', '', array(0, 0, 0), array(255, 255, 255)); 
$pdf->SetDisplayMode('fullpage', 'SinglePage', 'UseNone'); 
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 

$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 
$pdf->AddPage('P', 'A4'); 
$pdf->setJPEGQuality(100); 

list($width, $height) = getimagesize($__my_image); 
if (3 > $width/$height) 
    list($w, $h) = array(0, 300); 
else 
    list($w, $h) = array(540, 0); 

$pdf->Image($__my_image,10, 10, $w, $h, '', '', 'M'); 
$pdf->Output($__filename, 'F'); 

我想將圖像的修剪傳遞到下一頁,但它不這樣做。

+0

,而不是僅僅調整圖像大小,以適合頁面上的是什麼? – AndyD273

+0

@ AndyD273我也試過,但在這種情況下,它們內部的文本變得不可讀。 – Pranab

回答

0

PHP Graphics Draw (GD)庫有一些工具可以幫助你,比如裁剪。

一個例子:

$imgUrl = $_POST['imgUrl']; 
// original sizes 
$imgInitW = $_POST['imgInitW']; 
$imgInitH = $_POST['imgInitH']; 
// resized sizes 
$imgW = $_POST['imgW']; 
$imgH = $_POST['imgH']; 
// offsets 
$imgY1 = $_POST['imgY1']; 
$imgX1 = $_POST['imgX1']; 
// crop box 
$cropW = $_POST['cropW']; 
$cropH = $_POST['cropH']; 
// rotation angle 
$angle = $_POST['rotation']; 

$jpeg_quality = 100; 

$output_dir = "temp/"; 
$output_filename = "croppedImg_".rand(); 

$img_r = imagecreatefrompng($imgUrl); 
$source_image = imagecreatefrompng($imgUrl); 
$type = '.png'; 

// resize the original image to size of editor 
$resizedImage = imagecreatetruecolor($imgW, $imgH); 
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH); 

// rotate the rezized image 
$rotated_image = imagerotate($resizedImage, -$angle, 0); 

// find new width & height of rotated image 
$rotated_width = imagesx($rotated_image); 
$rotated_height = imagesy($rotated_image); 

// diff between rotated & original sizes 
$dx = $rotated_width - $imgW; 
$dy = $rotated_height - $imgH; 

// crop rotated image to fit into original rezized rectangle 
$cropped_rotated_image = imagecreatetruecolor($imgW, $imgH); 
imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0)); 
imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx/2, $dy/2, $imgW, $imgH, $imgW, $imgH); 

// crop image into selected area 
$final_image = imagecreatetruecolor($cropW, $cropH); 
imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0)); 
imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH); 

// finally output png image 
imagepng($final_image, $output_dir.$output_filename.".png", $png_quality); 

// Delete original uploaded image 
unlink($imgUrl); 

現在如何能夠適應是裁剪原始圖像的兩倍。
假設你知道頁面的最大尺寸是800像素。但圖像是1000像素。因此,您將它裁剪爲0至800像素並將其保存爲一個文件,然後將第一個文件保存爲800至1000像素,然後將這兩個圖像放入您的PDF中。

我沒有測試過這一點,但這樣的:

if($imgH > 800){ 
    $diff = $imgH - 800; 

    $file1 = imagecreatetruecolor($cropW, 800); 
    imagecolortransparent($file1, imagecolorallocate($file1, 0, 0, 0)); 
    imagecopyresampled($file1, $large_image, 0, 0, 0, 0, $cropW, 800, $cropW, 800); 
    imagepng($file1, $output_dir.$output_filename."-1.png", $png_quality); 

    $file2 = imagecreatetruecolor($cropW, 800); 
    imagecolortransparent($file2 , imagecolorallocate($file2 , 0, 0, 0)); 
    imagecopyresampled($file2 , $large_image, 0, 800, 0, 800, $cropW, $diff, $cropW, $diff); 
    imagepng($file2, $output_dir.$output_filename."-2.png", $png_quality); 
}