查看页面时,我需要将上传的 PDF 转换为 Jpeg。它们中的大多数都可以工作,但时不时地,有些图像是不可见的。由于它在大多数情况下都有效,我相信它与单个 PDF 有关,但我希望能够编写代码以包含这些不一致之处。
下面是PHP代码:
$print_jpegs = glob($pathinfo["dirname"] . "/print_jpegs/" . $pathinfo["filename"] . "-*.jpg");
if (count($print_jpegs) <= 0) {
if (!file_exists($pathinfo["dirname"] . "/print_jpegs/")) {
mkdir($pathinfo["dirname"] . "/print_jpegs/", 0777, true);
}
$pdf = new Imagick($packages[$j]);
$pages = $pdf->getNumberImages();
$pdf->destroy();
if ($pages > 0) {
for ($k = 0; $k < $pages && $k <= 10; $k++) {
$print_jpeg = $pathinfo["dirname"] . "/print_jpegs/" . $pathinfo["filename"] . "-" . $k . ".jpg";
$im = new Imagick();
$im->setResolution(150, 150);
$im->readImage($packages[$j] . "[" . $k . "]");
$im->setImageColorspace(imagick::COLORSPACE_RGB);
$im->flattenImages();
$d = $im->getImageGeometry();
$max_w = 794; // 210mm @ 96ppi (A4)
$max_h = 1123; // 297mm @ 96ppi (A4)
$ratio = $d["width"] / $d["height"];
if ($max_w / $max_h > $ratio) {
$w = $max_h * $ratio;
$h = $max_h;
} else {
$w = $max_w;
$h = $max_w / $ratio;
}
$im->resizeImage($w, $h, imagick::FILTER_SINC, 1, false);
$im->setImageCompression(imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(100);
$im->setImageFormat("jpeg");
$im->writeImage($print_jpeg);
$im->clear();
$im->destroy();
$print_jpegs[] = $print_jpeg;
}
}
}