LoginSignup
4
6

More than 5 years have passed since last update.

phpで動的に透過gif

Last updated at Posted at 2015-11-30

透過gif

透過gifはツールを使えば作れるのだけどphpで動的に作れるかと調べたところ、サンプルもあったので自分なりに確認して忘れないようにメモ。

インストール

debian 系のインストール、centos でも yum でモジュールは一緒かな?(未確認)

# apt-get -y install php5 php5-gd

サンプル

phpサンプル

php ドキュメントのサンプルをほぼそのまま。

gif.php
// 新しい画像のインスタンスを作成します
$im = imagecreatetruecolor(100, 100);

// 背景を白にします
$white = imagecolorallocate($im, 255,255,255);
imagefilledrectangle($im, 0,0,99,99, $white);

// テキスト文字列を画像の上に描画します
imagestring($im, 3, 20, 20, 'GD Library', 0xFFBA00);

// 透明色を指定
imagecolortransparent($im, $white);

// 画像をブラウザに出力します
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);                  

htmlサンプル

gif だけ表示しても透過の具合が分からないので、背景色を付けて呼んでみます。

gif.html
<html>
<head>
</head>
<body>
<table border="1">
<tr style="background-color:#CC0066;">
    <td><img src="/gif.php"></td>
</tr>
<tr style="background-color:#CC0066;">
   <td>sample</td>
</tr>
</table>
</body>
</html>

実際の表示

同じフォルダに置いて、gif.htmlを呼ぶと透過になっていますね。

スクリーンショット 2015-12-01 8.24.18.png

参考サイト

imagecolortransparent

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6