Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, December 28, 2010

PHPとGDで半透明のノイズがかかった画像に変換する

PHPとGDで半透明のノイズがかかった画像に変換するには、以下のコードを実行します。

<!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>gd_test7</title>
</head>
<body>
<?php
// 入力ファイル名
$fn = "sf.jpg";
// 入力ファイルサイズ取得
$bimg = imagecreatefromjpeg($fn);
//imagefilter($bimg, IMG_FILTER_GRAYSCALE);
$sx = imagesx($bimg);
$sy = imagesy($bimg);

$img = imagecreatetruecolor($sx, $sy);
imagesavealpha($img, true);
imagealphablending($img, false);
// 半透明のノイズをかける
for($ly=0;$ly<$sy;$ly++){
for($lx=0;$lx<$sx;$lx++){
$pa = rand(5,40);
$px = imagecolorat($bimg, $lx, $ly);
$pr = ($px >> 16) & 0xff;
$pg = ($px >> 8 ) & 0xff;
$pb = $px & 0xff;
imagesetpixel($img, $lx, $ly,
imagecolorresolvealpha($img, $pr, $pg, $pb, $pa));
}
}
// ファイル出力
imagepng($img, "gd_test7.png");
// 開放
imagedestroy($img);
imagedestroy($bimg);
?>
<img src="gd_test7.png" /><br />
</body>
</html>



元画像


出力画像


動作環境
Apache httpd 2.2.17, PHP5.3.3

Wednesday, December 22, 2010

PHPとGDで半透明ストライプのかかった画像に変換する

PHPとGDで半透明ストライプのかかった画像に変換するには、以下のコードを実行します。

<!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>gd_test5</title>
</head>
<body>
<?php
// 入力ファイル名
$fn = "sf.jpg";
// 入力ファイルサイズ取得
$bimg = imagecreatefromjpeg($fn);
//imagefilter($bimg, IMG_FILTER_GRAYSCALE);
$sx = imagesx($bimg);
$sy = imagesy($bimg);

$img = imagecreatetruecolor($sx, $sy);
imagesavealpha($img, true);
imagealphablending($img, false);
// 半透明のストライプの入った画像を作成
for($ly=0;$ly<$sy;$ly++){
$pa = ($ly % 2 == 0)?0:0x30;
for($lx=0;$lx<$sx;$lx++){
$px = imagecolorat($bimg, $lx, $ly);
$pr = ($px >> 16) & 0xff;
$pg = ($px >> 8 ) & 0xff;
$pb = $px & 0xff;
imagesetpixel($img, $lx, $ly,
imagecolorresolvealpha($img, $pr, $pg, $pb, $pa));
}
}
// ファイル出力
imagepng($img, "gd_test5.png");
// 開放
imagedestroy($img);
imagedestroy($bimg);
?>
<img src="gd_test5.png" /><br />
</body>
</html>



元画像


出力画像


動作環境
Apache httpd 2.2.17, PHP5.3.3

Sunday, December 19, 2010

PHPとGDでグラデーションのかかった画像に変換する

PHPとGDでグラデーションのかかった画像に変換するには、以下のコードを実行します。

<!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>gd_test4</title>
</head>
<body>
<?php
// 入力ファイル名
$fn = "sf.jpg";
// 入力ファイルサイズ取得
$bimg = imagecreatefromjpeg($fn);
imagefilter($bimg, IMG_FILTER_GRAYSCALE);
$sx = imagesx($bimg);
$sy = imagesy($bimg);
// グラデーション用画像を作成
$img = imagecreate($sx, $sy);
imagesavealpha($img, true);
imagealphablending($img, false);
// 半透明グラデーションを作成
$c1 = 0xffff10;
$c2 = 0x3070a0;
for($ly=0;$ly<$sy;$ly++){
$r1 = ($c1 >> 16) & 0xff;
$g1 = ($c1 >> 8 ) & 0xff;
$b1 = $c1 & 0xff;
$r2 = ($c2 >> 16) & 0xff;
$g2 = ($c2 >> 8 ) & 0xff;
$b2 = $c2 & 0xff;
$ro = $r1 + round(($r2 - $r1)/$sy*$ly);
$go = $g1 + round(($g2 - $g1)/$sy*$ly);
$bo = $b1 + round(($b2 - $b1)/$sy*$ly);
for($lx=0;$lx<$sx;$lx++){
imagesetpixel($img, $lx, $ly,
imagecolorresolve($img, $ro, $go, $bo));
}
}

// 重ね合わせ
imagecopymerge($bimg, $img, 0, 0, 0, 0, $sx, $sy, 50);

// ファイル出力
imagepng($bimg, "gd_test4.png");
// 開放
imagedestroy($bimg);
imagedestroy($img);
?>
<img src="gd_test4.png" /><br />
</body>
</html>



元画像


出力画像


動作環境
Apache httpd 2.2.17, PHP5.3.3

Tuesday, December 14, 2010

PHPとGDで半透明グラデーションを作成する

PHPとGDで半透明グラデーションを作成するには、以下のコードを実行します。

<!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>gd_test3</title>
</head>
<body>
<?php
$sx = 200;
$sy = 200;
$img = imagecreatetruecolor($sx, $sy);
imagesavealpha($img, true);
imagealphablending($img, false);
// 半透明グラデーションを作成
$c1 = 0x007799dd;
$c2 = 0x80ffffff;
for($ly=0;$ly<$sy;$ly++){
$a1 = ($c1 >> 24) & 0xff;
$r1 = ($c1 >> 16) & 0xff;
$g1 = ($c1 >> 8 ) & 0xff;
$b1 = $c1 & 0xff;
$a2 = ($c2 >> 24) & 0xff;
$r2 = ($c2 >> 16) & 0xff;
$g2 = ($c2 >> 8 ) & 0xff;
$b2 = $c2 & 0xff;
$ao = $a1 + round(($a2 - $a1)/$sy*$ly);
$ro = $r1 + round(($r2 - $r1)/$sy*$ly);
$go = $g1 + round(($g2 - $g1)/$sy*$ly);
$bo = $b1 + round(($b2 - $b1)/$sy*$ly);
for($lx=0;$lx<$sx;$lx++){
imagesetpixel($img, $lx, $ly,
imagecolorresolvealpha($img, $ro, $go, $bo, $ao));
}
}
// ファイル出力
imagepng($img, "gd_test3.png", 0);
// 開放
imagedestroy($img);
?>
<img src="gd_test3.png" /><br />
</body>
</html>



出力画像


動作環境
Apache httpd 2.2.17, PHP5.3.3

Thursday, December 09, 2010

PHPとGDで画像をグレースケールに変換する

PHPとGDで画像をグレースケールに変換するには、以下のコードを実行します。

<!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>gd_test2</title>
</head>
<body>
<?php
// 入力ファイル名
$fn = "sf.jpg";
// 入力ファイルサイズ取得
$img = imagecreatefromjpeg($fn);
$sx = imagesx($img);
$sy = imagesy($img);
// 出力ファイルサイズ
$ox = 100;
$oy = 100;
// イメージをグレースケールにする
if($img && imagefilter($img, IMG_FILTER_GRAYSCALE)){
// ファイル出力
imagejpeg($img, "gd_test2.jpg", 100);
// 開放
imagedestroy($img);
}
?>
<img src="gd_test2.jpg" /><br />

</body>
</html>



元画像


出力画像


動作環境
Apache httpd 2.2.17, PHP5.3.3

Friday, December 03, 2010

PHPとGDで画像をリサイズする

PHPとGDで画像をリサイズするには、以下のコードを実行します。

<!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>gd_test1</title>
</head>
<body>
<?php
// 入力ファイル名
$fn = "sf.jpg";
// 入力ファイルサイズ取得
$img = imagecreatefromjpeg($fn);
$sx = imagesx($img);
$sy = imagesy($img);
// 出力ファイルサイズ
$ox = 100;
$oy = 100;
// リサイズ
$img2 = imagecreatetruecolor($ox, $oy);
imagecopyresampled($img2, $img, 0, 0, 0, 0, $ox, $oy, $sx, $sy);
// ファイル出力
imagejpeg($img2, "gd_test1.jpg", 100);
// 開放
imagedestroy($img);
?>
<img src="gd_test1.jpg" /><br />

</body>
</html>



元画像


出力画像


動作環境
Apache httpd 2.2.17, PHP5.3.3

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で描画したグラデーションのかかったストライプ

Sunday, July 26, 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>sample1145(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* 画像サイズ */
$sx=200;
$sy=200;

/* 茎の幅 */
$rw = 20+rand(-2,2);

/* 根の位置 */
$rx = 100;
$ry = 200;

/* 繰り返し回数 */
$rt = 3;

/* 初期角度 */
$ia = 15;

/* 差分角度 */
$da = 26;

/* 描画色 */
$dc = "black";

/* 葉の長さ */
$ll = 90;

/* 葉の長さの差分 */
$dl = 10;




$im = new Imagick();
$im->newImage($sx, $sy, "none");
$im->setImageMatte(true);

$ca = $ia;



for($lc=0;$lc<$rt;$lc++){

$idraw = new ImagickDraw();
$idraw->setFillColor($dc);
$idraw->pathStart();
$idraw->pathMoveToAbsolute($rx-$rw/2, $ry);
$idraw->pathCurveToQuadraticBezierAbsolute(
$rx -$rw/2 + $ll/2*cos(pi()*($ca+40)/180),
$ry + $ll/2*sin(pi()*($ca+40)/180)*-1,
$rx -$rw/2 + $ll*cos(pi()*($ca)/180)+rand(0,10),
$ry + $ll*sin(pi()*($ca)/180)*-1+rand(0,10)
);
$idraw->pathCurveToQuadraticBezierAbsolute(
$rx +$rw/2 + $ll/2*cos(pi()*($ca+30)/180),
$ry + $ll/2*sin(pi()*($ca+30)/180)*-1,
$rx+$rw/2,
$ry
);
$idraw->pathClose();
$im->drawImage($idraw);
$idraw->destroy();


$idraw = new ImagickDraw();
$idraw->setFillColor($dc);
$idraw->pathStart();
$idraw->pathMoveToAbsolute($rx+$rw/2, $ry);
$idraw->pathCurveToQuadraticBezierAbsolute(
$rx +$rw/2 + $ll/2*cos(pi()*($ca+40)/180)*-1,
$ry + $ll/2*sin(pi()*($ca+40)/180)*-1,
$rx +$rw/2 + $ll*cos(pi()*($ca)/180)*-1+rand(0,10),
$ry + $ll*sin(pi()*($ca)/180)*-1+rand(0,10)
);
$idraw->pathCurveToQuadraticBezierAbsolute(
$rx -$rw/2 + $ll/2*cos(pi()*($ca+30)/180)*-1,
$ry + $ll/2*sin(pi()*($ca+30)/180)*-1,
$rx-$rw/2,
$ry
);
$idraw->pathClose();
$im->drawImage($idraw);
$idraw->destroy();



$rw -= 4;
$ca += $da+rand(-4,4);
$ll -= $dl;

}


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

</body>
</html>


出力画像(sample1145a.png)
Imagickで描画した草の影絵

Saturday, July 25, 2009

ImageMagickとPHPで画像をタイル配置にして台形変形する

ImageMagickとPHPで画像をタイル配置にして台形変形するには、以下のコードを実行します。


<!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>sample1138(ImageMagick6.5.2)</title>
</head>
<body>
<?php
$im = new Imagick('sf.jpg');
$iw = $im->getImageWidth();
$ih = $im->getImageHeight();
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TILE);

$points = array(
0,0, $iw/8,$ih/8,
$iw,0, $iw*3/4,$ih/4,
$iw,$ih, $iw*3/4, $ih*3/4,
0,$ih, $iw/8,$ih*7/8
);
// PerspectiveDistortion in distort.h
$im->distortImage(4, $points, false);
$im->writeImage('sample1138a.png');
$im->destroy();
?>
<img src="sample1138a.png" /><br />

</body>
</html>


元画像(sf.jpg)


出力画像(sample1138a.png)
Imagickでタイル配置して台形変形させた画像

Thursday, July 23, 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>sample1137(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* 画像サイズ */
$sx=200;
$sy=200;
/* 放射ストライプの中心 */
$cx=100;
$cy=100;
/* ストライプ数 */
$rays=24;
$sr=360/$rays;
/* 背景色 */
$bc="white";
/* ストライプ色 */
$sc="#99bbff";

$im = new Imagick();
$im->newImage($sx, $sy, $bc);
$idraw = new ImagickDraw();
$idraw->setFillColor($sc);
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);
$im->swirlImage(120);
$im->writeImage('sample1137a.png');
$idraw->destroy();
$im->destroy();
?>
<img src="sample1137a.png" /><br />

</body>
</html>


出力画像(sample1137a.png)
Imagickで作成したひねりのあるストライプ画像

Tuesday, July 21, 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>sample1136(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* 画像サイズ */
$sx = 200;
$sy = 50;

$im = new Imagick("tree1.jpg");

$im->blackThresholdImage('#808080');
$im->whiteThresholdImage('#808080');
$im->negateImage(true);
$im->setImageMatte(true);
$im = $im->fxImage("r", Imagick::CHANNEL_ALPHA);

$im2 = new Imagick();
$im2->newImage($im->getImageWidth(),
$im->getImageHeight(), "#303030");
$im2->sketchImage(8,0,135);

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

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

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

</body>
</html>


元画像(tree1.jpg)


出力画像(sample1136a.png)
Imagickで影絵スケッチ風に変換した画像

Sunday, July 19, 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>sample1135(ImageMagick6.5.2)</title>
</head>
<body>
<?php
/* グリッドサイズ */
$xs = 20;
$ys = 20;

$im = new Imagick("sf.jpg");
$iw = $im->getImageWidth();
$ih = $im->getImageWidth();

$idraw = new ImagickDraw();
/* 線の色 */
$idraw->setStrokeColor('#ffffff');
/* 塗りつぶし色 */
$idraw->setFillColor('none');
/* 点線のスタイル */
$idraw->setStrokeDashArray(array(2,2));
for($ly=0;$ly<$ih;$ly+=$ys){
$idraw->line(0,$ly, $iw,$ly);
}
for($lx=0;$lx<$iw;$lx+=$xs){
$idraw->line($lx,0, $lx,$ih);
}
$im->drawImage($idraw);
$im->writeImage('sample1135a.png');

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

</body>
</html>


元画像(sf.jpg)


出力画像(sample1135a.png)
Imagickで画像の上に点線を描画した画像

Friday, July 17, 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>sample1134(ImageMagick6.5.2)</title>
</head>
<body>
<?php
$im = new Imagick("sf2.jpg");
$im->medianFilterImage(2);
$im->quantizeImage(8, Imagick::COLORSPACE_GRAY, 0, FALSE , false);
$im->edgeImage(1);
$im->negateImage(true);
$im->writeImage('sample1134a.png');

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

</body>
</html>


元画像(sf2.jpg)


出力画像(sample1134a.png)
Imagickで等高線のように変換した画像

Wednesday, July 15, 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>sample1133(ImageMagick6.5.2)</title>
</head>
<body>
<?php
$im = new Imagick("sf.jpg");
//$im->setImageColorspace(Imagick::COLORSPACE_GRAY);
$im->medianFilterImage(1.5);
$im->quantizeImage(8, Imagick::COLORSPACE_GRAY, 0, FALSE , false);
$im2 = new Imagick();
$im2->newPseudoImage(1, 2, 'gradient:#803010ff-#ffffffff');
/* 任意の2色に変換 */
$im->addImage($im2);
$im->setImageIndex(0);
$im3 = $im->fxImage("v.p{0,(r+g+b)/3}");
$im3->writeImage('sample1133a.png');

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

</body>
</html>


元画像(sf.jpg)


出力画像(sample1133a.png)
Imagickで変換した抽象的なグラデーション画像

Monday, July 13, 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>sample1132(ImageMagick6.5.2)</title>
</head>
<body>
<?php
$hpad = 20;
$im = new Imagick("sf.jpg");
$iw = $im->getImageWidth();
$ih = $im->getImageWidth();
$im2 = $im->clone();
$im2->cropImage($iw-$hpad*2, $ih, $hpad, 0);
$im2->blurImage(0, 5);
$im3 = new Imagick();
$im3->newPseudoImage($iw-$hpad*2, $ih,
"gradient:#00000010-#00000070");
$im2->compositeImage($im3, Imagick::COMPOSITE_OVER,
0, 0, Imagick::CHANNEL_ALL);

$im->compositeImage($im2, Imagick::COMPOSITE_OVER,
$hpad, 0, Imagick::CHANNEL_ALL);
$im->writeImage('sample1132a.png');

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

</body>
</html>


元画像(sf.jpg)


出力画像(sample1132a.png)
Imagickで背景がぼやける半透明グラデーションを描画した画像

Saturday, July 11, 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>sample1155(ImageMagick6.5.2)</title>
</head>
<body>
<?php
$im = new Imagick("sf2.jpg");
$im->evaluateImage(imagick::EVALUATE_MULTIPLY, 0.5);
$im->modulateImage(130, 20, 100);
$im->medianFilterImage(2);
$im->sepiaToneImage(80);
$im->writeImage('sample1155a.png');

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

</body>
</html>


元画像(sf2.jpg)


出力画像(sample1155a.png)
Imagickでピントがぼけたようなセピア調に変換した画像