ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
PHP(GD) : 透過画像に指定色の背景を重ねて画像として返す
日時: 2013/03/17 03:55
名前: lightbox



拡張子:
<?
ini_set( 'display_errors', "1" );

header('Content-Type: image/png');
//header('Content-Type: text/html');

if ( $_GET['img'] == '' ) {
	$_GET['img'] = 'a.png';
}
if ( $_GET['c'] == '' ) {
	$_GET['c'] = 'ffffff';
}

$r = hexdec( substr( $_GET['c'], 0, 2 ) );
$g = hexdec( substr( $_GET['c'], 2, 2 ) );
$b = hexdec( substr( $_GET['c'], 4, 2 ) );

$pi = pathinfo($_GET['img']);
$ext = strtolower($pi['extension']);

if ( $ext == 'png' ) {
	$im = @imagecreatefrompng($_GET['img']);
}
if ( $ext == 'gif' ) {
	$im = @imagecreatefromgif($_GET['img']);
}
if ( $ext == 'jpg' || $ext == 'jpeg' ) {
	$im = @imagecreatefromjpeg($_GET['img']);
}

// 失敗
if(!$im) {

	//空の画像
	$im  = imagecreatetruecolor(200, 200);			// 200x200
	$bgc = imagecolorallocate($im, 255, 255, 255);		// 背景色(白)
	$tc  = imagecolorallocate($im, 0, 0, 0);		// 文字色(黒)

	imagefilledrectangle($im, 0, 0, 200, 200, $bgc);	// 背景を塗る

	// メッセージ
	// (5,5) は座標
	imagestring($im, 1, 5, 5, 'Error loading', $tc);

}
else {
	$ww = imagesx( $im );
	$hh = imagesy( $im );

	//空の画像
	$im_base  = imagecreatetruecolor($ww, $hh);			// 200x200
	$bgc = imagecolorallocate($im_base, $r, $g, $b);		// 背景色(白)
	imagefilledrectangle($im_base, 0, 0, $ww, $hh, $bgc);		// 背景を塗る

	imagecopy($im_base, $im, 0, 0, 0, 0, $ww, $hh );
	$im = $im_base;

}

/*
// 縮小は品質が悪いのでボツ
if ( $_GET['w'] != '' ) {
	$nw = $_GET['w']+0;
	$nh = $hh * ( $nw / $ww );
	$thumb = imagecreatetruecolor( $nw, $nh );
	imagecopyresized($thumb, $im, 0, 0, 0, 0, $nw, $nh, $ww, $hh);
	$im = $thumb;
}
*/

// PNG イメージをブラウザに出力する
imagepng($im);
// 画像を破棄する
imagedestroy($im);

?>
メンテナンス


日時: 2013/03/17 03:55
名前: lightbox