gd2.php

  Image 処理



  
<?php
# **********************************************************
# クラス
# **********************************************************
class GD {

	var $im;
	var $type;

# **********************************************************
# コンストラクタ
# **********************************************************
	function GD( ) {
	}

# **********************************************************
# PNG ロード
# **********************************************************
	function LoadPng( $Target ) {
		$this->type = "PNG";
		$this->im = ImageCreateFromPng($Target);
	}
 
# **********************************************************
# JPEG ロード
# **********************************************************
	function LoadJpeg( $Target ) {
		$this->type = "JPEG";
		$this->im = ImageCreateFromJpeg($Target);
	}

# **********************************************************
# 色リソース作成
# **********************************************************
	function CreateColor( $Red, $Green, $Blue ) {
		$ret = ImageColorAllocate (
			$this->im,
			$Red, $Green, $Blue );
		return $ret;
	}

# **********************************************************
# 線幅設定
# **********************************************************
	function SetLineWidth( $Width ) {
		ImageSetThickness( $this->im, $Width );
	}

# **********************************************************
# 直線の描画
# **********************************************************
	function Line( $x1, $y1, $x2, $y2, $Option ) {
		if ( is_array( $Option ) ) {
			ImageSetStyle( $this->im, $Option );
			ImageLine(
				$this->im, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED );
		}
		else {
			ImageLine(
				$this->im, $x1, $y1, $x2, $y2, $Option );
		}
	}

# **********************************************************
# 矩形の描画
# **********************************************************
	function Box( $x, $y, $h, $w, $Color, $Fill=FALSE ) {
		if ( $fill ) {
			imageFilledRectAngle(
				$this->im, $x, $y, $x+$w, $y+$w, $Color );
		}
		else {
			ImageRectAngle(
				$this->im, $x, $y, $x+$w, $y+$w, $Color );
		}
	}

# **********************************************************
# 楕円の描画
# **********************************************************
	function Arc( $x, $y, $h, $w, $Color ) {
		ImageArc( $this->im, $x, $y, $h, $w, 0, 359, $Color );
	}

# **********************************************************
# ブラウザへ出力
# **********************************************************
	function Response( ) {
		switch( $this->type ) {
			case "PNG":
				header('Content-Type: image/png');
				ImagePng( $this->im );
				break;
			case "JPEG":
				header('Content-Type: image/jpeg');
				ImageJpeg( $this->im );
				break;
		}
	}

# **********************************************************
# PNG 保存
# **********************************************************
	function SavePng( $FilePath ) {
		ImagePng( $this->im, $FilePath );
	}

# **********************************************************
# JPEG 保存
# **********************************************************
	function SaveJpeg( $FilePath, $Quality=75 ) {
		ImageJpeg( $this->im, $FilePath, $Quality );
	}

# **********************************************************
# 色リソース開放
# **********************************************************
	function DestroyColor( $Color ) {
		ImageColorDeallocate( $this->im, $Color );
	}

# **********************************************************
# イメージの破棄
# **********************************************************
	function Destroy( ) {
		ImageDestroy ( $this->im );
	}

# **********************************************************
# 伸縮された新しいイメージの作成
# **********************************************************
	function Copy( &$New, $rate ) {
		$w = ImageSx( $this->im );
		$h = ImageSy( $this->im );
		$New = new GD();
		$New->im = ImageCreateTrueColor( $w * $rate, $h * $rate );
		$w2 = ImageSx( $New->im );
		$h2 = ImageSy( $New->im );
		ImageCopyResampled(
			$New->im,
			$this->im,
			0,0,0,0,
			$w2, $h2,
			$w, $h
		);
		$New->type = $this->type;
	}

	function CopyW( &$New, $w_new ) {
		$w = ImageSx( $this->im );
		$rate = $w_new / $w;
		$h = ImageSy( $this->im );
		$New = new GD();
		$New->im = ImageCreateTrueColor( $w_new, $h * $rate );
		$w2 = ImageSx( $New->im );
		$h2 = ImageSy( $New->im );
		ImageCopyResampled(
			$New->im,
			$this->im,
			0,0,0,0,
			$w2, $h2,
			$w, $h
		);
		$New->type = $this->type;
	}

	function CopyH( &$New, $h_new ) {
		$w = ImageSx( $this->im );
		$h = ImageSy( $this->im );
		$rate = $h_new / $h;
		$New = new GD();
		$New->im = ImageCreateTrueColor( $w * $rate, $h_new );
		$w2 = ImageSx( $New->im );
		$h2 = ImageSy( $New->im );
		ImageCopyResampled(
			$New->im,
			$this->im,
			0,0,0,0,
			$w2, $h2,
			$w, $h
		);
		$New->type = $this->type;
	}

	function CopyWH( &$New, $w_new, $h_new ) {
		$w = ImageSx( $this->im );
		$h = ImageSy( $this->im );
		$New = new GD();
		$New->im = ImageCreateTrueColor( $w_new, $h_new );
		$w2 = ImageSx( $New->im );
		$h2 = ImageSy( $New->im );
		ImageCopyResampled(
			$New->im,
			$this->im,
			0,0,0,0,
			$w2, $h2,
			$w, $h
		);
		$New->type = $this->type;
	}
}
?>
  



  処理サンプル



  
<?php
# 新しいオブジェクト
$GD = new GD();

# WEB よりイメージを作成
# Google の短縮url で動作します
$GD->LoadJpeg( "http://goo.gl/CO1XaJ" );

# 赤のパレットを作成
$red = $GD->CreateColor( 255, 0, 0 );

# 線の太さを設定
$GD->SetLineWidth( 3 );

# パラメータの処理
if ( ctype_digit( $_GET['x'] ) ) {
	$x = $_GET['x'];
}
else {
	$x = 135;
}
if ( ctype_digit( $_GET['y'] ) ) {
	$y = $_GET['y'];
}
else {
	$y = 118;
}

# 楕円を描画
$GD->Arc( $x, $y, 60, 35, $red );

# 黒のパレットを作成
$black = $GD->CreateColor( 0, 0, 0 );

# 白のパレットを作成
$white = $GD->CreateColor( 255, 255, 255 );

# 点線のスタイルを作成
$style = array(
	$black,$black,$black,$black,$black,
	$white,$white,$white,$white,$white
);

# 線の太さを設定
$GD->SetLineWidth( 1 );

# スタイルを使用して斜め線を描画
$GD->Line( 20, 30, 200, 100, $style );

# 30%の大きさで新しいオブジェクトを作成
//$GD->Copy( $GD2, 0.3 );
# 幅を100で新しいオブジェクトを作成
//$GD->CopyW( $GD2, 100 );
# 高さを200で新しいオブジェクトを作成
//$GD->CopyH( $GD2, 200 );
# 幅を1000高さを100で新しいオブジェクトを作成
$GD->CopyWH( $GD2, 1000, 100 );

# 縮小画像をファイルとして保存
$GD2->SaveJpeg( "uf3_001.jpg" );

# ブラウザに表示
$GD2->Response( );

# 後処理
$GD->DestroyColor( $red );
$GD->Destroy( );
$GD2->Destroy( );
?>
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ