PHP图像将PDF转换为JPG后的黑色区域
我正在使用Imagick为PDF文件生成JPG缩略图,但其中一些生成时带有黑色区域()——我假设这是由PDF中的透明度引起的,但可以对此采取任何措施吗 我用来生成这些的代码:PHP图像将PDF转换为JPG后的黑色区域,php,pdf,imagick,Php,Pdf,Imagick,我正在使用Imagick为PDF文件生成JPG缩略图,但其中一些生成时带有黑色区域()——我假设这是由PDF中的透明度引起的,但可以对此采取任何措施吗 我用来生成这些的代码: $imagick = new Imagick($filename); $imagick->setIteratorIndex(0); $imagick->setImageFormat('jpg'); return $imagick->getImageBlob(); 有没有办法将PDF展平和/或添加白色背景
$imagick = new Imagick($filename);
$imagick->setIteratorIndex(0);
$imagick->setImageFormat('jpg');
return $imagick->getImageBlob();
有没有办法将PDF展平和/或添加白色背景,这样黑色区域就不会出现?试试下面的代码:
替代解决方案:这可能有助于:
<?php
//Note that the function returns an Imagick object and does not modify the existing object. Below is my code for converting a PNG with transparency into a JPG with a background color. This code illustrates the difference.
$im = new Imagick($filename);
$im->setImageBackgroundColor('white');
$im->flattenImages(); // This does not do anything.
$im = $im->flattenImages(); // Use this instead.
$im->setImageFormat('jpg');
$im->writeImage('image.jpg');
?>
像这样使用
类ImageConvertorLib{
private $CI;
/**
* loading codeIgniter instance
*/
public function __construct(){
$this->CI =& get_instance();
}
public function pdfToJpg($param){
$filename = $param['filename'];
$image_name = $param['image_name'];
$path = $param['path'];
$db_path = $param['db_path'];
$im = new Imagick();
$im->setResolution(220,220);
$im->readimage($filename."[0]");
$im->setImageFormat('jpeg');
$im->setImageBackgroundColor('#ffffff');
$im->flattenImages();
$image_name = $image_name.".jpg";//"save_as_name.jpg";
$imageprops = $im->getImageGeometry();
/*if ($imageprops['width'] <= 175 && $imageprops['height'] <= 300) {
// don't upscale
} else {
$im->resizeImage(175,300, imagick::FILTER_LANCZOS, 0.9, true);
}*/
$im->writeImage($path.$image_name);
if($im){
$Img = array();
$Img['status'] = 1;
$Img['image'] = ($db_path.$image_name);
return $Img;
}
$im->clear();
$im->destroy();
}
private$CI;
/**
*加载codeIgniter实例
*/
公共函数构造(){
$this->CI=&get_instance();
}
公共功能pdfToJpg($param){
$filename=$param['filename'];
$image_name=$param['image_name'];
$path=$param['path'];
$db_path=$param['db_path'];
$im=新的Imagick();
$im->setResolution(220220);
$im->readimage($filename.“[0]”);
$im->setImageFormat('jpeg');
$im->setImageBackgroundColor('#ffffff');
$im->flattleimages();
$image\u name=$image\u name.“.jpg”;/“另存为\u name.jpg”;
$imageprops=$im->getImageGeometry();
/*如果($imageprops['width']这里有一个解决方案,它只适用于PNG到JPG。
这段代码将白色背景添加到PNG中的透明区域,并将其转换为JPG
它是干什么的
这段代码从一个文件夹中获取所有PNG图像,将它们转换为带有白色背景的JPG图像,并将它们保存在另一个文件夹中
<?php
ini_set('max_execution_time', 3000);
$dir = 'transparent/';
$arr = scandir($dir);
for($i=0;$i<count($arr);$i++)
{
if($i==0 || $i==1)
{
}
else{
$input_file = "transparent/".$arr[$i];
$output_file = "White/".str_replace('.png','.jpg',$arr[$i]);
$input = imagecreatefrompng($input_file);
list($width, $height) = getimagesize($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);
}
}
?>
它的图像处理和GD在PHP中。
希望有帮助,您可以随意更改。此代码$im->setImageBackgroundColor(''ffffff');$im->FlattImages();已尝试使用setImageBackgroundColor和FlattImages方法–无效:($im->setImageCompose(\Imagick::COMPOSITE_OVER);
可能正在设置此选项将有所帮助。.添加$im->FlattImages()
t您找到解决方法了吗?这里也有同样的问题。。
<?php
ini_set('max_execution_time', 3000);
$dir = 'transparent/';
$arr = scandir($dir);
for($i=0;$i<count($arr);$i++)
{
if($i==0 || $i==1)
{
}
else{
$input_file = "transparent/".$arr[$i];
$output_file = "White/".str_replace('.png','.jpg',$arr[$i]);
$input = imagecreatefrompng($input_file);
list($width, $height) = getimagesize($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);
}
}
?>