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


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 相关文章推荐
require(),include(),require_once()和include_once()的异同
Jan 02 PHP
php图片验证码代码
Mar 27 PHP
在字符串指定位置插入一段字符串的php代码
Feb 16 PHP
PHP字符串的连接的简单实例
Dec 30 PHP
WebQQ最新登陆协议的用法
Dec 22 PHP
php计划任务之ignore_user_abort函数实现方法
Jan 08 PHP
php读取文件内容的方法汇总
Jan 24 PHP
PHP实现将textarea的值根据回车换行拆分至数组
Jun 10 PHP
PHP封装CURL扩展类实例
Jul 28 PHP
php三种实现多线程类似的方法
Oct 30 PHP
PHP实现负载均衡session共享redis缓存操作示例
Aug 22 PHP
WordPress伪静态规则设置代码实例
Dec 10 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
php横向重复区域显示二法
2008/09/25 PHP
php 什么是PEAR?(第三篇)
2009/03/19 PHP
php正则表达式使用的详细介绍
2013/04/27 PHP
php文字水印和php图片水印实现代码(二种加水印方法)
2013/12/25 PHP
PHP调试函数和日志记录函数分享
2015/01/31 PHP
Zend Framework框架Smarty扩展实现方法
2016/03/22 PHP
自定义min版smarty模板引擎MinSmarty.class.php文件及用法
2016/05/20 PHP
解决在laravel中leftjoin带条件查询没有返回右表为NULL的问题
2019/10/15 PHP
xml文档转换工具,附图表例子(hta)
2010/11/17 Javascript
JavaScript设计模式之单例模式实例
2014/09/24 Javascript
JavaScript判断变量是否为空的自定义函数分享
2015/01/31 Javascript
vue2实现数据请求显示loading图
2017/11/28 Javascript
jQuery中的类名选择器(.class)用法简单示例
2018/05/14 jQuery
layui-laydate时间日历控件使用方法详解
2018/11/15 Javascript
JS中getElementsByClassName与classList兼容性问题解决方案分析
2019/08/07 Javascript
JavaScript相等运算符的九条规则示例详解
2019/10/20 Javascript
在NodeJs中使用node-schedule增加定时器任务的方法
2020/06/08 NodeJs
Vue2.0 $set()的正确使用详解
2020/07/28 Javascript
盘点提高 Python 代码效率的方法
2014/07/03 Python
python实现bucket排序算法实例分析
2015/05/04 Python
Python多进程并发(multiprocessing)用法实例详解
2015/06/02 Python
使用python实现链表操作
2018/01/26 Python
在django admin中添加自定义视图的例子
2019/07/26 Python
Python的形参和实参使用方式
2019/12/24 Python
pyecharts调整图例与各板块的位置间距实例
2020/05/16 Python
pyCharm 设置调试输出窗口中文显示方式(字符码转换)
2020/06/09 Python
CSS3实现3D翻书效果
2016/06/20 HTML / CSS
Myprotein中国网站:欧洲畅销运动营养品牌
2021/02/11 全球购物
软件工程师岗位职责
2013/11/16 职场文书
小学生倡议书范文
2014/05/13 职场文书
启动仪式策划方案
2014/06/14 职场文书
国际贸易求职信
2014/07/05 职场文书
王亚平太空授课观后感
2015/06/12 职场文书
岗位聘任协议书
2015/09/21 职场文书
如何使用Tkinter进行窗口的管理与设置
2021/06/30 Python
vue3种table表格选项个数的控制方法
2022/04/14 Vue.js