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


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 相关文章推荐
Search File Contents PHP 搜索目录文本内容的代码
Feb 21 PHP
PHP中全面阻止SQL注入式攻击分析小结
Jan 30 PHP
php自定义session示例分享
Apr 22 PHP
php实现文件下载代码分享
Aug 19 PHP
php中的字符编码转换函数用法示例
Oct 20 PHP
详解PHP中instanceof关键字及instanceof关键字有什么作用
Nov 05 PHP
php session的锁和并发
Jan 22 PHP
thinkphp中多表查询中防止数据重复的sql语句(必看)
Sep 22 PHP
浅析php-fpm静态和动态执行方式的比较
Nov 09 PHP
详解PHP防止直接访问.php 文件的实现方法
Jul 28 PHP
php redis setnx分布式锁简单原理解析
Oct 23 PHP
浅谈PHP中的那些魔术常量
Dec 02 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 删除记录实现代码
2009/03/12 PHP
php中时间函数date及常用的时间计算
2017/05/12 PHP
PHP实现将几张照片拼接到一起的合成图片功能【便于整体打印输出】
2017/11/14 PHP
php使用yield对性能提升的测试实例分析
2019/09/19 PHP
Git命令之分支详解
2021/03/02 PHP
JAVASCRIPT 对象的创建与使用
2021/03/09 Javascript
js下判断 iframe 是否加载完成的完美方法
2010/10/26 Javascript
jquery win 7透明弹出层效果的简单代码
2013/08/06 Javascript
js 与 php 通过json数据进行通讯示例
2014/03/26 Javascript
原生js实现倒计时功能(多种格式调用)
2017/01/12 Javascript
vue使用xe-utils函数库的具体方法
2018/03/06 Javascript
详解Vue中的MVVM原理和实现方法
2020/07/15 Javascript
[04:52]2015国际邀请赛LGD战队晋级之路
2015/08/14 DOTA
[01:08:48]LGD vs OG 2018国际邀请赛淘汰赛BO3 第三场 8.25
2018/08/29 DOTA
python的socket编程入门
2018/01/29 Python
Django中使用Celery的方法示例
2018/11/29 Python
Python中GeoJson和bokeh-1的使用讲解
2019/01/03 Python
Python队列RabbitMQ 使用方法实例记录
2019/08/05 Python
Python爬虫HTPP请求方法有哪些
2020/06/03 Python
CSS3绘制六边形的简单实现
2016/08/25 HTML / CSS
Shopee马来西亚:随拍即卖,最佳行动电商拍卖平台
2017/06/05 全球购物
英国儿童鞋和靴子:Start-Rite
2019/05/06 全球购物
Deux par Deux官方网站:设计师童装
2020/01/03 全球购物
会计自荐书
2013/12/02 职场文书
总经理驾驶员岗位职责
2013/12/04 职场文书
施工协议书范本
2014/04/22 职场文书
希特勒经典演讲稿
2014/05/19 职场文书
分公司经理任命书
2014/06/05 职场文书
节约粮食标语
2014/06/18 职场文书
居委会四风问题个人对照检查材料
2014/09/25 职场文书
国庆阅兵观后感
2015/06/15 职场文书
结婚典礼主持词
2015/06/29 职场文书
初中班干部工作总结
2015/08/10 职场文书
党员心得体会范文2016
2016/01/23 职场文书
一文解答什么是MySQL的回表
2022/08/05 MySQL
HTML中link标签属性的具体用法
2023/05/07 HTML / CSS