文字

使用 PHP 创建 PNG 图像

Example #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", 然后输出绘制后的图像。 当你需要经常修改图像上的文字时, 动态生成图像就可以省去了每次都重新制作图像的麻烦。

用户评论:

[#1] Jeff [2015-03-30 21:59:39]

Keep in mind that transparency is not preserved when doing what is shown in Example #1 to a PNG with transparency.

上一篇: 下一篇: