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


Posted in PHP onOctober 09, 2006

通过文字传递创建的图形按钮,详细说明请看文内英文说明
<?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
    ?>         

PHP 相关文章推荐
php实现的简单压缩英文字符串的代码
Apr 24 PHP
PHP COOKIE设置为浏览器进程
Jun 21 PHP
PHP类与对象中的private访问控制的疑问
Nov 01 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(一)
Jun 23 PHP
ThinkPHP框架实现session跨域问题的解决方法
Jul 01 PHP
ThinkPHP基本的增删查改操作实例教程
Aug 22 PHP
php中mysql连接方式PDO使用详解
Feb 25 PHP
PHP的Yii框架中移除组件所绑定的行为的方法
Mar 18 PHP
php性能分析之php-fpm慢执行日志slow log用法浅析
Oct 17 PHP
PHP观察者模式示例【Laravel框架中有用到】
Jun 15 PHP
PHP的PDO大对象(LOBs)
Jan 27 PHP
laravel-admin 实现给grid的列添加行数序号的方法
Oct 08 PHP
计算2000年01月01日起到指定日的天数
Oct 09 #PHP
文件上传程序的全部源码
Oct 09 #PHP
一个简单计数器的源代码
Oct 09 #PHP
一个用mysql_odbc和php写的serach数据库程序
Oct 09 #PHP
PHP脚本数据库功能详解(下)
Oct 09 #PHP
PHP脚本数据库功能详解(中)
Oct 09 #PHP
PHP脚本数据库功能详解(上)
Oct 09 #PHP
You might like
让你同时上传 1000 个文件 (二)
2006/10/09 PHP
推荐一篇入门级的Class文章
2007/03/19 PHP
FleaPHP的安全设置方法
2008/09/15 PHP
ThinkPHP后台首页index使用frameset时的注意事项分析
2014/08/22 PHP
laravel实现批量更新多条记录的方法示例
2017/10/22 PHP
JavaScript 应用技巧集合[推荐]
2009/08/30 Javascript
JavaScript获取图片的原始尺寸以宽度为例
2014/05/04 Javascript
JavaScript函数使用的基本教程
2015/06/04 Javascript
json格式的javascript对象用法分析
2016/07/04 Javascript
Bootstrap 模态框(Modal)插件代码解析
2016/12/21 Javascript
JS如何生成一个不重复的ID的函数
2016/12/25 Javascript
使用AngularJS2中的指令实现按钮的切换效果
2017/03/27 Javascript
vue-devtools的安装步骤
2018/04/23 Javascript
JavaScript遍历数组的三种方法map、forEach与filter实例详解
2019/02/27 Javascript
js实现图片粘贴到网页
2019/12/06 Javascript
微信小程序登陆注册功能的实现代码
2019/12/10 Javascript
js实现无缝轮播图特效
2020/05/09 Javascript
微信小程序 wx:for 与 wx:for-items 与 wx:key的正确用法
2020/05/19 Javascript
[02:16]DOTA2超级联赛专访Burning 逆袭需要抓住机会
2013/06/24 DOTA
[01:06:30]DOTA2-DPC中国联赛定级赛 Phoenix vs DLG BO3第二场 1月9日
2021/03/11 DOTA
跟老齐学Python之一个免费的实验室
2014/09/14 Python
python文件与目录操作实例详解
2016/02/22 Python
20个常用Python运维库和模块
2018/02/12 Python
解决pandas中读取中文名称的csv文件报错的问题
2018/07/04 Python
Python标准库json模块和pickle模块使用详解
2020/03/10 Python
Python 将代码转换为可执行文件脱离python环境运行(步骤详解)
2021/01/25 Python
Python爬虫爬取ts碎片视频+验证码登录功能
2021/02/22 Python
阿根廷旅游网站:almundo阿根廷
2018/02/12 全球购物
家长会主持词
2014/03/26 职场文书
学期个人工作总结
2015/02/13 职场文书
农村环境卫生倡议书
2015/04/29 职场文书
永不妥协观后感
2015/06/10 职场文书
《成长的天空》读后感3篇
2019/12/06 职场文书
nginx简单配置多个server的方法
2021/03/31 Servers
Python3.10的一些新特性原理分析
2021/09/15 Python
Windows Server 2012 R2 磁盘分区教程
2022/04/29 Servers