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


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 相关文章推荐
新闻分类录入、显示系统
Oct 09 PHP
php缓存技术介绍
Nov 25 PHP
php 什么是PEAR?
Mar 19 PHP
一个PHP分页类的代码
May 18 PHP
PHP include_path设置技巧分享
Jul 03 PHP
关于php连接mssql:pdo odbc sql server
Jul 20 PHP
ThinkPHP3.1.3版本新特性概述
Jun 19 PHP
PHP反向代理类代码
Aug 15 PHP
php 判断字符串编码是utf-8 或gb2312实例
Nov 01 PHP
微信 getAccessToken方法详解及实例
Nov 23 PHP
PHP实现对图片的反色处理功能【测试可用】
Feb 01 PHP
PHP isset()及empty()用法区别详解
Aug 29 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
德劲1102收音机的打理维修案例
2021/03/02 无线电
php简单对象与数组的转换函数代码(php多层数组和对象的转换)
2011/05/18 PHP
关于PHP内存溢出问题的解决方法
2013/06/25 PHP
通过table标签,PHP输出EXCEL的实现方法
2013/07/24 PHP
PHP小教程之实现链表
2014/06/09 PHP
WordPress中给文章添加自定义字段及后台编辑功能区域
2015/12/19 PHP
php正则表达式验证(邮件地址、Url地址、电话号码、邮政编码)
2016/03/14 PHP
zend framework重定向方法小结
2016/05/28 PHP
PHP5.6.8连接SQL Server 2008 R2数据库常用技巧分析总结
2019/05/06 PHP
PHP 多进程与信号中断实现多任务常驻内存管理实例方法
2019/10/04 PHP
批量实现面向对象的实例代码
2013/07/01 Javascript
解决js数据包含加号+通过ajax传到后台时出现连接错误
2013/08/01 Javascript
javascript dom追加内容实现示例
2013/09/21 Javascript
处理文本部分内容的TextRange对象应用实例
2014/07/29 Javascript
jquery+CSS实现的多级竖向展开树形TRee菜单效果
2015/08/24 Javascript
深入解析桶排序算法及Node.js上JavaScript的代码实现
2016/07/06 Javascript
jQuery+css实现非常漂亮的水平导航菜单效果
2016/07/27 Javascript
Easyui 去除jquery-easui tab页div自带滚动条的方法
2019/05/10 jQuery
Vue之Mixins(混入)的使用方法
2019/09/24 Javascript
JS中自定义事件的使用与触发操作实例分析
2019/11/01 Javascript
JavaScript中的类型检查
2020/02/03 Javascript
[10:24]郎朗助力完美“圣”典,天籁交织奏响序曲
2016/12/18 DOTA
详解Python如何获取列表(List)的中位数
2016/08/12 Python
python使用opencv进行人脸识别
2017/04/07 Python
Python 列表(List) 的三种遍历方法实例 详解
2017/04/15 Python
django之状态保持-使用redis存储session的例子
2019/07/28 Python
Python 类属性与实例属性,类对象与实例对象用法分析
2019/09/20 Python
python实现单张图像拼接与批量图片拼接
2020/03/23 Python
用CSS3实现无限循环的无缝滚动的实例代码
2017/07/04 HTML / CSS
巴西男士胡须和头发护理产品商店:Beard
2017/11/13 全球购物
输入一行文字,找出其中大写字母、小写字母、空格、数字、及其他字符各有多少
2016/04/15 面试题
汽车装潢店创业计划书范文
2014/02/05 职场文书
高中军训第一天感言
2014/03/06 职场文书
教师三严三实对照检查材料
2014/09/25 职场文书
机关干部四风问题自我剖析及整改措施
2014/10/26 职场文书
springcloud之Feign超时问题的解决
2021/06/24 Java/Android