グラデーション画像を作成


サンプル

<?php
	$sc = 'ffffff'; // 開始色
 	$ec = '0000cc'; // 終了色
 
	$w = 200; // 画像幅
	$h = 50; // 画像高さ
 
	$im  = imagecreatetruecolor($w, $h);
	$bgc = imagecolorallocate($im, 255, 255, 255);
 
	$al = gradDiv($sc, $ec, $h);
	$sc = str2hec($sc);
 
	for($i=0; $i<$w; $i++){
		$bgc = imagecolorallocate($im
									, $sc['R'] + $al['R'] * $i  
									, $sc['G'] + $al['G'] * $i 
									, $sc['B'] + $al['B'] * $i );
		imageLine($im, 0, $i, $w, $i, $bgc);
	}
 
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
 
exit;
 
function gradDiv($sc, $ec, $l){
 
  $S = str2hec($sc);
  $E = str2hec($ec);
 
  return array( 'R'=>($E['R'] - $S['R']) / $l
              , 'G'=>($E['G'] - $S['G']) / $l
              , 'B'=>($E['B'] - $S['B']) / $l); 
}
 
function str2hec($col){
  $R = hexdec(substr($col, 0, 2));
  $G = hexdec(substr($col, 2, 2));
  $B = hexdec(substr($col, 4, 2));
 
  return array('R'=>$R, 'G'=>$G, 'B'=>$B);
}
?>
 
 

イメージ



最終更新:2012年08月18日 23:10