Monday, July 27, 2009

ImageMagickとPHPでグラデーションのかかったストライプを描画する

Imagickでグラデーションのかかったストライプを描画するには、以下のコードを実行します。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja"
xml:lang="ja" dir="ltr">
<head>
<title>sample1139(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* 画像サイズ */
$sx=200;
$sy=200;
/* ストライプ数 */
$rays=16;
$sr=360/$rays;
/* 背景色 */
$bc="#f0f0f0";
/* ストライプ色 */
$sc1="#99bbff";
$sc2="orange";

$cx=$sx/2;
$cy=$sy/2;

$im = new Imagick();
$im->newImage($sx, $sy, "none");
//$im->setImageMatte(true);
$idraw = new ImagickDraw();
$idraw->setFillColor("white");
for($lc=0;$lc<$rays;$lc++){
$points[] = array(
'x' => $cx, 'y' => $cy);
$points[] = array(
'x' => $cx+cos(pi()*$sr*$lc/180)*$sx,
'y' => $cy+sin(pi()*$sr*$lc/180)*$sy);
$points[] = array(
'x' => $cx+cos(pi()*($sr*$lc+$sr/2)/180)*$sx,
'y' => $cy+sin(pi()*($sr*$lc+$sr/2)/180)*$sy);
$idraw->polygon($points);
}
$im->drawImage($idraw);
$im2 = new Imagick();
$im2->newPseudoImage($sx, $sy,
"radial-gradient:" . $sc1 . "-" . $sc2);
$im2->setImageMatte(true);

$im2->compositeImage($im, Imagick::COMPOSITE_DSTIN,
0, 0, Imagick::CHANNEL_ALL);

$im3 = new Imagick();
$im3->newImage($sx, $sy, $bc);
$im3->compositeImage($im2, Imagick::COMPOSITE_OVER,
0, 0, Imagick::CHANNEL_ALL);


$im3->writeImage('sample1139a.png');
$idraw->destroy();
$im3->destroy();
$im2->destroy();
$im->destroy();
?>
<img src="sample1139a.png" /><br />

</body>
</html>


出力画像(sample1139a.png)
Imagickで描画したグラデーションのかかったストライプ

No comments: