14

So I have this IM command ($plistnew is a list of coords as you'd expect for polygon):

convert in.png ( -size 101x101 xc:black -fill white \
    -draw "polygon $plistnew" -alpha off \
    -crop 101x100+0+1 +repage \
    -scale 101x1! ) \
    -clut out.png

So I need to convert this to pure PHP. I have been pretty much successful except for one remaining issue with setImageAlphaChannel().

Anyway, this is my PHP:

$tmpa = new Imagick(); // for the image I'm assuming is generated inside the parens
$tmpa->newPseudoImage(101, 101, 'canvas:black'); // xc:black

$draw = new ImagickDraw();
$draw->setFillColor(new ImagickPixel('white')); // -fill white
$draw->polygon($points); // -draw "polygon $plistnew"

$tmpa->drawImage($draw);
$tmpa->setImageAlphaChannel(self::ALPHACHANNEL_DEACTIVATE); // -alpha off
$tmpa->cropImage(101, 100, 0, 1); // -crop 101x100+0+1

// +repage
$tmpa->resetImagePage('');

$tmpa->scaleImage(101, 1); // -scale 101x1! -- I think scaleImage() ignores ratio per the ! by default ... I'm not positive though.

$im = new Imagick('in.png');
$im->clutImage($tmpa); // -clut
$im->writeImage('out.png');
$tmpa->destroy();

The $points variable is an array formed properly for use with ImagickDraw::polygon().

This line:

$tmpa->setImageAlphaChannel(self::ALPHACHANNEL_DEACTIVATE);

flat out doesn't work. It produces this error:

PHP Fatal error:  Uncaught exception 'ImagickException' with message 'Unable to set image alpha channel'

When I comment that line out everything seems to be working fine otherwise. How can I prevent this error?

gregghz
  • 3,925
  • 7
  • 40
  • 69
  • 1
    You can find all the options here: http://www.imagemagick.org/script/command-line-options.php?ImageMagick=mcok6gc8c92rvajik8ldkhmd90 101x1! means the image is sized to 101x1 and the aspect ratio is ignored. +repage "reset the virtual canvas " in otherwords the image canvas which you can not see is resized from 101x101 to 101x1. If you checked the image in another program before and after you would see a checkerdoard image around the new image; this is removed. – Bonzo May 29 '12 at 19:14
  • thanks, I've updated to reflect new code/comments based on the info you gave. – gregghz May 29 '12 at 19:43
  • What are your polygon values and I will try your code as it could be a version problem. – Bonzo May 29 '12 at 19:54
  • ah, my appologies about $tmpA .. that's just a temporary image that is being processed, that's just the input image in this case. This is part of a larger script that does some other processing as well. I'll edit that real quick. Also, polygon values: 4,0 27,33 46,53 62,73 100,86 – gregghz May 29 '12 at 20:39
  • I have to go now; I had different problems to you and assume it was version problems. I could not use newPseudoImage and also get stuck on the setImageAlphaChannel. When commenting that out I get one more error with $this-> Have you thought about using exec( ) ? – Bonzo May 29 '12 at 21:24
  • yes, it works with exec but it's slower and restrictions in place prevent this from being a viable solution. And the problem with $this-> ... that's because the code I listed above is actually part of a method in a class. I'll modify it so it works without the class and $this reference. – gregghz May 30 '12 at 14:10
  • I give up as I can not deactivate the alpha channel. Out of interest the image produced looks the same using the command line and Imagick after commenting out the alpha deactivet part. – Bonzo May 30 '12 at 17:02
  • Forgot to say the file is larger though. – Bonzo May 30 '12 at 17:11
  • Bonzo, thanks so much for the input. It's also working for me now. I removed a section of code that was outputting debug info. For some reason that cleared up the issues I was having. -- Leaving this unanswered though because the ALPHA channel thing still doesn't work. – gregghz May 31 '12 at 16:18
  • I've also edited the question to reflect that the alpha channel is the only issue now. – gregghz May 31 '12 at 16:22

3 Answers3

7

I also was having issues with removing the alpha channel of PNG's and GIF's, so I tried a couple things, but ended up using this:

// Image has transparency
if ($image->getImageAlphaChannel()) {

    // Remove alpha channel
    $image->setImageAlphaChannel(11);

    // Set image background color
    $image->setImageBackgroundColor('white');

    // Merge layers
    $image->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);
}

I went with using "11" because imagick::ALPHACHANNEL_REMOVE wouldn't work with my version. For reference on imagick::ALPHACHANNEL_REMOVE, see this comment: http://php.net/manual/en/imagick.flattenimages.php#116665

stwhite
  • 3,156
  • 4
  • 37
  • 70
2

Per the manual for setImageAlphaChannel

This method is available if Imagick has been compiled against ImageMagick version 6.3.8 or newer.

You'll likely need to build the Imagick extension against an appropriate version of imagemagick on your own. Surely you can google for the steps, but here is a rough example of what I've done on Ubuntu, YMMV

Install imagemagick from source

apt-get install build-essential libpng12-dev libglib2.0-dev libfontconfig1-dev zlib1g-dev libtiff4-dev libexif-dev libfreetype6-dev

mkdir sources
cd sources
wget http://www.imagemagick.org/download/ImageMagick.tar.gz
tar -xzf ImageMagick.tar.gz

new_dir=$(ls -1 | grep ImageMagick-)
cd $new_dir
./configure --prefix=/usr --without-lzma --without-jng --without-jbig --without-mpeg --without-ps
make
make install

Install pecl

sudo apt-get install php5-dev php-pear

Install php imagick from source

sudo pecl config-set preferred_state beta
sudo apt-get remove --purge php5-imagick
sudo pecl install imagick
sudo bash -c 'echo extension=/usr/lib/php5/20121212/imagick.so > /etc/php5/mods-available/imagick.ini'
sudo php5enmod imagick

Verify php imagick is installed and setImageAlphaChannel is available

php --rc Imagick | grep -i setImageAlphaChannel

If grep finds a match, the installation was successful.

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
-1

I haven't tested this, but if your end goal is the generate an opaque image with no alpha channel support, perhaps try setting the Imagick::setFormat function to png24.

$tmpa->setFormat('png24');

This will force the Imagick object to use the opaque 24bit PNG format, rather than the opaque or transparent 32bit PNG format. You may have to set a background color or something to that effect to ensure that any alpha objects added to the image are dealt with appropriately.

Andrew Odri
  • 8,868
  • 5
  • 46
  • 55