以PDF格式更改图像

时间:2018-03-02 13:28:28

标签: php pdf

嗨,我一直收到这个错误: 运行时不推荐使用的代码 - 不推荐使用Imagick :: clone方法,应该避免使用它

它始终围绕着这个功能:

protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
        if (empty($filehash)) {
            $filehash = md5($file);
        }
        // create temp image file (without alpha channel)
        $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
        // create temp alpha file
        $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
        if (extension_loaded('imagick')) { // ImageMagick
            // ImageMagick library
            $img = new Imagick();
            $img->readImage($file);
            // clone image object
            $imga = $img->clone();
            // extract alpha channel
            $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
            $img->negateImage(true);
            $img->setImageFormat('png');
            $img->writeImage($tempfile_alpha);
            // remove alpha channel
            $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
            $imga->setImageFormat('png');
            $imga->writeImage($tempfile_plain);
        } else { // GD library
            // generate images
            $img = imagecreatefrompng($file);
            $imgalpha = imagecreate($wpx, $hpx);
            // generate gray scale palette (0 -> 255)
            for ($c = 0; $c < 256; ++$c) {
                ImageColorAllocate($imgalpha, $c, $c, $c);
            }
            // extract alpha channel
            for ($xpx = 0; $xpx < $wpx; ++$xpx) {
                for ($ypx = 0; $ypx < $hpx; ++$ypx) {
                    $color = imagecolorat($img, $xpx, $ypx);
                    $alpha = ($color >> 24); // shifts off the first 24 bits (where 8x3 are used for each color), and returns the remaining 7 allocated bits (commonly used for alpha)
                    $alpha = (((127 - $alpha) / 127) * 255); // GD alpha is only 7 bit (0 -> 127)
                    $alpha = $this->getGDgamma($alpha); // correct gamma
                    imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
                }
            }
            imagepng($imgalpha, $tempfile_alpha);
            imagedestroy($imgalpha);
            // extract image without alpha channel
            $imgplain = imagecreatetruecolor($wpx, $hpx);
            imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
            imagepng($imgplain, $tempfile_plain);
            imagedestroy($imgplain);
        }
        // embed mask image
        $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
        // embed image, masked with previously embedded mask
        $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
        // remove temp files
        unlink($tempfile_alpha);
        unlink($tempfile_plain);
    }

我所要做的就是更改PDF中的图像,但出于某种原因,它不会允许我。我尝试在线查找,但我找不到相同的问题。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

正如Imagick::clone documentation中所述,此方法确实已弃用,并由object cloning替换。

将行$imga = $img->clone();替换为$imga = clone $img;