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


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生成EXCEL的东东
Oct 09 PHP
巧用php中的array_filter()函数去掉多维空值的代码分享
Sep 07 PHP
探讨:如何编写PHP扩展
Jun 13 PHP
PHP加密扩展库Mcrypt安装和实例
Nov 10 PHP
php实现分页工具类分享
Jan 09 PHP
php简单的留言板与回复功能具体实现
Feb 19 PHP
Zend Framework框架Smarty扩展实现方法
Mar 22 PHP
php上传图片类及用法示例
May 11 PHP
PHP实现数组array转换成xml的方法
Jul 19 PHP
PHP 5.6.11 访问SQL Server2008R2的几种情况详解
Aug 08 PHP
PHP+AJAX 投票器功能
Nov 11 PHP
漂亮的thinkphp 跳转页封装示例
Oct 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+mysqli实现批量执行插入、更新及删除数据的方法
2015/01/29 PHP
简介PHP的Yii框架中缓存的一些高级用法
2016/03/29 PHP
php计数排序算法的实现代码(附四个实例代码)
2020/03/31 PHP
JQuery实现点击div以外的位置隐藏该div窗口
2013/09/13 Javascript
谷歌showModalDialog()方法不兼容出现对话窗口的解决办法
2016/02/15 Javascript
基于AngularJS+HTML+Groovy实现登录功能
2016/02/17 Javascript
WEB前端开发框架Bootstrap3 VS Foundation5
2016/05/16 Javascript
简单实现的JQuery文本框水印插件
2016/06/14 Javascript
AngularJS 模块详解及简单实例
2016/07/28 Javascript
JS异步加载的三种实现方式
2017/03/16 Javascript
使用bootstraptable插件实现表格记录的查询、分页、排序操作
2017/08/06 Javascript
浅谈vue的iview列表table render函数设置DOM属性值的方法
2017/09/30 Javascript
使用Vant完成通知栏Notify的提示操作
2020/11/11 Javascript
js回到页面指定位置的三种方式
2020/12/17 Javascript
详细解析Python中__init__()方法的高级应用
2015/05/11 Python
python非递归全排列实现方法
2017/04/10 Python
LRUCache的实现原理及利用python实现的方法
2017/11/21 Python
django实现用户登陆功能详解
2017/12/11 Python
Python实现读取字符串按列分配后按行输出示例
2018/04/17 Python
Python3爬虫学习之应对网站反爬虫机制的方法分析
2018/12/12 Python
解决pyinstaller打包发布后的exe文件打开控制台闪退的问题
2019/06/21 Python
Python字节单位转换实例
2019/12/05 Python
python的数学算法函数及公式用法
2020/11/18 Python
Python: glob匹配文件的操作
2020/12/11 Python
澳大利亚最超值的自行车之家:Reid Cycles
2019/03/24 全球购物
请说出你所知道的线程同步的方法
2013/04/19 面试题
优秀的毕业生的自我评价
2013/12/12 职场文书
银行员工辞职信范文
2014/01/20 职场文书
2014年员工工作总结范文
2014/11/18 职场文书
志愿者事迹材料
2014/12/26 职场文书
慈善募捐倡议书
2015/04/27 职场文书
篮球赛新闻稿
2015/07/17 职场文书
《金色的草地》教学反思
2016/02/17 职场文书
2019让人心动的商业计划书
2019/06/27 职场文书
python opencv将多个图放在一个窗口的实例详解
2022/02/28 Python
Python OpenCV形态学运算示例详解
2022/04/07 Python