Graphic buttons created by passing text

Source: Internet
Author: User
Tags array
A graphical button created by passing text, please read the text in English to explain
<?php Header ("Content-type:image/gif"); Info for the browser
/* PHP3 Button Generator, (c) by Izzysoft (izzysoft@buntspecht.de)
* LICENSE:GPL (and it would is nice to drop me a
* Useful-if use it. And, of course, I am very interested in
* Enhancements you made to the script!
*
* purpose:generate buttons with text passed by parameter.
*
* Possible parameters to the script:
*button-input gif image. Just the part of the filename before the dot.
*the specified image file is expected in the same directory
*as this script resides in.
*font-font to-use (1-5) 1 is very small, 3 medium, 5 normal size.
*the script would automatically use a smaller the font if text is
*too long for selected size.) Default:5
*text-the text to is put on the button. would be centered.
*textcolor-color for the letters. Default:white.
*in This example code only blue, white and black are defined;
*but You can add custom colors easily.
*width,heigth-width and Heigth of the button. Useful only if Target
*button should have different size than source image.
*
* Example for usage:
*
* 'll look for yellow.gif and put the string ' Example ' on it.
*
* I use to have three buttons I normally generate (one displays selected
* Item, one is for mouseover (), and one is the default button). The source
* Images are yellow.gif, white.gif and blue.gif-so my script assumes
* Blue.gif as Default if "button=" not specified-you could wish to change
* This below, it's easy;)
*/
===========================[Check fo
R parameters and/or set defaults]===
if (($font = = "") | | ($font > 5) | | ($font < 1)) {$font = 5;}
if ($text = = "") {$text = "moin!";} Button text
if ($textcolor = = "") {//color for the letters
Switch ($button) {
Case "Yellow":
Case "White":
$textcolor = "BLACK";
Break
Default
if ($button = = "") {$button = "blue";}
$textcolor = "White";
Break
}
}//TextColor end
$im _info = getimagesize ("$button. gif"); Button size
if ($width = = "") {
if ($im _info = "") {
$buttonwidth = 125;
} else {
$buttonwidth = "$im _info[0]";
}
} else {
$buttonwidth = $width;
}
if ($heigth = = "") {
if ($im _info = "") {
$buttonheigth = 30;
} else {
$buttonheigth = "$im _info[1]";
}
} else {
$buttonheigth = $heigth;
}
$vmidth = Ceil ($buttonheigth/2);
// =====================================
===[now lets define some colors]===

$white = "255,255,255";
$black = "0,0,0";
$blue = "0x2c,0c6d,0xaf";
// =====================================
=============[Build Color Array]===
Now we put the needed color in an A
Rray (if e.g. "$textcolor =white",
The array $textcolor _array represents
"White")
$textcolor _array = Explode (",", $ $textcolor);
=======================[Calculate PO
Sition of the text on the button]===
do {
$textwidth = strlen ($text) * Imagefontwidth ($font);
$x = ($buttonwidth-$textwidth)/2; $x = Ceil ($x);
$y = $vmidth-(imagefontheight ($font)/2);
$font--;
while (($x < 0) && ($font > 0)); $font + +;
// =====================================
======[Now we create the button]===
if (Isset ($width) | | | isset ($HEIGTH)) {//size change expected?
$ima = Imagecreatefromgif ("$button. gif");//Open Input GIF
$im = Imagecreate ($buttonwidth, $buttonheigth); Create img in desired size
$UGLYBG = Imagecolorallocate ($im, 0xf4,0xb2,0xe5);
Imagerectangle ($im, 0,0, $buttonwidth, $buttonheigth, $UGLYBG);
$dummy = imagecopyresized ($im, $ima, 0,0,0,0, $buttonwidth, $buttonheigth, $im _info[0], $im _info[1]);
if ($dummy = = "") {
Imagedestroy ($im); If it didn ' t work, create default below instead
} else {;}
Imagedestroy ($ima);
Imagecolortransparent ($im, $UGLYBG);
} else {
$im = Imagecreatefromgif ("$button. gif");//Open Input GIF
}
if ($im = = "") {$im = Imagecreate ($buttonwidth, $buttonheigth); If input gif not found,
$rblue = Imagecolorallocate ($im, 0X2C,0X6D,0XAF);//Create a default box
Imagerectangle ($im, 0,0,200,100, $rblue);
}
$color = Imagecolorallocate ($im, $textcolor _array[0], $textcolor _array[1], $textcolor _array[2]); Allocate the color
Imagestring ($im, $font, $x, $y, "$text", $color); Put the text on it
Imagegif ($im);/Send button to browser
Imagedestroy ($im)//Free The Used memory
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.