欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-15 17:05:00
通过文字传递创建的图形按钮,详细说明请看文内英文说明
通过文字传递创建的图形按钮,详细说明请看文内英文说明
<?php header( "content-type: image/gif"); // info for the browser
    /* php3 button generator, (c) 2000 by izzysoft (izzysoft@buntspecht.de)
    * license: gpl (and it would be nice to drop me a note that you find it
    * useful - if you 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 will automatically use a smaller font if text is
    *too long for selected size.) default: 5
    *text - the text to be put on the button. will 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:
    * <img src="button.php3?button=yellow&text=example">
    * will 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 may 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 into 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
    ?>