lynx   »   [go: up one dir, main page]

Showing posts with label IMagick. Show all posts
Showing posts with label IMagick. Show all posts

Friday, July 31, 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>sample1151(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* スクリューの半径 */
$sr = 10;
/* 余白 */
$pad = 5;
/* 塗りつぶし色 */
$fc = "#eeeeee";

/* 円を描画 */
$im = new Imagick();
$im->newImage($sr*2+$pad*2, $sr*2+$pad*2, "none");
$im->setImageMatte(true);
$idraw = new ImagickDraw();
$idraw->setFillColor($fc);
$idraw->ellipse($sr+$pad,$sr+$pad,$sr,$sr,0,360);
$im->drawImage($idraw);

/* 直線部分を透明にする */
$im2 = new Imagick();
$im2->newImage($sr*2+$pad*2, $sr*2+$pad*2, "none");
$im2->setImageMatte(true);
$idraw2 = new ImagickDraw();
$idraw2->setStrokeColor($fc);
$idraw2->setStrokeWidth(3);
$idraw2->line(0,$im2->getImageHeight()/2,
$im2->getImageWidth(), $im2->getImageHeight()/2);
$im2->drawImage($idraw2);
$im2->negateImage(false);

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

/* 左上ねじ */
$im3 = $im2->clone();
$im3->rotateImage('none', rand(0,360));
$im3->cropImage($im2->getImageWidth(),
$im2->getImageHeight(),
($im3->getImageWidth() - $im2->getImageWidth())/2,
($im3->getImageHeight() - $im2->getImageHeight())/2
);

/* 右上ねじ */
$im4 = $im2->clone();
$im4->rotateImage('none', rand(0,360));
$im4->cropImage($im2->getImageWidth(),
$im2->getImageHeight(),
($im4->getImageWidth() - $im2->getImageWidth())/2,
($im4->getImageHeight() - $im2->getImageHeight())/2
);

/* 左下ねじ */
$im5 = $im2->clone();
$im5->rotateImage('none', rand(0,360));
$im5->cropImage($im2->getImageWidth(),
$im2->getImageHeight(),
($im5->getImageWidth() - $im2->getImageWidth())/2,
($im5->getImageHeight() - $im2->getImageHeight())/2
);

/* 右下ねじ */
$im6 = $im2->clone();
$im6->rotateImage('none', rand(0,360));
$im6->cropImage($im2->getImageWidth(),
$im2->getImageHeight(),
($im6->getImageWidth() - $im2->getImageWidth())/2,
($im6->getImageHeight() - $im2->getImageHeight())/2
);

/* ねじ画像を重ねあわせ */
$im7 = new Imagick("sf.jpg");
$im7->compositeImage($im3, Imagick::COMPOSITE_OVER,
0, 0, Imagick::CHANNEL_ALL);
$im7->compositeImage($im4, Imagick::COMPOSITE_OVER,
$im7->getImageWidth()-$im4->getImageWidth(), 0,
Imagick::CHANNEL_ALL);
$im7->compositeImage($im5, Imagick::COMPOSITE_OVER,
0, $im7->getImageHeight()-$im5->getImageHeight(),
Imagick::CHANNEL_ALL);
$im7->compositeImage($im6, Imagick::COMPOSITE_OVER,
$im7->getImageWidth()-$im6->getImageWidth(),
$im7->getImageHeight()-$im6->getImageHeight(),
Imagick::CHANNEL_ALL);

$im7->writeImage('sample1151a.png');

$idraw2->destroy();
$idraw->destroy();
$im7->destroy();
$im6->destroy();
$im5->destroy();
$im4->destroy();
$im3->destroy();
$im2->destroy();
$im->destroy();
?>
<img src="sample1151a.png" /><br />

</body>
</html>


元画像(sf.jpg)


出力画像(sample1151a.png)
Imagickで画像の四隅にネジを描画した画像

Wednesday, July 29, 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>sample1150(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* メッセージ(UTF-8でファイルを保存する) */
$msg = "ImageMagick";
/* 上下余白 */
$padtop = 10;
$padbottom = 40;
/* 看板余白 */
$padx = 5;
$pady = 10;
/* 棒の幅 */
$polew = 10;
/* 塗りつぶし色 */
$fc = "#7799ff";
/* 文字色 */
$tc = "white";

$im = new Imagick();
$idraw = new ImagickDraw();
/* フォント設定 */
//$idraw->setFont('C:\\Windows\\Fonts\\MSGOTHIC.TTC');
$idraw->setFont('Tahoma');
/* フォントサイズ設定 */
$idraw->setFontSize(30);
/* 文字列のサイズ取得 */
$metrics = $im->queryFontMetrics($idraw, $msg);
$tw = $metrics["textWidth"];
$th = $metrics["textHeight"];
$td = $metrics["descender"];

$im->newPseudoImage(
$tw+$padx*2,
$th+$pady*2+$padtop+$padbottom, "xc:none");
/* 看板 */
$idraw->setFillColor($fc);
$idraw->rectangle(0,$padtop,
$tw+$padx*2-1,
$padtop+$th+$pady*2-1);
$idraw->setStrokeColor($fc);
$idraw->setStrokeWidth($polew);
$idraw->rectangle(($tw+$padx*2)/2,0,
($tw+$padx*2)/2,
$padtop+$th+$pady*2+$padbottom);
$idraw->setFillColor($tc);
$idraw->setStrokeColor($tc);
$idraw->setStrokeWidth(0);
$idraw->annotation($padx, $padtop+$pady+$th+$td, $msg);
$im->drawImage($idraw);


$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$im->setImageMatte(true);
$args2 = array($im->getImageWidth()/2,
$im->getImageHeight(),1,rand(-3,3));
//$im->distortImage(Imagick::DISTORTION_SCALEROTATETRANSLATE,
// $args2, false);
// ScaleRotateTranslateDistortion in distort.h
$im->distortImage(3, $args2, true);

$im->writeImage('sample1150a.png');

$idraw->destroy();
$im->destroy();
?>
<img src="sample1150a.png" /><br />

</body>
</html>


出力画像(sample1150a.png)
Imagickで描画した看板

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で描画したグラデーションのかかったストライプ
Лучший частный хостинг