通过文字传递创建的图形按钮

通过文字传递创建的图形按钮,详细说明请看文内英文说明

 1 Header( "Content-type: image/gif"); // info for the browser   
 2/* PHP3 Button generator, (c) 2000 by IzzySoft ([email protected])   
 3* License: GPL (and it would be nice to drop me a note that you find it   
 4* useful - if you use it. And, of course, I am very interested in   
 5* enhancements you made to the script!   
 6*   
 7* Purpose: generate buttons with text passed by parameter.   
 8*   
 9* possible parameters to the script:   
10*button- input gif image. Just the part of the filename before the dot.   
11*The specified image file is expected in the same directory   
12*as this script resides in.   
13*font - font to use (1 - 5; 1 is very small, 3 medium, 5 normal size.   
14*The script will automatically use a smaller font if text is   
15*too long for selected size.) Default: 5   
16*text - the text to be put on the button. Will be centered.   
17*textcolor - color for the letters. Default: white.   
18*in this example code only blue, white and black are defined;   
19*but you can add custom colors easily.   
20*width,heigth - width and heigth of the button. Useful only if target   
21*button should have different size than source image.   
22*   
23* Example for usage:   
24*

<img src="button.php3?button=yellow&amp;text=Example"/>

 1* will look for yellow.gif and put the string "Example" on it.   
 2*   
 3* I use to have three buttons I normally generate (one displays selected   
 4* item, one is for mouseover(), and one is the default button). The source   
 5* images are yellow.gif, white.gif and blue.gif - so my script assumes   
 6* blue.gif as default if "button=" not specified - you may wish to change   
 7* this below, it's easy ;)   
 8*/   
 9// ===========================[ check fo   
10// r parameters and/or set defaults ]===   
11if (($font == "") || ($font > 5) || ($font < 1)) { $font = 5; }   
12if ($text == "") { $text="Moin!"; }// button text   
13if ($textcolor == "") {// color for the letters   
14switch ($button) {   
15case "yellow":   
16case "white":   
17$textcolor = "black";   
18break;   
19default:   
20if ($button == "") { $button = "blue"; }   
21$textcolor = "white";   
22break;   
23}   
24} // textcolor end   
25$im_info = getimagesize("$button.gif"); // button size   
26if ($width == "") {   
27if ($im_info == "") {   
28$buttonwidth = 125;   
29} else {   
30$buttonwidth = "$im_info[0]";   
31}   
32} else {   
33$buttonwidth = $width;   
34}   
35if ($heigth == "") {   
36if ($im_info == "") {   
37$buttonheigth = 30;   
38} else {   
39$buttonheigth = "$im_info[1]";   
40}   
41} else {   
42$buttonheigth = $heigth;   
43}   
44$vmidth = ceil($buttonheigth / 2);   
45// =====================================   
46// ===[ now lets define some colors ]===   
47  
48$white = "255,255,255";   
49$black = "0,0,0";   
50$blue = "0x2c,0c6d,0xaf";   
51// =====================================   
52// =============[ build color array ]===   
53// now we put the needed color into an a   
54// rray (if e.g. "$textcolor=white",   
55// the array $textcolor_array represents   
56// "white")   
57$textcolor_array = explode(",", $$textcolor);   
58// =======================[ calculate po   
59// sition of the text on the button ]===   
60do {   
61$textwidth = strlen($text) * imagefontwidth($font);   
62$x = ($buttonwidth - $textwidth) / 2; $x = ceil($x);   
63$y = $vmidth - (imagefontheight($font) / 2);   
64$font--;   
65} while (($x < 0) && ($font > 0)); $font++;   
66// =====================================   
67// ======[ now we create the button ]===   
68if (isset($width) || isset($heigth)) {// size change expected?   
69$ima = imagecreatefromgif("$button.gif");// open input gif   
70$im = imagecreate($buttonwidth,$buttonheigth); // create img in desired size   
71$uglybg = ImageColorAllocate($im,0xf4,0xb2,0xe5);   
72ImageRectangle($im,0,0,$buttonwidth,$buttonheigth,$uglybg);   
73$dummy = imagecopyresized($im,$ima,0,0,0,0,$buttonwidth,$buttonheigth,$im_info[0],$im_info[1]);   
74if ($dummy == "") {   
75ImageDestroy($im); // if it didn't work, create default below instead   
76} else {;}   
77ImageDestroy($ima);   
78ImageColorTransparent($im,$uglybg);   
79} else {   
80$im = imagecreatefromgif("$button.gif");// open input gif   
81}   
82if ($im == "") { $im = imagecreate($buttonwidth,$buttonheigth); // if input gif not found,   
83$rblue = ImageColorAllocate($im, 0x2c,0x6D,0xAF);// create a default box   
84ImageRectangle($im,0,0,200,100,$rblue);   
85}   
86$color = ImageColorAllocate($im, $textcolor_array[0], $textcolor_array[1], $textcolor_array[2]); // allocate the color   
87imagestring($im, $font, $x, $y, "$text", $color); // put the text on it   
88ImageGif($im);// send button to browser   
89ImageDestroy($im);// free the used memory   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus