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);
}
,而不是仅仅调整图像大小,以适合页面上的是什么? – AndyD273
@ AndyD273我也试过,但在这种情况下,它们内部的文本变得不可读。 – Pranab