PHP Imagick:转换ImageMagick命令行代码时出现问题

我在Windows 8.1上使用ImageMagick 7.0.8-66 Q16 x64 我的PHP在XAMPP上是7.4.1。

我已经在PHP中使用exec()成功运行了以下命令:

convert _temp_.jpg 
( +clone -background black -shadow 88x3+2+2 ) 
+swap -background none -layers merge 
+repage -background #eeeeee -layers flatten 
+repage -shave 3x3 
( -size 100x100 xc:#eeeeee ) 
+swap -gravity northwest -geometry +5+5 -compose over -composite output.jpg

它会拍摄图像,将其调整大小以适合100x100的缩略图,并在中性的#eeeeee背景画布上为图像添加阴影。可以。

我想重写它以使用Imagick PHP扩展,但是在翻译时遇到了麻烦。这是我翻译它(带有注释)的方法,该方法不起作用:

// convert _temp_.jpg 
$im = new imagick();
$im->readImage('_temp_.jpg');

// ( +clone -background black -shadow 88x3+2+2 ) 
$im_clone = clone $im;
$im_clone->setImageBackgroundColor('black');
$im_clone->shadowImage(88,3,2,2);

// +swap -background none -layers merge 
$im->setImageBackgroundColor('none');
$im->addImage($im_clone);
$im->mergeImageLayers(imagick::LAYERMETHOD_MERGE);

// +repage -background #eeeeee -layers flatten 
$im->cropImage(88,88,+5,+5);
$im->setImagePage(88,0);
$im->setImageBackgroundColor('#eeeeee');
$im->mergeImageLayers(imagick::LAYERMETHOD_flaTTEN);

// +repage -shave 3x3 
$im->cropImage(88,0);
$im->shaveImage(3,3);

// ( -size 100x100 xc:#eeeeee )
$im_pseudo = new Imagick();
$im_pseudo->newPseudoImage(100,100,'xc:#eeeeee');

// +swap -gravity northwest -geometry +5+5 -compose over -composite output.jpg
$im->setImageGravity(imagick::GRAVITY_NORTHWEST);
$im->compositeImage($im_pseudo,Imagick::COMPOSITE_OVER,5,5);

$im->writeImage('output.jpg');

我想念什么?

源图像:

PHP Imagick:转换ImageMagick命令行代码时出现问题

生成的图像:

PHP Imagick:转换ImageMagick命令行代码时出现问题

wbenit 回答:PHP Imagick:转换ImageMagick命令行代码时出现问题

正如我所说,如果您分步骤进行,您可以看到正在发生的事情。这来自php Imagick手册,并创建了Shadow:

/* Read the image into the object */ 
$im = new Imagick( 'DOqeZ.jpg' ); 
$im->setImageFormat("png"); 

/* Make the image a little smaller,maintain aspect ratio */ 
$im->thumbnailImage( 200,null ); 

/* Clone the current object */ 
$shadow = $im->clone(); 

/* Set image background color to black 
        (this is the color of the shadow) */ 
$shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) ); 

/* Create the shadow */ 
$shadow->shadowImage( 80,3,5,5 ); 

/* Imagick::shadowImage only creates the shadow. 
        That is why the original image is composited over it */ 
$shadow->compositeImage( $im,Imagick::COMPOSITE_OVER,0 ); 

/* Display the image */ 
$shadow->writeImage('shadow.png');

enter image description here

更新: 我以为我会让你看看剩下的事

创建背景并对其进行合成应该可以,但是范围应该更好。我还有其他事情要做,但是下面有一些更新的代码供您使用:

/* Read the image into the object */ 
$im = new Imagick( 'DOqeZ.jpg' ); 
$im->setImageFormat("png"); 

/* Make the image a little smaller,maintain aspect ratio */ 
$im->thumbnailImage( 80,0 ); 

/* Create the background image 100x100 with a background colour */
/* Gravity seems to have no effect  */
/* $shadow->setImageGravity(imagick::GRAVITY_CENTER);*/
$shadow->setImageBackgroundColor( '#eeeeee' );
$height = $shadow->getImageHeight();
$width =  $shadow->getImageWidth();
$shadow->extentImage( 100,100,(((100 - $width)/2)*-1 ),(((100 - $height)/2)*-1 ));    

$shadow->writeImage('shadow.jpg');

enter image description here

****更新了代码以使图像居中并为颜色使用十六进制值**** enter image description here

本文链接:https://www.f2er.com/2833679.html

大家都在问