Imagick setColor无法使用php [英] Imagick setColor not working with php

查看:127
本文介绍了Imagick setColor无法使用php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将所有像素设置为黑色。但它没有用。我得到了与原版相同的图像。

I've tried setting all pixels to black. But it isn't working. I am getting the same image as original.

这是我的代码:

$image = new Imagick(__DIR__."/image_new.jpg");

$i=0;
$j=0;

while ($i < 100)
{
    $j=0;
    while($j < 100)
    {
         $pixel = $image->getImagePixelColor($i, $j); 
         $pixel->setColor("#000000");
         $j++;
    }

    $i++;
}

header("content-type:image/jpeg");
echo $image;

图片大小为100x100。

Image size is 100x100.

任何想法?

推荐答案

Imagick :: getImagePixelColor 将返回 ImagickPixel 对象;哪个,会从原始的Imagick对象复制数据。更改像素的数据/状态后,您需要将像素同步回图像。为了帮助这个过程中, ImagickPixelIterator 对象已经提供 - 见 Imagick :: getPixelIterator 。这是一个简单的例子

The Imagick::getImagePixelColor will return an ImagickPixel object; which, would have copied data from the originating Imagick object. After altering the pixel's data/state, you would need to "sync" the pixel back to the image. To help with this process, a ImagickPixelIterator object has been provided -- see Imagick::getPixelIterator. Here's a quick example

$image = new Imagick(__DIR__."/image_new.jpg");
$pixel_iterator = $image->getPixelIterator();
foreach($pixel_iterator as $i => $pixels)
{
 if( $i < 100 )
 {
  foreach($pixels as $j => $pixel)
  {
   if( $j < 100 )
   {
    $pixel->setColor("#000000");
   }
  }
 }
 $pixel_iterator->syncIterator();
}

header("content-type:image/jpeg");
echo $image;

这篇关于Imagick setColor无法使用php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆