我使用TCPDF在PDF中包含各种图像。图像大小不同。很少有图像是非常日志的,因为它们是完整的HTML页面转换为jpgs。当我试图包含这些图像时,他们没有移动到其他页面而是将其余部分修剪掉。这就是我正在做的事情:
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 :(得分: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);
}