我需要在查看页面时将上传的PDF转换为Jpegs。它们中的大多数都可以工作但是偶尔会有一些图像不可见。 因为它大部分时间都有效,我认为它与单个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;
}
}
}