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


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 多行多列显示
Aug 15 PHP
Search File Contents PHP 搜索目录文本内容的代码
Feb 21 PHP
浅析PHP的静态成员函数效率更高的原因
Jun 13 PHP
ThinkPHP CURD方法之field方法详解
Jun 18 PHP
php中隐形字符65279(utf-8的BOM头)问题
Aug 16 PHP
Java和PHP在Web开发方面对比分析
Mar 01 PHP
PHP模板引擎Smarty内建函数foreach,foreachelse用法分析
Apr 11 PHP
根据key删除数组中指定的元素实现方法
Mar 02 PHP
详解PHP中mb_strpos的使用
Feb 04 PHP
PHP实现提高SESSION响应速度的几种方法详解
Aug 09 PHP
PHP设计模式(七)组合模式Composite实例详解【结构型】
May 02 PHP
thinkphp 获取控制器及控制器方法
Apr 16 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中json_decode()和json_encode()的使用方法
2012/06/04 PHP
php中jpgraph类库的使用介绍
2013/08/08 PHP
ThinkPHP框架实现session跨域问题的解决方法
2014/07/01 PHP
PHP中auto_prepend_file与auto_append_file用法实例分析
2014/09/22 PHP
php文件包含的几种方式总结
2019/09/19 PHP
JavaScript CSS修改学习第五章 给“上传”添加样式
2010/02/19 Javascript
基于jquery的cookie的用法
2011/01/10 Javascript
js setTimeout 常见问题小结
2013/08/13 Javascript
动态的绑定事件addEventListener方法的使用
2014/01/24 Javascript
JavaScript中的prototype.bind()方法介绍
2014/04/04 Javascript
我的Node.js学习之路(二)NPM模块管理
2014/07/06 Javascript
详解Bootstrap的aria-label和aria-labelledby应用
2016/01/04 Javascript
AngularJS入门教程之Select(选择框)详解
2016/07/27 Javascript
AngularJS ng-bind 指令简单实现
2016/07/30 Javascript
详解js的异步编程技术的方法
2017/02/09 Javascript
JavaScript 数据类型详解
2017/03/13 Javascript
jQuery+pjax简单示例汇总
2017/04/21 jQuery
jQuery zTree树插件动态加载实例代码
2017/05/11 jQuery
浅谈vue+webpack项目调试方法步骤
2017/09/11 Javascript
jQuery实现鼠标响应式淘宝动画效果示例
2018/02/13 jQuery
layui实现数据表格隐藏列的示例
2019/10/25 Javascript
python学习笔记:字典的使用示例详解
2014/06/13 Python
python单线程文件传输的实例(C/S)
2019/02/13 Python
NumPy 基本切片和索引的具体使用方法
2019/04/24 Python
python对文件目录的操作方法实例总结
2019/06/24 Python
Python自动化完成tb喵币任务的操作方法
2019/10/30 Python
Python Numpy库常见用法入门教程
2020/01/16 Python
让你相见恨晚的十个Python骚操作
2020/11/18 Python
Melijoe时尚童装德国官网:Melijoe德国
2016/09/03 全球购物
艺术家策划的室内设计:Curious Egg
2019/03/06 全球购物
流行文化收藏品:Sideshow(DC漫画,星球大战,漫威)
2019/03/17 全球购物
财务稽核岗位职责
2015/04/13 职场文书
巴黎圣母院观后感
2015/06/10 职场文书
MySQL千万级数据表的优化实战记录
2021/08/04 MySQL
使用Python+OpenCV进行卡类型及16位卡号数字的OCR功能
2021/08/30 Python
python实现双向链表原理
2022/05/25 Python