PHP 日本研討會 2024

imagestring

(PHP 4, PHP 5, PHP 7, PHP 8)

imagestring水平繪製字串

說明

imagestring(
    GdImage $image,
    GdFont|int $font,
    int $x,
    int $y,
    string $string,
    int $color
): bool

在給定的座標繪製 string

參數

image

GdImage 物件,由影像建立函式之一傳回,例如 imagecreatetruecolor()

font

可以是 1、2、3、4、5,表示 latin2 編碼的內建字型(數字較高表示字型較大),或是 GdFont 實例,由 imageloadfont() 傳回。

x

左上角的 x 座標。

y

左上角的 y 座標。

string

要寫入的字串。

color

使用 imagecolorallocate() 建立的顏色識別符。

傳回值

成功時傳回 true,失敗時傳回 false

變更記錄

版本 說明
8.1.0 font 參數現在同時接受 GdFont 實例和 int;先前只接受 int
8.0.0 image 現在期望 GdImage 實例;先前,期望有效的 gd resource

範例

範例 1 imagestring() 範例

<?php
// 建立一個 100*30 的影像
$im = imagecreate(100, 30);

// 白色背景和藍色文字
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// 在左上角寫入字串
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);

// 輸出影像
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

以上範例的輸出結果類似於

Output of example : imagestring()

參見

新增筆記

使用者貢獻的筆記 30 則筆記

keksnicoh at googlemail dot com
16 年前
使用 imagestring 的一些樂趣

這個函式是太多時間的產物..
它開啟一個影像,並建立一個新影像,其中一個字母取代一個像素。

<?php
error_reporting
(E_ALL);
/**
* 產生一個以字元而非像素組成的圖片
*
* @param string $url 檔案路徑或網址
* @param string $chars 應該取代像素的字元
* @param int $shrpns 清晰度 (2 = 每隔一個像素, 1 = 每個像素 ...)
* @param int $size
* @param int $weight 字體粗細/大小
* @return sesource
* @author Nicolas 'KeksNicoh' Heimann <www.salamipla.net>
* @date 02nov08
*/
function pixelfuck($url, $chars='ewk34543§G§$§$Tg34g4g', $shrpns=1, $size=4,$weight=2)
{
list(
$w, $h, $type) = getimagesize($url);
$resource = imagecreatefromstring(file_get_contents($url));
$img = imagecreatetruecolor($w*$size,$h*$size);

$cc = strlen($chars);
for(
$y=0;$y <$h;$y+=$shrpns)
for(
$x=0;$x <$w;$x+=$shrpns)
imagestring($img,$weight,$x*$size,$y*$size, $chars{@++$p%$cc}, imagecolorat($resource, $x, $y));
return
$img;
}

$url = 'http://upload.wikimedia.org/wikipedia/commons/b/be/Manga_Icon.png';
$text = 'I-dont-like-manga-...-Why-do-they-have-such-big-eyes? Strange-...-WHAT-WANT-YOU-DO?';

Header('Content-Type: image/png');
imagepng(pixelfuck($url, $text, 1, 6));
?>

玩得開心 :)
Booteille
9 年前
這是一個具有類似 imagestring() 宣告的函式,但它可以處理空白字元(它會建立新行和 4 個空格來代替 \n 和 \t)以及圖片的大小限制

<?php

/**
* @author Booteille
*
* @param resource $image
* @param int $font
* @param int $x
* @param int $y
* @param string $string
* @param int $color
*/
function whitespaces_imagestring($image, $font, $x, $y, $string, $color) {
$font_height = imagefontheight($font);
$font_width = imagefontwidth($font);
$image_height = imagesy($image);
$image_width = imagesx($image);
$max_characters = (int) ($image_width - $x) / $font_width ;
$next_offset_y = $y;

for(
$i = 0, $exploded_string = explode("\n", $string), $i_count = count($exploded_string); $i < $i_count; $i++) {
$exploded_wrapped_string = explode("\n", wordwrap(str_replace("\t", " ", $exploded_string[$i]), $max_characters, "\n"));
$j_count = count($exploded_wrapped_string);
for(
$j = 0; $j < $j_count; $j++) {
imagestring($image, $font, $x, $next_offset_y, $exploded_wrapped_string[$j], $color);
$next_offset_y += $font_height;

if(
$next_offset_y >= $image_height - $y) {
return;
}
}
}
}

?>
jordanslost at gmail
15 年前
這是我做的一小段程式碼,用於在您僅限於使用 imagestring() 時,從右到左寫入圖片

<?php

$pageview_letters
= preg_split('//', $string, -1 ); // 形式為原始的字母陣列。
$minus = 6; // 字母間距(以像素為單位)
$first = true; // 是否已開始字串
$x = 375; // imagestring 的 X 位置
$y = 23; // imagestring 的 Y 位置
$letters = array(); // 初始化字母陣列。

foreach ( $pageview_letters as $letter ) {

$letters[] = $letter;

}

$letters = array_reverse( $letters );

foreach (
$letters as $letter ) {

if (
$first ) {

imagestring( $image, 2, $x, $y, $letter, $light_blue );
$first = false;

} else {

$x = ( $x - $minus );
imagestring( $image, 2, $x, $y, $letter, $light_blue );

}

}
?>
eviloverlord+php at gmail dot com
16 年前
將字串(例如電子郵件地址)轉換為透明圖片的簡單腳本。

用法
<img src="stringtoimg.php?string=<?= urlencode(base64_encode($email)) ?>">

從垃圾郵件機器人的角度來看,它們會看到
<img src="stringtoimg.php?string=ZpbXZG92ZXJsb3JkQGdtYWlsLmNvbQ%3D%3D">

可選參數
font_size:1 到 5,預設為 3
R/G/B:字體顏色,以十六進位表示。

用法
<img src="stringtoimg.php?string=<?= urlencode(base64_encode($email)) ?>&font_size=4&R=FF&G=FF&B=00">

<?php
/*
檔案名稱: stringtoimg.php

參數:
string: 要列印的字串
font_size (可選): 字體大小,從 1 到 5
R/G/B (可選): 字體 RGB 顏色,以十六進位表示
*/

header ("Content-type: image/png");

// 取得字串資訊
$font_size = isset($_GET['font_size']) ? $_GET['font_size'] : 3;
$string = urldecode(base64_decode($_GET['string']));

// 取得字串大小
$width = imagefontwidth($font_size) * strlen($string);
$height = imagefontheight($font_size);

// 建立影像
$img = @imagecreatetruecolor($width, $height)
or die(
"無法初始化新的 GD 影像串流");

// 使其透明
imagesavealpha($img, true);
$trans_colour = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $trans_colour);

// 取得文字顏色
$text_color = isset($_GET['R'], $_GET['G'], $_GET['B']) ?
imagecolorallocate($img, hexdec($_GET['R']), hexdec($_GET['G']), hexdec($_GET['B'])) :
imagecolorallocate($img, 0, 0, 0);

// 繪製字串
imagestring($img, $font_size, 0, 0, $string, $text_color);

// 輸出影像
imagepng($img);
imagedestroy($img);
?>
gannon (在) portablesofdoom (點) org
17 年前
我比較喜歡這個,而不是 "tjpoe at cableaz dot com" 的用於將文字換行以適應寬度(根據需要自動調整高度)的函式,因為它不只做每行一個單字。

function make_wrapped_txt($txt, $color=000000, $space=4, $font=4, $w=300) {
if (strlen($color) != 6) $color = 000000;
$int = hexdec($color);
$h = imagefontheight($font);
$fw = imagefontwidth($font);
$txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
$lines = count($txt);
$im = imagecreate($w, (($h * $lines) + ($lines * $space)));
$bg = imagecolorallocate($im, 255, 255, 255);
$color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
$y = 0;
foreach ($txt as $text) {
$x = (($w - ($fw * strlen($text))) / 2);
imagestring($im, $font, $x, $y, $text, $color);
$y += ($h + $space);
}
header('Content-type: image/jpeg');
die(imagejpeg($im));
}
Piotr dot Sulecki 在 traxelektronik dot pl
18 年前
內建字體以前使用 latin-2 (iso8859-2) 編碼。一段時間以來,它們使用 latin-1 (iso8859-1) 編碼。完全沒有辦法更改編碼。如果您需要使用任何其他編碼,則必須使用 TrueType 字體。
deejay_world 在 yahoo dot com
22 年前
寬度 ImageString,您繪製的字串不會自動以影像邊緣換行。您可以使用此函式自動換行它們

function ImageStringWrap($image, $font, $x, $y, $text, $color, $maxwidth)
{
$fontwidth = ImageFontWidth($font);
$fontheight = ImageFontHeight($font);

if ($maxwidth != NULL) {
$maxcharsperline = floor($maxwidth / $fontwidth);
$text = wordwrap($text, $maxcharsperline, "\n", 1);
}

$lines = explode("\n", $text);
while (list($numl, $line) = each($lines)) {
ImageString($image, $font, $x, $y, $line, $color);
$y += $fontheight;
}
}

因此,特別是,如果您想要使用影像邊緣換行文字,您可以執行
ImageStringWrap($img, $font, 0, $y, $text, $color, ImageSX($img) );
aly 在 slo-igre dot net
19 年前
"tjpoe at cableaz dot com" 的函式 ImageStringWrap 中有一個錯誤。代替

else
$string = $text;

應該是

else
$string = array($text);

以便函式適用於只有一個單字的字串。否則它工作得很好,謝謝。
sk89q
16 年前
建立文字方塊。具有水平和垂直對齊、方塊參數和自訂行距。我實際上在 2003 年將其提交給了手冊,但在一年左右後消失了(不確定原因)。它又回來了。

<?php
define
("ALIGN_LEFT", "left");
define("ALIGN_CENTER", "center");
define("ALIGN_RIGHT", "right");
define("VALIGN_TOP", "top");
define("VALIGN_MIDDLE", "middle");
define("VALIGN_BOTTOM", "bottom");

function
imagestringbox(&$image, $font, $left, $top, $right, $bottom, $align, $valign, $leading, $text, $color)
{
// 取得文字框大小
$height = $bottom - $top;
$width = $right - $left;

// 將文字斷行,並放入陣列
$lines = wordwrap($text, floor($width / imagefontwidth($font)), "\n", true);
$lines = explode("\n", $lines);

// 其他重要數值
$line_height = imagefontheight($font) + $leading;
$line_count = floor($height / $line_height);
$line_count = ($line_count > count($lines)) ? (count($lines)) : ($line_count);

// 迴圈處理每一行
for ($i = 0; $i < $line_count; $i++)
{
// 垂直對齊
switch($valign)
{
case
VALIGN_TOP: // 頂部
$y = $top + ($i * $line_height);
break;
case
VALIGN_MIDDLE: // 中間
$y = $top + (($height - ($line_count * $line_height)) / 2) + ($i * $line_height);
break;
case
VALIGN_BOTTOM: // 底部
$y = ($top + $height) - ($line_count * $line_height) + ($i * $line_height);
break;
default:
return
false;
}

// 水平對齊
$line_width = strlen($lines[$i]) * imagefontwidth($font);
switch(
$align)
{
case
ALIGN_LEFT: // 左對齊
$x = $left;
break;
case
ALIGN_CENTER: // 置中對齊
$x = $left + (($width - $line_width) / 2);
break;
case
ALIGN_RIGHT: // 右對齊
$x = $left + ($width - $line_width);
break;
default:
return
false;
}

// 繪製
imagestring($image, $font, $x, $y, $lines[$i], $color);
}

return
true;
}
?>
wheberson dot com dot br at gmail dot com
5 年前
// 將電子郵件轉換為圖片 (png)
function convertEmailToImg ($aValue, $aRed, $aGreen, $aBlue, $aAlphaF=0, $aAlphaB=127, $aFontSize=4)

{
$img= imagecreatetruecolor (imagefontwidth ($aFontSize) * strlen ($aValue), imagefontheight ($aFontSize));
imagesavealpha ($img, true);
imagefill ($img, 0, 0, imagecolorallocatealpha ($img, 0, 0, 0, $aAlphaB));
imagestring ($img, $aFontSize, 0, 0, $aValue, imagecolorallocatealpha ($img, $aRed, $aGreen, $aBlue, $aAlphaF));
ob_start ();
imagepng ($img);
imagedestroy ($img);
return base64_encode (ob_get_clean ());
}

// 範例
$imgString= convertEmailToImg ('contact@example.com', 0, 0, 255, 0, 127, 4);
jlamer
17 年前
// 使用範例...

// 這是一個簡單的函式,用於將文字輸出到圖片
// 該文字會置中 (盡可能以目測方式做到)
// 並換行
// 請記住,所有尺寸都是猜測的
// 不會根據空間裁剪 (僅根據字元數)
// 或變更文字顏色,但這並非此函式的目的...
function imageCenterString( $imgw, $imgh,
$image_text = '', $text_size=5 )
{
$im = imagecreate( $imgw, $imgh );

// 白色背景和藍色文字
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);

$t_h = $t_w = $t_x = $t_y = 0;
$base_w =9; $base_h = 16;
$m = 0.88;
switch ( $text_size )
{
case 1: $t_w = $base_w*pow(($m*.98),4);
$t_h = $base_h*pow(($m*.98),4);
break;
case 2: $t_w = $base_w*pow($m,3);
$t_h = $base_h*pow($m,3);
break;
case 3: $t_w = $base_w*pow($m,2);
$t_h = $base_h*pow($m,2);
break;
case 4: $t_w = $base_w*$m;
$t_h = $base_h*$m;
break;
case 5: $t_w = $base_w;
$t_h = $base_h;
break;
default
if ( $text_size >= 5 ) // 設定為 5
{ $t_w = $base_w; $t_h = $base_h; }
if ( $text_size < 5 ) // 設定為 1
{
$t_w = $base_w*pow(($m*.98),4);
$t_h = $base_h*pow(($m*.98),4);
}
break;
}

$text_array = array();

$max = floor($imgw/$t_w);

for( $i=0; strlen($image_text) > 0; $i += $max)
{
array_push($text_array, substr($image_text,0,$max));
if ( strlen($image_text) >= $max )
{ $image_text = substr($image_text,$max); }
else
{ $image_text = ''; }
}

$t_y = ($imgh/2) - ($t_h*count($text_array)/2);

foreach ( $text_array as $text )
{
$t_x = ($imgw/2)-($t_w*strlen($text)/2);
imagestring($im, $text_size, $t_x, $t_y,
$text, $textcolor);
$t_y += $t_h;
}

// 輸出圖片
header("Content-type: image/gpeg");
imagejpeg($im);
}
cesargus at yahoo dot com
20 年前
// 簡單的 Hello World

<?
header ("Content-type: image/png");

$img_handle = ImageCreate (200, 20) or die ("無法建立圖片");
$back_color = ImageColorAllocate ($img_handle, 0, 10, 10);
$txt_color = ImageColorAllocate ($img_handle, 235, 235, 51);
ImageString ($img_handle, 10, 25, 5, "Hello world!", $txt_color);
ImagePng ($img_handle);
?>
mustafa at hafunny dot info
16 年前
如果您在處理中歐文字時遇到任何問題,例如:ľščťžýáíéúäňôď,我嘗試使用 iconv() 函式解決這個問題。

<?php
// 建立範例圖片
$im = imagecreate(200, 20);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
$text = "ľščťžýáíéúäňôď...";

// 簡單轉換字串
$string = iconv("Windows-1250", "Latin2", $text);

// 在左上角寫入轉換後的字串
imagestring($im, 4, 0, 0, $string, $textcolor);

// 輸出圖片
header("Content-type: image/png");
imagepng($im);
?>
iibm at free dot fr
17 年前
我對 (相當有用的) `imagestringcutted` 函式做了一些小修改 (當 align=center 時,如果 x1!=0,對我來說無法正常運作),因此請將最後一行替換為

<?php
[...]
else
imagestring($img,$font,$x1+($x2-$x1)/ 2 - strlen($text) * $fontwidth / 2,$y,$text,$color);
}
?>
webmaster at acdrifter dot com
19 年前
如果您想要將文字置中,請使用以下函式;我無法保證完美...

function imagecenteredstring ( &$img, $font, $xMin, $xMax, $y, $str, $col ) {
$textWidth = imagefontwidth( $font ) * strlen( $str );
$xLoc = ( $xMax - $xMin - $textWidth ) / 2 + $xMin + $font;
imagestring( $img, $font, $xLoc, $y, $str, $col );
}
php dot net at mvoncken dot nl
21 年前
一個簡單的範例
讓單行文字符合圖片大小。

<?php
header
("Content-type: image/png");
$string = "spam@mvoncken.nl";
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);

$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 255, 255, 255); //白色背景
$text_color = imagecolorallocate ($im, 0, 0,0);//黑色文字
imagestring ($im, $font, 0, 0, $string, $text_color);
imagepng ($im);
?>

我使用類似的方法來保護訪客免受垃圾郵件侵擾(將使用者 ID 作為 URL 參數傳遞給此 PHP)
mike at mike-griffiths dot co dot uk
15 年前
我使用 imagechar 函式建立影像字串,建立了一個替代方案。以下函式用於建立與文字字串高度和寬度相同的影像。它在我的網站上用於遮蔽使用者的電子郵件地址。

<?PHP

// 以某種方式設定您的字串
$string = 'your@example.com';

// 設定字型大小
$font_size = 4;

// 根據字串寬度建立影像寬度
$width = imagefontwidth($font_size)*strlen($string);
// 設定高度為字型高度
$height = imagefontheight($font_size);
// 建立影像調色盤
$img = imagecreate($width,$height);
// 灰色背景
$bg = imagecolorallocate($img, 25, 25, 25);
// 白色字型顏色
$color = imagecolorallocate($img, 255, 255, 255);
// 字串長度
$len = strlen($string);
// 字元的 Y 座標,X 改變,Y 是靜態的
$ypos = 0;
// 迴圈遍歷字串
for($i=0;$i<$len;$i++){
// 字元水平位置
$xpos = $i * imagefontwidth($font_size);
// 繪製字元
imagechar($img, $font_size, $xpos, $ypos, $string, $color);
// 從字串移除字元
$string = substr($string, 1);

}
// 傳回影像
header("Content-Type: image/gif");
imagegif($img);
// 移除影像
imagedestroy($img);

?>
Epidemiah
16 年前
這是一個簡單的函式,可以在圖片中間寫入字串。

<?php

function imageCenterString(&$img, $font, $text, $color)
{
if(
$font < 0 || $font > 5){ $font = 0; }
$num = array(array(4.6, 6),
array(
4.6, 6),
array(
5.6, 12),
array(
6.5, 12),
array(
7.6, 16),
array(
8.5, 16));
$width = ceil(strlen($text) * $num[$font][0]);
$x = imagesx($img) - $width - 8;
$y = Imagesy($img) - ($num[$font][1] + 2);
imagestring($img, $font, $x/2, $y/2, $text, $color);
}

?>
god at in-heaven dot org
18 年前
這是一個簡單的函式,用於建立對齊的字串,並將其剪裁以符合 $x1 和 $x2 之間的空間
<?php
function imagestringcutted($img,$font,$y,$x1,$x2,$text,$color,$align="center") {
$fontwidth = imagefontwidth($font);
$fullwidth = strlen($text) * $fontwidth;
$maxwidth = $x2-$x1;
$targetwidth = $fullwidth-(4*$fontwidth);
if(
$fullwidth > $maxwidth) {
for(
$i = 0; $i < strlen($text) AND ((strlen($text)-($i-4))*$fontwidth) > $targetwidth ;$i++) { }
$text = substr($text,0,(strlen($text)-$i)-4)."...";
}
if(
$align == "left") imagestring($img,$font,$x1,$y,$text,$color);
elseif(
$align == "right") imagestring($img,$font,$x2 - ((strlen($text) * $fontwidth)),$y,$text,$color);
else
imagestring($img,$font,($x2-$x1)/ 2 - strlen($text) * $fontwidth / 2,$y,$text,$color);
}
?>
用法
<?php
imagestringcutted
($img,$font,$y,$x1,$x2,$text,$color,$align);
?>
將會建立一個字串 $text,如果字串太長以至於無法在 $x1 和 $x2 之間顯示,則會被截斷。此字串會使用 $font 字型和 $color 顏色繪製在 $img 圖像上,高度為 $y,並根據 $align 參數對齊。
希望這對某些人有幫助。
抱歉我的英文不好。
shadikka at gmail dot com
19 年前
我修改的版本是將字串置中,它會減少字型大小(因為我注意到數字越小,字型就越小)直到字型大小為 1,如果字串仍然無法符合,就會放棄。

<?php
function imagestringcentered ($img,$font,$cy,$text,$color) {
while (
strlen($text) * imagefontwidth($font) > imagesx($img)) {
if (
$font > 1) { $font--; }
else { break; }
}
imagestring($img,$font,imagesx($img) / 2 - strlen($text) * imagefontwidth($font) / 2,$cy,$text,$color);
}
?>
bob dot brown at opus dot co dot nz
22 年前
如果您發現圖像字串的結尾出現兩個看起來像 Y 和倒 L 的字符,它們很可能代表 CR/LF。嘗試在輸出之前使用 trim() 修剪字串。(我非常確定這是一個錯誤 <g>)
Abubaker dot shamlan at gmail dot com
16 年前
這是一個基於 imagestring 的函數,但它會在圖像中心產生文字,我希望這會有幫助 :D

<?php
function ImageStringCenter($image_resource, $font_size, $line_number, $total_lines, $text, $color ) {

$center_x = ceil( ( imagesx($image_resource) - ( ImageFontWidth($font_size) * strlen($text) ) ) / 2 );

$center_y = ceil( ( ( imagesy($image_resource) - ( ImageFontHeight($font_size) * $total_lines ) ) / 2) + ( ($line_number-1) * ImageFontHeight($font_size) ) );

ImageString($image_resource, $font_size, $center_x, $center_y, $text, $color );

}
?>
tjpoe at cableaz dot com
19 年前
我修改了置中函數並建立了這個,它會將每個單字置於自己的行上。您可以使用 $valign 變數調整間距。目前如果文字對於圖像來說太大,則沒有任何實作。字串會以空格符號來分詞,但顯然可以更改。

function ImageStringWrap($image, $font, $text, $color)
{
$fontwidth = ImageFontWidth($font);
$fontheight = ImageFontHeight($font);
$words= str_word_count($text);
if ($words > 1){
$string=array(strtok($text,' '));
for ($i = 1 ; $i <= $words ; $i++){
$string=array_merge($string,array($i=>strtok(' ')));
}
}
else
$string=$text;
$vspace=4;
$y=((imagesy($image)-($fontheight*$words)-($words*$vspace))/2);
foreach($string as $st){
$x=((imagesx($image)-($fontwidth * strlen($st)))/2);
ImageString($image,$font,$x,$y,$st,$color);
$y+=($fontheight+$vspace);
}
}
希望這有幫助
brooks dot boyd at gmail dot com
20 年前
將字串繪製成圖像是一種方便的方法,可以偽裝電子郵件地址,使垃圾郵件嗅探器無法輕易獲取它。使用電子郵件建立動態圖像的唯一問題是,要顯示的電子郵件必須透過查詢字串傳遞,以使靜態 HTML 可以使用它。因此,必須稍微加密電子郵件,以免破壞不直接鍵入電子郵件地址的目的。我編寫了以下腳本來做到這一點

將以下內容另存為 email.php
<?php
if ($_GET['addr'] != "") {
$msg = $_GET['addr'];
$msg = preg_replace("/\[dot]/",".",$msg);
$msg = preg_replace("/\[at]/","@",$msg);
$final = "";
for (
$i=0; $i<=strlen($msg); $i++) {
$final .= substr($msg, strlen($msg)-$i, 1);
}
$msg = $final;

$char_width = 8;
$char_height = 17;
$padding = 3;
$width = $padding*2+strlen($msg)*$char_width;
$height = +$padding*2+$char_height;
$im = imagecreatetruecolor($width,$height);
imagealphablending($im, FALSE);
imagesavealpha($im, TRUE);
$bg = imagecolorallocatealpha($im, 255, 255, 0, 100);
$text = imagecolorallocatealpha($im, 0, 0, 0, 0);
imagefilledrectangle ($im, 0, 0, $width, $height, $bg); # Make transparent
imagestring($im, 4, $padding, $padding, $msg, $text);
} else {
$im = imagecreatetruecolor(1,1);
imagealphablending($im, FALSE);
imagesavealpha($im, TRUE);
$bg = imagecolorallocatealpha($im, 255, 0, 0, 125);
imagefilledrectangle ($im, 0, 0, 1, 1, $bg); # Make transparent
}
header('Content-type: image/jpg');
imagepng($im);
imagedestroy($im);

?>

如果腳本在沒有電子郵件地址的情況下被呼叫,它會輸出一個 2x2 的透明圖像。

要呼叫腳本產生電子郵件 "user@home.com",HTML 標籤會是

<img src="email.php?addr=moc[dot]emoh[at]resu">

為了「加密」要傳遞給腳本的電子郵件地址,請將地址反向寫入,並將「.」替換為「[dot]」,將「@」替換為「[at]」。這不是最堅不可摧的保護,但它可以阻止大多數隨意的電子郵件嗅探器。
aholmes84 at hotmail dot com
22 年前
設定字體時,任何小於 1 的整數都會預設為 1,而任何大於 5 的整數都會預設為 5。
rush at 507magazine dot com
18 年前
哈囉,我注意到如果你放入 rand(3,5),它會為圖像上的每個字元放入隨機大小的字體。這在為反垃圾郵件表單驗證編寫驗證碼時非常有用。
julien / at / theoconcept.com
18 年前
如果您正在尋找一種為表單驗證產生「驗證碼」圖像的方法(以驗證它不是機器人),請查看此處:http://blog.theoconcept.com/static/distortion/

它會給出一個帶有參數字串的動畫圖像,並帶有扭曲效果,這裡有一個範例
http://blog.theoconcept.com/static/distortion/distortion.php

(*) 您需要 GD + Freetype 支援
(**) 您需要在機器上安裝 ImageMagick
jurgen dot vanoosterwijck at pandora dot be
19 年前
根據先前的範例,以下是如何水平和垂直置中字串的方法...

<?php
function imagestringcentered ($img,$font,$text,$color) {
while (
strlen($text) * imagefontwidth($font) > imagesx($img)) {
if (
$font > 1) { $font--; }
else { break; }
}
$cy = (imagesy($img)/2) - (imagefontwidth($font)/2);
imagestring($img,$font,imagesx($img) / 2 - strlen($text) * imagefontwidth($font) / 2,$cy,$text,$color);
}
?>
eric dot brison at anakeen dot com
15 年前
與上面的函數相同,但它可以顯示多行字串。
<?php
function sendimagetext($text) {
// 設定字體大小
$font_size = 4;

$ts=explode("\n",$text);
$width=0;
foreach (
$ts as $k=>$string) { //計算寬度
$width=max($width,strlen($string));
}

// 建立圖片寬度,取決於字串寬度
$width = imagefontwidth($font_size)*$width;
// 設定高度為字體高度
$height = imagefontheight($font_size)*count($ts);
$el=imagefontheight($font_size);
$em=imagefontwidth($font_size);
// 建立影像調色盤
$img = imagecreatetruecolor($width,$height);
// 深紅色背景
$bg = imagecolorallocate($img, 0xAA, 0x00, 0x00);
imagefilledrectangle($img, 0, 0,$width ,$height , $bg);
// 白色字體顏色
$color = imagecolorallocate($img, 255, 255, 255);

foreach (
$ts as $k=>$string) {
// 字串長度
$len = strlen($string);
// 字元的 Y 座標,X 會變動,Y 是靜態的
$ypos = 0;
// 迴圈遍歷字串
for($i=0;$i<$len;$i++){
// 字元的水平位置
$xpos = $i * $em;
$ypos = $k * $el;
// 繪製字元
imagechar($img, $font_size, $xpos, $ypos, $string, $color);
// 從字串移除字元
$string = substr($string, 1);
}
}
// 回傳圖片
header("Content-Type: image/png");
imagepng($img);
// 移除圖片
imagedestroy($img);
}
?>
bpgordon at gmail dot com
19 年前
這段程式碼會產生一個 PNG 圖片,內容為查詢字串中的文字。它會自動調整圖片寬度以符合字串長度。
使用方式: http://yoursite.com/text.php?abcdefg+hijk

使用 + 號在圖片中產生空格。可以使用脫字符號 (^) 來跳脫 + 號。大多數其他符號在查詢字串中都能正常運作,例如 ? 號。

<?php
header
("Content-type: image/png");
$string = $_ENV["QUERY_STRING"];
$md5 = md5($string); // 確保我們不會將有效的文字轉換為 + 號
$string = str_replace("^+", $md5, $string); // 將 ^+ 替換成長而不自然的字串
$string = str_replace("+", " ", $string); // 將 + 號替換為空格
$string = str_replace($md5, "+", $string); // 將長而不自然的字串替換回 + 號
$width = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);
$image = @imagecreate($width+2, $height+2);
$black = imagecolorallocate($image, 0, 0, 0); // 背景
$white = imagecolorallocate($image, 255, 255, 255);
imagestring($image, 2, 1, 1, $string, $white);
imagepng($image);
imagedestroy($image);
?>
To Top