2017-10-10 212 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); 
}