<?php
# 新しいオブジェクト
$GD = new GD();
# WEB よりイメージを作成
# 短縮url で動作します
$GD->LoadJpeg( "https://bit.ly/2MCYPI0" );
# 赤のパレットを作成
$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 );
# 指定の大きさで新しいオブジェクトを作成
//@$GD->Copy( $GD2, 0.3 );
//@$GD->CopyW( $GD2, 100 );
//@$GD->CopyH( $GD2, 200 );
@$GD->CopyWH( $GD2, 600, 400 );
# 縮小画像をファイルとして保存
$GD2->SaveJpeg( "uf3_001.jpg" );
# ブラウザに表示
$GD2->Response( );
# 後処理
$GD->DestroyColor( $red );
$GD->Destroy( );
$GD2->Destroy( );
# ***********************
# クラス
# ***********************
class GD {
var $im;
var $type;
# ***********************
# コンストラクタ
# ***********************
function GD( ) {
}
# ***********************
# キャンバス作成
# ***********************
function CreateCanvas( $Width, $Height, $Type="PNG" ) {
$this->type = $Type;
$this->im = imagecreatetruecolor($Width, $Height);
}
# ***********************
# 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, $w, $h, $Color, $fill=FALSE ) {
if ( $fill ) {
imageFilledRectAngle(
$this->im, $x, $y, $x+$w, $y+$h, $Color );
}
else {
ImageRectAngle(
$this->im, $x, $y, $x+$w, $y+$h, $Color );
}
}
# ***********************
# 楕円の描画
# ***********************
function Arc( $x, $y, $w, $h, $Color ) {
ImageArc( $this->im, $x, $y, $w, $h, 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;
}
}