【问题标题】:PHP Imagick black areas after converting PDF to JPG将PDF转换为JPG后的PHP Imagick黑色区域
【发布时间】:2018-01-03 00:27:29
【问题描述】:

我正在使用 Imagick 为 PDF 文件生成 JPG 缩略图,但其中一些生成带有黑色区域 (http://i.imgur.com/fKBncKw.jpg) – 我假设它是由 PDF 中的透明度引起的,但可以做些什么吗?

我用来生成这些的代码:

$imagick = new Imagick($filename);
$imagick->setIteratorIndex(0);
$imagick->setImageFormat('jpg');
return $imagick->getImageBlob();

有没有办法使 PDF 变平和/或添加白色背景以使黑色区域不出现?

【问题讨论】:

  • 您找到解决方法了吗?同样的问题在这里..

标签: php pdf imagick


【解决方案1】:

这是一个仅适用于 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);

    }
}
?>

它在 PHP 中的图像处理和 GD。 PHP Manual

希望对您有所帮助,您可以随意更改。

【讨论】:

    【解决方案2】:

    试试这个代码:Imagick::setCompressionQuality

    $im = new imagick(realpath($file).'[0]');
    $im->setCompression(Imagick::COMPRESSION_JPEG);
    $im->setCompressionQuality(100);
    $im->setImageFormat("jpeg");
    $im->writeImage("imagename.jpg");
    

    替代解决方案:这可能会有所帮助:

    <?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');
    
    ?>
    

    【讨论】:

    • $im-&gt;setImageCompose(\Imagick::COMPOSITE_OVER); 可能会设置这将有所帮助..添加 $im-&gt;flattenImages();
    【解决方案3】:

    这样使用

    类 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();
    }
    

    }

    【讨论】:

    • 这段代码 $im->setImageBackgroundColor('#ffffff'); $im->flattenImages();
    • 已经尝试使用 setImageBackgroundCOlor 和 flattenImage 方法 - 没用 :(
    【解决方案4】:

    在无数次尝试使用 jpeg 图像附加 pdf 文件而不出现黑色区域​​后,我找到了解决方案:函数 transformImageColorspace

    按此顺序使用效果很好:

    $im = new Imagick();
    $im->readImage("file.pdf");
    
    $im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
    
    $im->setImageFormat('jpeg');
    $im->writeImage('image.jpg');
    

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 2015-01-03
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2010-10-09
      • 2013-11-02
      • 1970-01-01
      • 2014-07-03
      相关资源
      最近更新 更多