【发布时间】:2019-04-28 17:21:45
【问题描述】:
我上传一个文件,如果是“pdf”,它会搜索它是否有多个页面。 如果页面 > 1,则创建两个图像转换。 所以它可以工作,但我有一个重复的转换,所以如果我有一个 2 页的 pdf,它会创建 4 个文件,其中 2 个是具有不同名称的销售。 为什么?
if (isset($_POST)) {
// setto il tipo di file
$type = "png";
// se ho caricaato il file
if (!empty($_FILES['fileToUpload'])) {
$file_name = $_FILES['fileToUpload']['name'];//Get file name with extension
$file_type = $_FILES['fileToUpload']['type'];//Get only extension
$file_size = $_FILES['fileToUpload']['size'];//Get File Size
$file_tmp = $_FILES['fileToUpload']['tmp_name'];//Temporary file name that saves in the computer to be processed
$filesplit = pathinfo($file_name, PATHINFO_FILENAME);//Get only the name of file without extension
// I haven't set file size restriction as DICOM files would be more than 5MB
// If you wanna restrict files with size greater than 1MB just add the condition
// if($file_size > 1048576){ and finish the 'if' at appropriate line. Size is defined in KB
$file = $uploadpath.$file_name;
move_uploaded_file($file_tmp, $file);
// recupero il nome + estensione del file caricato
$path_parts = pathinfo($_FILES["fileToUpload"]["name"]);
// recupero solo il nome del file SENZA estensione
$nome_file = $path_parts["filename"];
// trasformo il nome del file nella notazione seo friendly
$nome_file = seo_friendly_url($nome_file);
// recupero estensione del file
$estensione_file = $path_parts['extension'];
// rinomino il file che ho caricato originale con nome seo friendly e aggiungo _BAK
$file_copia_rinominato = $uploadpath.$nome_file.'_BAK'.'.'.$estensione_file;
// copio il file nella stessa directory
copy($file, $file_copia_rinominato);
// leggo tutti i file a 288 dpi
$newname = time().'_converted.'.$type;//Rename file and set extension
// imposto la variabile che conta il numero di pagine
$variabile_nome = 0;
//preparo array per memorizzare il nome delle pagine
$array_multiplagina = array();
if ($estensione_file == "pdf") {
$imagick = new Imagick();//Define imagick function
$imagick->setResolution(288, 288);
$imagick->readImage($file);//Read the file
$imageprops = $imagick->getImageGeometry();
$imagick->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$imagick->setImageResolution(288, 288);
$imagick->resampleImage(288, 288, imagick::FILTER_UNDEFINED, 0);
$imagick->setImageBackgroundColor('white');
$imagick->setCompressionQuality(100);
$imagick->setCompression(Imagick::COMPRESSION_NO);
$imagick->setImageFormat("png");
$imagick->setImageDepth(8/* bits */);
// Set all other properties
$pages = $imagick->getNumberImages();
// vedo il numero di pagine
echo "il numero di pagine è:".$pages;// debug
echo "<br>";// debug
if ($pages > 1) {
foreach ($imagick as $index => $pdf_image) {
$variabile_nome++;
$nome_file_multiplo = "m".$variabile_nome."-".$newname;
echo $nome_file_multiplo;// debug
echo "<br>";// debug
$create = $imagick->writeImages('uploads/'.$nome_file_multiplo, false);//Write the file to uploads folder
echo $create; // debug
echo "<br>"; // debug
echo "pdf multipagina"; // debug
//$pdf_image->writeImage('destination/path/' . $index . '-image_file.jpg');
$nome_ogni_file = $nome_file_multiplo;
echo "<br>";// debug
// trick to retrieve the name
$array_multipagina[] = str_replace(".png", "-1.png", $nome_ogni_file);
}
// stampo array se è multipagina
print_r($array_multipagina);
} else {
echo 'PDF non è multipagina';
}
}
}
}
所以,转换没问题,但如果 pdf 有 2 页,我有 4 个文件:
- m1-1556471596_converted-0.png
- m1-1556471596_converted-1.png
- m2-1556471596_converted-0.png
- m2-1556471596_converted-1.png
m1-....这对夫妇与m2-....夫妇相同
【问题讨论】:
-
您的代码没有使用 $pdf_image 变量,所以 $imagick->writeImages() 仍然会写出页面。你可能可以解决这个问题。或者,显式获取当前图像可能更合适github.com/mkoppanen/imagick/issues/122#issuecomment-216480744
-
嗨,问题是循环内的
$create = $imagick->writeImages('uploads/'.$nome_file_multiplo, false);//Write the file to uploads folder。 $create 在我的例子中只是将图像创建为 pdf 的页面,所以现在的问题是如何获取新的文件名。它会在扩展名前自动添加“-0”、“-1”