使用 PHP 创建 PNG 图像

示例 #1 使用 PHP 创建 PNG 图像

<?php

header
("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);

?>
本示例需要从带有 <img src="button.php?text=text"> 标签的页面调用。 上述 button.php 脚本将 "text" 字符串绘制到一个图像上, 在本例中图像文件为 "images/button1.png", 然后输出绘制后的图像。 当你需要经常修改图像上的文字时, 动态生成图像就可以省去了每次都重新制作图像的麻烦。
To Top