How to count pdf color or bw pages using php except imagemagic?
Posted: 2017-07-11T00:13:17-07:00
I have one function to first conver each pdf page as image and then check into image that is colored or not .
So below executed command takes time because it in for loop and it cause server hang or takes it down.
So i want to found alternative way for it.
So below executed command takes time because it in for loop and it cause server hang or takes it down.
So i want to found alternative way for it.
Code: Select all
function getPDFInfo($file, $product_id) {
try {
$inputFile = $file;
$product = Product::find($product_id);
/*exec('G:\xampp\htdocs\pdf\bin\C#_ParsingTest.exe C:\Users\drindia\Desktop\pdf.pdf',$fill);
print_r($fill);
die;*/
echo convert(memory_get_usage(true));
if(extension_loaded('imagick')) {
$imagick = new Imagick();
$imagick->pingImage($inputFile);
$number_page = $imagick->getNumberImages();
$imagick2 = new Imagick();
$grey = 0;
$color = 0;
if($product->black_page_price == $product->color_page_price) {
$grey = $number_page;
$color = 0;
} else {
for($i=0;$i<$number_page;$i++) {
$result = $imagick2->readImage($inputFile.'['.$i.']');
$filename = "UBQhklw64WO8AuVcQzzGkgZoZcTHisvq_".$i.'.jpg';
$imagick2->setImageFormat("jpg");
$imagick2->writeImage('./public/frontend/tempimages/'.$filename);
$result = exec('convert ./public/frontend/tempimages/'.$filename.' -colorspace HSL -channel g -separate +channel -format "%[fx:mean]" info:');
//$result = exec('gs -o - -sDEVICE=inkcov color-or-grayscale-test.pdf');
if ($result > 0) {
$color++;
} else {
//echo "Page Number : ".$i."Color ".$imageType."<br>";
$grey++;
}
$imagick2->clear();
$imagick2->destroy();
File::delete('./public/frontend/tempimages/'.$filename);
}
}
$imagick->clear();
$imagick->destroy();
return ['grey' => $grey, 'color' => $color, 'total_pages' => $number_page];
} else{
echo "Error";
return ['grey' => '0', 'color' => '0', 'total_pages' => '0'];
}
} catch (Exception $e) {
return ['grey' => '0', 'color' => '0', 'total_pages' => '0'];
}
}