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


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 相关文章推荐
基于mysql的论坛(5)
Oct 09 PHP
PHP 反向排序和随机排序代码
Jun 30 PHP
PHP中设置时区方法小结
Jun 03 PHP
php初始化对象和析构函数的简单实例
Mar 11 PHP
ThinkPHP模板引擎之导入资源文件方法详解
Jun 18 PHP
php从完整文件路径中分离文件目录和文件名的方法
Mar 13 PHP
php源码分析之DZX1.5随机数函数random用法
Jun 17 PHP
PHP实现简单的新闻发布系统实例
Jul 28 PHP
Yii中CGridView实现批量删除的方法
Dec 28 PHP
thinkPHP5 tablib标签库自定义方法详解
May 10 PHP
Laravel5.1 框架Request请求操作常见用法实例分析
Jan 04 PHP
laravel框架使用FormRequest进行表单验证,验证异常返回JSON操作示例
Feb 18 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控制linux服务器常用功能 关机 重启 开新站点等
2012/09/05 PHP
一个PHP的远程图片抓取函数分享
2013/09/25 PHP
ThinkPHP基本的增删查改操作实例教程
2014/08/22 PHP
js 获取屏幕各种宽高的方法(浏览器兼容)
2013/05/15 Javascript
原生Js实现元素渐隐/渐现(原理为修改元素的css透明度)
2013/06/24 Javascript
HTML长文本截取含有HTML代码同样适用的两种方法
2013/07/31 Javascript
二叉树先序遍历的非递归算法具体实现
2014/01/09 Javascript
jQuery scroll事件实现监控滚动条分页示例
2014/04/04 Javascript
使用JavaScript实现网页版Pongo设计思路及源代码分享
2014/06/16 Javascript
页面内容排序插件jSort使用方法
2015/10/10 Javascript
jquery实现网站列表切换效果的2种方法
2016/08/12 Javascript
利用require.js与angular搭建spa应用的方法实例
2017/07/19 Javascript
使用async-validator编写Form组件的方法
2018/01/10 Javascript
深入理解Vue nextTick 机制
2018/04/28 Javascript
JS解析后台返回的JSON格式数据实例
2018/08/06 Javascript
jQuery实现的点击图片居中放大缩小功能示例
2019/01/16 jQuery
详解使用React.memo()来优化函数组件的性能
2019/03/19 Javascript
jQuery实现手风琴效果(蒙版)
2020/01/11 jQuery
Vue向后台传数组数据,springboot接收vue传的数组数据实例
2020/11/12 Javascript
Python标准异常和异常处理详解
2015/02/02 Python
python中的随机函数random的用法示例
2018/01/27 Python
DataFrame 将某列数据转为数组的方法
2018/04/13 Python
Python实现提取XML内容并保存到Excel中的方法
2018/09/01 Python
Python利用 utf-8-sig 编码格式解决写入 csv 文件乱码问题
2020/02/21 Python
Python基于QQ邮箱实现SSL发送
2020/04/26 Python
python中random.randint和random.randrange的区别详解
2020/09/20 Python
详解html5 postMessage解决跨域通信的问题
2018/08/17 HTML / CSS
大学生毕业求职的自我评价
2013/09/29 职场文书
公司企业表扬信
2014/01/11 职场文书
写给女朋友的道歉信
2014/01/12 职场文书
学生会个人自荐书范文
2014/02/12 职场文书
《愚公移山》教学反思
2014/02/20 职场文书
七匹狼男装广告词
2014/03/21 职场文书
离婚协议书范本(2014版)
2014/09/28 职场文书
扶贫办主任查摆“四风”问题个人对照检查材料思想汇报
2014/10/02 职场文书
Nginx搭建rtmp直播服务器实现代码
2021/03/31 Servers