簡體   English   中英

為什么我想像一下打開開關backgroundcolor?

[英]Why does Imagick turn the switch backgroundcolor?

我正在嘗試將pdf轉換為jpeg,修剪內容周圍的空格並將其大小調整為300x600。在PHP中,使用ImageMagick 6.7.7-10

這是我的代碼:

$im = new \Imagick();
$im->setBackgroundColor("white");
$im->readimage($url);
$im->setImageFormat("jpeg");
$im->trimImage(0);
$im->resizeImage(300, 600, Imagick::FILTER_LANCZOS, 0.9);
$im->writeImage($tmpFilePath);
$im->clear();
$im->destroy();

這是PDF
http://cs1.fuman.de/file.php/1AOrL6-PzT71Z-dk0000-CsjquC和此處生成的JPG http://cs1.fuman.de/file.php/1AOrL0-kWAl8P-ml0000-xAhOiw

有誰知道,這是怎么回事?

預先感謝JD

我不知道為什么背景顏色被忽略。 我認為這與JPEG沒有“背景”顏色的概念有關,因此,當圖像從具有alpha轉換為不具有alpha時,就會發生不良情況。

我建議使用以下代碼:

$imagick = new \Imagick();

// Make the image be large when read from PDF so have decent quality later
$imagick->setResolution(92, 92);

// only use the first page of the PDF
$imagick->readimage("./orig.pdf[0]");

// Make a white background imge
$canvas = new Imagick();
$canvas->newPseudoImage(
    $imagick->getImageWidth(),
    $imagick->getImageHeight(),
    'canvas:white'
);

// Need to use png format to keep alpha channge
$imagick->setImageFormat('png');

// Composite our image, into the white background
$canvas->compositeImage($imagick, \Imagick::COMPOSITE_ATOP, 0, 0);
$canvas->resizeImage(300, 600, Imagick::FILTER_LANCZOS, 1);
$canvas->setImageFormat('png');
$canvas->writeImage("output.png");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM