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


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(8) php 数组
Mar 05 PHP
php expects parameter 1 to be resource, array given 错误
Mar 23 PHP
允许phpmyadmin空密码登录的配置方法
May 29 PHP
php类声明和php类使用方法示例分享
Mar 29 PHP
php计算当前程序执行时间示例
Apr 24 PHP
php中文验证码实现方法
Jun 18 PHP
在Mac OS上自行编译安装Apache服务器和PHP解释器
Dec 24 PHP
利用ajax和PHP实现简单的流程管理
Mar 23 PHP
PHP回调函数与匿名函数实例详解
Aug 16 PHP
PHP解析url并得到url参数方法总结
Oct 11 PHP
php PDO属性设置与操作方法分析
Dec 27 PHP
PHP从零开始打造自己的MVC框架之类的自动加载实现方法详解
Jun 03 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
给apache2.2加上mod_encoding模块後 php5.2.0 处理url出现bug
2007/04/12 PHP
php set_magic_quotes_runtime() 函数过时解决方法
2010/07/08 PHP
Windows和Linux中php代码调试工具Xdebug的安装与配置详解
2014/05/08 PHP
PHP xpath()函数讲解
2019/02/11 PHP
thinkphp5+layui实现的分页样式示例
2019/10/08 PHP
PHP利用缓存处理用户注册时的邮箱验证,成功后用户数据存入数据库操作示例
2019/12/31 PHP
YII2框架中查询生成器Query()的使用方法示例
2020/03/18 PHP
Avengerls vs Newbee BO3 第二场2.18
2021/03/10 DOTA
javascript 写类方式之八
2009/07/05 Javascript
ASP.NET jQuery 实例17 通过使用jQuery validation插件校验ListBox
2012/02/03 Javascript
js/ajax跨越访问-jsonp的原理和实例(javascript和jquery实现代码)
2012/12/27 Javascript
jquery写个checkbox——类似邮箱全选功能
2013/03/19 Javascript
易被忽视的js事件问题总结
2016/05/14 Javascript
iOS和Android用同一个二维码实现跳转下载链接的方法
2016/09/28 Javascript
jquery横向纵向鼠标滚轮全屏切换
2017/02/27 Javascript
基于BootStrap实现简洁注册界面
2017/07/20 Javascript
详解vue2 $watch要注意的问题
2017/09/08 Javascript
微信小程序 wxParse插件显示视频问题
2019/09/27 Javascript
Vue自定义多选组件使用详解
2020/09/08 Javascript
[33:17]OG vs VGJ.T 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
wxpython 学习笔记 第一天
2009/03/16 Python
基于python中的TCP及UDP(详解)
2017/11/06 Python
pycharm新建一个python工程步骤
2019/07/16 Python
Python编写打字训练小程序
2019/09/26 Python
python中的函数递归和迭代原理解析
2019/11/14 Python
Jupyter Notebook安装及使用方法解析
2020/11/12 Python
戴尔英国官网:Dell英国
2017/05/27 全球购物
加拿大时尚床上用品零售商:QE Home | Quilts Etc
2018/01/22 全球购物
波兰灯具、照明和LED购物网站:Lampy.pl
2019/03/11 全球购物
中专三年学习的个人自我评价
2013/12/12 职场文书
十月份红领巾广播稿
2014/01/22 职场文书
销售主管岗位职责范本
2014/02/14 职场文书
《母鸡》教学反思
2014/02/25 职场文书
经销商订货会主持词
2014/03/27 职场文书
保护环境演讲稿
2014/05/10 职场文书
企业总经理助理岗位职责
2014/09/12 职场文书