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


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 随机排序广告的实现代码
May 09 PHP
PHP 图片上传代码
Sep 13 PHP
浅析PHP递归函数返回值使用方法
Feb 18 PHP
php类中的各种拦截器用法分析
Nov 03 PHP
php准确计算复活节日期的方法
Apr 18 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
Apr 21 PHP
详细解读PHP的Yii框架中登陆功能的实现
Aug 21 PHP
Thinkphp单字母函数使用指南
May 08 PHP
PHP模块化安装教程
Jun 01 PHP
php及codeigniter使用session-cookie的方法(详解)
Apr 06 PHP
PHP PDOStatement::closeCursor讲解
Jan 30 PHP
PHP中国际化的字符串排序和比较对象详解
Aug 23 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
简单实用的.net DataTable导出Execl
2013/10/28 PHP
PHP获取当前完整URL地址的函数
2014/12/21 PHP
CentOS7.0下安装PHP5.6.30服务的教程详解
2018/09/29 PHP
jQuery UI Autocomplete 体验分享
2012/02/14 Javascript
javascript学习笔记(十二) RegExp类型介绍
2012/06/20 Javascript
js 延迟加载 改变JS的位置加快网页加载速度
2012/12/11 Javascript
javascript中typeof的使用示例
2013/12/19 Javascript
Nodejs使用mysql模块之获得更新和删除影响的行数的方法
2014/03/18 NodeJs
最流行的Node.js精简型和全栈型开发框架介绍
2015/02/26 Javascript
百度地图API之本地搜索与范围搜索
2015/07/30 Javascript
JS实现完全语义化的网页选项卡效果代码
2015/09/15 Javascript
JS实现按比例缩放图片的方法(附C#版代码)
2015/12/08 Javascript
分享JavaScript与Java中MD5使用两个例子
2015/12/23 Javascript
JS 中可以提升幸福度的小技巧(可以识别更多另类写法)
2018/07/28 Javascript
在小程序Canvas中使用measureText的方法示例
2018/10/19 Javascript
[05:39]2014DOTA2西雅图国际邀请赛 淘汰赛7月14日TOPPLAY
2014/07/14 DOTA
编程语言Python的发展史
2014/09/26 Python
在RedHat系Linux上部署Python的Celery框架的教程
2015/04/07 Python
在DigitalOcean的服务器上部署flaskblog应用
2015/12/19 Python
Python Django使用forms来实现评论功能
2016/08/17 Python
Python新手们容易犯的几个错误总结
2017/04/01 Python
Python实现扩展内置类型的方法分析
2017/10/16 Python
Python中常见的异常总结
2018/02/20 Python
Django开发中复选框用法示例
2018/03/20 Python
用PyInstaller把Python代码打包成单个独立的exe可执行文件
2018/05/26 Python
Django DRF路由与扩展功能的实现
2020/06/03 Python
简单了解Django项目应用创建过程
2020/07/06 Python
python 合并多个excel中同名的sheet
2021/01/22 Python
给幼儿园老师的表扬信
2014/01/19 职场文书
企业总经理岗位职责
2014/02/13 职场文书
规划编制实施方案
2014/03/15 职场文书
幼儿园个人师德总结
2015/02/06 职场文书
还在手动盖楼抽奖?教你用Python实现自动评论盖楼抽奖(一)
2021/06/07 Python
python简单验证码识别的实现过程
2021/06/20 Python
Mysql排序的特性详情
2021/11/01 MySQL
SpringCloud Function SpEL注入漏洞分析及环境搭建
2022/04/08 Java/Android