PHP 8.3.4 Released!

imagegd2

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

imagegd2GD2 イメージをブラウザまたはファイルに出力する

説明

imagegd2(
    GdImage $image,
    ?string $file = null,
    int $chunk_size = 128,
    int $mode = IMG_GD2_RAW
): bool

GD2 イメージを、指定した file に出力します。

パラメータ

image

imagecreatetruecolor()のような画像作成関数が返す GdImage オブジェクト。

file

ファイル保存先のパスあるいはオープン中のリソース (この関数が値を戻した後で自動的にクローズされます)。省略したり null を設定したりした場合は、画像ストリームを直接出力します。

chunk_size

チャンクの大きさ。

mode

IMG_GD2_RAW あるいは IMG_GD2_COMPRESSED のいずれかです。デフォルトは IMG_GD2_RAW です。

戻り値

成功した場合に true を、失敗した場合に false を返します。

警告

しかしながら、libgd がイメージの出力に失敗した場合、この関数は true を返します。

変更履歴

バージョン 説明
8.0.3 file は、nullable になりました。
8.0.0 image は、 GdImage クラスのインスタンスを期待するようになりました。 これより前のバージョンでは、有効な gd resource が期待されていました。

例1 GD2 画像の出力

<?php
// 空の画像を作成し、テキストを追加します
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);

// 画像を出力します
imagegd2($im);

// メモリを開放します
imagedestroy($im);
?>

例2 GD2 画像の保存

<?php
// 空の画像を作成し、テキストを追加します
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);

// GD2 画像を保存します
// GD2 画像のファイル形式は .gd2 です。 http://www.libgd.org/GdFileFormats を参照ください
imagegd2($im, 'simple.gd2');

// メモリを開放します
imagedestroy($im);
?>

注意

注意:

通常 GD2 フォーマットはイメージの一部を高速にロードするために 使用されます。GD2 フォーマットは GD2 互換アプリケーションでのみ使用可能であることに注意してください。

警告

GD と GD2 画像フォーマットは libgd が作った独自仕様の画像フォーマットです。これらは obsolete と見なされているため、開発とテストの用途にだけ使うべきです。

参考

  • imagegd() - GD イメージをブラウザまたはファイルに出力する
add a note

User Contributed Notes 2 notes

up
2
Nick
12 years ago
You can use this function in combination with imagecreatefromstring() to clone the gd resource with minimum fuss (no writing to tmp file):

<?php
function cloneGd($gd)
{
ob_start();
imagegd2($gd);
return
imagecreatefromstring(ob_get_clean());
}
?>
up
0
mark at teckis dot com
20 years ago
yes, the gd2 file format does improve the speed of image creations as the data-setup is designed to be native for the GD function - ie, the image doesn't have to be converted to a usable format prior to processing.

you may also note that the newer gd2 format creates much smaller size files than the older imagegd function, certainly for images involving chunks of single colours anyway. you'll probably find this function most useful for saving overlay images or background images used in larger image creation scripts.

to read a ping or jpeg image (.png / .jpg) and save a .gd2 version to server...

$img = $_GET['img'];
if(file_exists($img))
{
$dim = getimagesize($img);
$cr = ($dim[2] < 4) ? ($dim[2] < 3) ? ($dim[2] < 2) ? NULL : imagecreatefromjpeg($img) : imagecreatefrompng($img) : Null;
if($cr !== NULL)
{
imagegd2($cr,substr($img,0,strrpos($img,'.')).'.gd2');
}
}

should save a copy with the same filename and directory using extension .gd2 - which can then be nicely and swiftly read using either imagecreatefromgd2 or imagecreatefromgd2part
To Top