【问题标题】:PHP Imagick: problem converting ImageMagick command-line codePHP Imagick:转换 ImageMagick 命令行代码的问题
【发布时间】:2020-04-20 14:04:20
【问题描述】:

我在 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, 88, 0, 0);
$im->setImageBackgroundColor('#eeeeee');
$im->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);

// +repage -shave 3x3 
$im->cropImage(88, 88, +5, +5);
$im->setImagePage(88, 88, 0, 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');

我错过了什么?

源图片:

结果图片:

【问题讨论】:

  • 什么不起作用?在我进入下一步之前,我会完成每一步并让它发挥作用。我会尝试更改的一件事是将“”更改为“”
  • 我已经做到了。当我只处理与“convert temp.jpg ( +clone -background black -shadow 88x3+2+2 ) +swap -background none -layers merge ”对应的部分时,结果不一样.此外,使用单引号和双引号没有区别。
  • 你应该附上你的输入图像和预期的输出图像以及你得到的结果。
  • 完成。见上文。
  • 也许您可以添加它的外观。

标签: php image-processing xampp imagemagick imagick


【解决方案1】:

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

/* 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, 0 ); 

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

更新: 我想我会让你看看你能不能做剩下的事情

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

/* 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, 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, 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');

**** 更新代码以使图像居中并使用十六进制值作为颜色****

【讨论】:

  • 我不知道您是否理解所需的结果是调整输入图像的大小以保持纵横比但适合(在顶部)100x100 灰色背景。您在上面创建的内容很可爱,只是它只是一个带有阴影的图像。我的例程的目标(以及它在命令行版本中正确获得的结果)是原始的纵横比保留调整大小,阴影漂浮在中性灰色背景之上,因此所有步骤我的命令行过程。
  • ...并且“漂浮在中性灰色背景之上”我的意思是背景是合成图片的一部分。保存我上面提供的第二张图片,你会看到。
猜你喜欢
  • 2019-12-20
  • 2018-04-09
  • 2019-08-27
  • 2013-01-02
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多