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


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 相关文章推荐
生成缩略图
Oct 09 PHP
PHP6 mysql连接方式说明
Feb 09 PHP
介绍一些PHP判断变量的函数
Apr 24 PHP
PHP的autoload机制的实现解析
Sep 15 PHP
PHP中mb_convert_encoding与iconv函数的深入解析
Jun 21 PHP
单点登录 Ucenter示例分析
Oct 29 PHP
PHP的一个完美GIF等比缩放类,附带去除缩放黑背景
Apr 01 PHP
PHP使用GIFEncoder类生成gif动态滚动字幕
Jul 01 PHP
php中FTP函数ftp_connect、ftp_login与ftp_chmod用法
Nov 18 PHP
php实现的数字验证码及数字运算验证码
Jul 30 PHP
php 自定义错误日志实例详解
Nov 12 PHP
php 算法之实现相对路径的实例
Oct 17 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 线程安全与非线程安全版本的区别深入解析
2013/08/06 PHP
PHP命名空间用法实例分析
2019/09/04 PHP
PHP常用函数之根据生日计算年龄功能示例
2019/10/21 PHP
用javascript实现给出的盒子的序列是否可连为一矩型
2007/08/30 Javascript
js常用代码段收集
2011/10/28 Javascript
js FLASH幻灯片字符串中有连接符&的处理方法
2012/03/01 Javascript
JSON为什么那样红为什么要用json(另有洞天)
2012/12/26 Javascript
jQuery基础_入门必看知识点
2016/07/04 Javascript
详解Vue用axios发送post请求自动set cookie
2017/05/10 Javascript
jquery-file-upload 文件上传带进度条效果
2017/11/21 jQuery
用vue快速开发app的脚手架工具
2018/06/11 Javascript
vue弹窗插件实战代码
2018/09/08 Javascript
微信小程序点击图片实现长按预览、保存、识别带参数二维码、转发等功能
2019/07/20 Javascript
javascript利用键盘控制小方块的移动
2020/04/20 Javascript
在Python的web框架中配置app的教程
2015/04/30 Python
Python实现将DOC文档转换为PDF的方法
2015/07/25 Python
Python中模块string.py详解
2017/03/12 Python
django 开发忘记密码通过邮箱找回功能示例
2018/04/17 Python
解决Pandas to_json()中文乱码,转化为json数组的问题
2018/05/10 Python
对numpy Array [: ,] 的取值方法详解
2018/07/02 Python
python TKinter获取文本框内容的方法
2018/10/11 Python
Python完成毫秒级抢淘宝大单功能
2019/06/06 Python
Python 运行.py文件和交互式运行代码的区别详解
2019/07/02 Python
python控制台实现tab补全和清屏的例子
2019/08/20 Python
python实现人像动漫化的示例代码
2020/05/17 Python
HTML5 Plus 实现手机APP拍照或相册选择图片上传功能
2016/07/13 HTML / CSS
线程同步的方法
2016/11/23 面试题
毕业生护理专业个人求职信范文
2014/01/04 职场文书
学习两会精神心得范文
2014/03/17 职场文书
庆六一文艺汇演活动方案
2014/08/26 职场文书
家属慰问信
2015/02/14 职场文书
2015年全国爱眼日活动方案
2015/05/05 职场文书
新年晚会主持词开场白
2015/05/28 职场文书
生产车间管理制度
2015/08/04 职场文书
2016年国庆节67周年活动总结
2016/04/01 职场文书
七年级写作指导之游记作文
2019/10/07 职场文书