将html中的图片和html保存在一个文件里

html是超文本标记语言,里面显示的图片一般是指向另一个url的,但是,在我使用word的时候,我发现word文件里面是可以包含图片的,而且图片是保存在word文件里面的,这个就比html方便多了,当我保存html的时候,一般需要把相应的资源文件(图片、css、js)保存到一个目录里面,这个感觉麻烦。
css、js是可以直接写在html里面的,那么,能不能将图片数据也直接写到html里面的,去google百度了一下,还真有:
http://hi.baidu.com/guodong828/blog/item/7d86960acf23052bb0351d7c.html

需要注意的是:
1. IE到IE8都还不支持(IE9没试)
2. base64的数据膨胀系数为 1/7,即原来的7位的数据被编码成了1个字节

避免变成死链接,这里copy一份:
—————————————————————————

        所谓"data"类型的Url格式,是在RFC2397中提出的,目的对于一些“小”的数据,可以在网页中直接嵌入,而不是从外部文件载入。例如对于img这个Tag,哪怕这个图片非常非常的小,小到只有一个点,也是要从另外一个外部的图片文件例如gif文件中读入的,如果浏览器实现了data类型的Url格式,这个文件就可以直接从页面文件内部读入了。

  data类型的Url格式早在1998年就提出了,时至今日,Firfox、Opera、Safari和Konqueror这些浏览器都已经支持,但是IE直到7.0版本都还没有支持,IE不支持的东西太多了,也不差这一个。

[小例子]

下面这个html代码可以在支持data类型Url的浏览器中运行,例如Firefox。运行后会看到一条蓝色渐变底色的标题。

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml" >

      <head>

      <style type="text/css">

            .title {  background-image:url(data:image/gif;base64,R0lGODlhAQAcALMAAMXh96HR97XZ98Hf98Xg97DX97nb98Lf97vc98Tg973d96rU97ba97%2Fe96XS9
wAAACH5BAAAAAAALAAAAAABABwAAAQVMLhVBDNItXESAURyDI2CGIxQLE4EADs%3D);

            background-repeat:repeat-x;

            height:28px;

            line-height: 28px;

            text-align:center;

      }

      </style>

      </head>

      <body>

            <div class="title">Hello, world!</div>

      </body>

      </html>

  这个渐变的蓝色底色实际上是用一个1×28的小图片通过横行重复(repeat-x)形成的。这个图片很小,不过104个字节,直接嵌入到html或css文件还是很合适的。

  data格式的Url最直接的好处是,这些Url原本会引起一个新的网络访问,因为那里是一个网页的地址,现在不会有新的网络访问了,因为现在这里是网页的内容。这样做,会减少服务器的负载,当然同时也增加了当前网页的大小。所以对“小”数据特别有好处。

  data类型Url的形式既然是Url,当然也可以直接在浏览器的地址栏中输入。

  data:text/html,<html><body><p><b>Hello, world!</b></p></body></html>在浏览器中输入以上的Url,会得到一个加粗的"Hello, world!"。也就是说,data:后面的数据直接用做网页的内容,而不是网页的地址。

简单的说,data类型的Url大致有下面几种形式。

      data:,<文本数据>data:text/plain,<文本数据>

      data:text/html,<HTML代码>

      data:text/html;base64,<base64编码的HTML代码>

      data:text/css,<CSS代码>

      data:text/css;base64,<base64编码的CSS代码>

      data:text/javascript,<Javascript代码>

      data:text/javascript;base64,<base64编码的Javascript代码>

      data:image/gif;base64,base64编码的gif图片数据

      data:image/png;base64,base64编码的png图片数据

      data:image/jpeg;base64,base64编码的jpeg图片数据

      data:image/x-icon;base64,base64编码的icon图片数据

  因为Url是一种基于文本的协议,所以gif/png/jpeg这种二进制属于需要用base64进行编码。换句话说,引入base64以后,就可以支持任意形式的数据格式。下面是个png图片的例子,会在浏览器中显示一个Mozilla的图标。

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHd

hcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dP

FpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAE

EAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIG

TDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8v

Zswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJF

Pcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwT

AR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==

 可以在Html的Img对象中使用,例如:

<img src="data:image/x-icon;base64,AAABAAEAEBAAAAAAAABoBQAAF…" />

 可以在Css的background-image属性中使用,例如:

div.image {  width:100px;  height:100px;  background-image:url(data:image/x-icon;base64,AAABAAEAEBAAAAAAAABoBQAAF…);}

 可以在Html的Css链接处使用,例如:

<link rel="stylesheet" type="text/css"  href="data:text/css;base64,LyogKioqKiogVGVtcGxhdGUgKioq…" _fcksavedurl=""data:text/css;base64,LyogKioqKiogVGVtcGxhdGUgKioq…"" />

 可以在Html的Javascript链接处使用,例如:

<script type="text/javascript"  href="data:text/javascript;base64,dmFyIHNjT2JqMSA9IG5ldyBzY3Jv…"></script>

  完整的语法定义在RFC中,完整的语法定义如下:

dataurl:= "data:" [mediatype] [";base64"] "," datamediatype  := [type"/"subtype] *( ";" parameter )data:= *urlcharparameter  := attribute "=" valueurlchar指的就是一般url中允许的字符,有些字符需要转义,例如"="要转义为"%3D",不过我测试下来,至少在Firefox里面,不转义也是可以的。

  parameter可以对mediatype进行属性的扩展,常见的是charset,用来定义编码格式,在多语言情况下需要用到。例如下面的例子。

  data:text/plain;charset=UTF-8;base64,5L2g5aW977yM5Lit5paH77yB这个例子会显示出"你好,中文!"。如果吧charset部分去掉,就会显示乱码,因为我用的是UTF-8编码。

  Firefox有一个data类型Url的测试页面,列出了各种格式的data类型Url的测试Url,和测试结果说明。

base64编码和内容的隐秘

  把二进制数据转换成为Base64不是什么难事,比如Total Commander就有这样的功能。还有一些在线资源:

  http://www.greywyvern.com/code/php/binary2base64

  http://www.kawa.net/works/js/data-scheme/base64-e.html

  有些在线转换把base64里面的“=”转换成为%3D,这个在Url中和“=”是一样的,不转换也没什么问题。当然,这种Url还有一种隐秘的好处,就是将一些道貌岸然者不喜欢的东西,堂而皇之的放在页面上。


php 设置中的 auto_append_file 的一点注意

php 的auto_append_file设置允许在PHP执行退出时自动执行一个文件,我们就可以在这个文件中做一些自己想做的事了, 但是你要注意:
1. 该设置不能通过ini_set来设置,可在 php.ini, .htaccesshttpd.conf 中设置; 参考: http://php.net/ini
2.  如果主脚本中执行了exit,则auto_append_file 指定的文件将不被执行

参考资料:
http://cn.php.net/manual/zh/ini.php
http://cn.php.net/manual/zh/ini.sections.php

C语言中宏的几种定义方式

C语言中宏的几种定义方式:


#include
#include
#define ECHO(str) printf(“%s: %s”, #str,str)
#define ECHO2(str,num) printf(“%s”, str##num)
#define MULTILINE_ECHO(str,str2,str3) do {\
printf(“multiline echo:\n”); \
printf(“%s”, str); \
printf(“%s”, str2); \
printf(“%s”, str3); \
}while(0)

int main(int ac, char **av) {

char str[10] = “str1\n\0”;
char str2[10] = “str2\n\0”;
char str3[10] = “str3\n\0”;
ECHO(str);
ECHO2(str,2);
MULTILINE_ECHO(str, str2, str3);
return 0;
}

str: str1
str2
multiline echo:
str1
str2
str3

vim阅读apache的mod_php

创建tags 文件:
php-5.3.3 目录下:
ctags -R .
ctags -R -a -f tags /path/to/httpd-2.2.16
如果需要tags使用绝对路径,则可以使用参数:
–tag-relative=no

1. mod_php.c的写法,主要是定义一些函数,然后通过模块结构体变量挂到apache的钩子上:

php-5.3.3/sapi/apache/mod_php5.c
  1.     
    //…

        

  2.     
    // 1. 这个结构的名字php5_module是有严格要求的

        

  3.     
    module MODULE_VAR_EXPORT php5_module =

        

  4.     
    {

        

  5.     
        STANDARD_MODULE_STUFF,

        

  6.     
        php_init_handler,           /* initializer */

        

  7.     
        php_create_dir,             /* per-directory config creator */

        

  8.     
        php_merge_dir,              /* dir merger */

        

  9.     
        NULL,                       /* per-server config creator */

        

  10.     
        NULL,                       /* merge server config */

        

  11.     
        php_commands,               /* command table */

        

  12.     
        php_handlers,               /* handlers */

        

  13.     
        NULL,                       /* filename translation */

        

  14.     
        NULL,                       /* check_user_id */

        

  15.     
        NULL,                       /* check auth */

        

  16.     
        NULL,                       /* check access */

        

  17.     
        NULL,                       /* type_checker */

        

  18.     
        NULL,                       /* fixups */

        

  19.     
        NULL                        /* logger */

        

  20.     
    #if MODULE_MAGIC_NUMBER >= 19970103

        

  21.     
        , NULL                      /* header parser */

        

  22.     
    #endif

        

  23.     
    #if MODULE_MAGIC_NUMBER >= 19970719

        

  24.     
        , NULL                      /* child_init */

        

  25.     
    #endif

        

  26.     
    #if MODULE_MAGIC_NUMBER >= 19970728

        

  27.     
        , php_child_exit_handler        /* child_exit */

        

  28.     
    #endif

        

  29.     
    #if MODULE_MAGIC_NUMBER >= 19970902

        

  30.     
        , NULL                      /* post read-request *

        

  31.     
    #endif

        

  32.     
    };

        

2. apache模块结构的定义:

http-2.216/include/http_config.h
  1.     
    typedef struct module_struct module;

        

  2.     
    struct module_struct {          

        

  3.     
        /** API version, *not* module version; check that module is

        

  4.     
         * compatible with this version of the server.

        

  5.     
         */

        

  6.     
        int version;                

        

  7.     
        /** API minor version. Provides API feature milestones. Not checked

        

  8.     
         *  during module init */  

        

  9.     
        int minor_version;

        

  10.     
        /** Index to this modules structures in config vectors.  default -1*/

        

  11.     
        int module_index;

        

  12.     
     

        

  13.     
        /** The name of the module’s C file */

        

  14.     
        const char *name;

        

  15.     
        /** The handle for the DSO.  Internal use only , default null*/

        

  16.     
        void *dynamic_load_handle;

        

  17.     
                                                                                                         

        

  18.     
        /** A pointer to the next module in the list                                                    

        

  19.     
         *  @defvar module_struct *next , default null*/                                                              

        

  20.     
        struct module_struct *next;                                                                      

        

  21.     
                                                                                                         

        

  22.     
        /** Magic Cookie to identify a module structure;  It’s mainly                                    

        

  23.     
         *  important for the DSO facility (see also mod_so).  */                                        

        

  24.     
        unsigned long magic;                                                                            

        

  25.     
                                                                                                         

        

  26.     
        /** Function to allow MPMs to re-write command line arguments.  This                            

        

  27.     
         *  hook is only available to MPMs.                                                              

        

  28.     
         *  @param The process that the server is running in.                                            

        

  29.     
                                                                   

        

  30.     
         */

        

  31.     
        void (*rewrite_args) (process_rec *process);

        

  32.     
        /** Function to allow all modules to create per directory configuration

        

  33.     
         *  structures.

        

  34.     
         *  @param p The pool to use for all allocations.

        

  35.     
         *  @param dir The directory currently being processed.

        

  36.     
         *  @return The per-directory structure created

        

  37.     
         */

        

  38.     
        void *(*create_dir_config) (apr_pool_t *p, char *dir);

        

  39.     
        /** Function to allow all modules to merge the per directory configuration

        

  40.     
         *  structures for two directories.

        

  41.     
         *  @param p The pool to use for all allocations.

        

  42.     
         *  @param base_conf The directory structure created for the parent directory.

        

  43.     
         *  @param new_conf The directory structure currently being processed.

        

  44.     
         *  @return The new per-directory structure created

        

  45.     
         */                                                                                              

        

  46.     
        void *(*merge_dir_config) (apr_pool_t *p, void *base_conf, void *new_conf);                      

        

  47.     
        /** Function to allow all modules to create per server configuration                            

        

  48.     
         *  structures.                                                                                  

        

  49.     
         *  @param p The pool to use for all allocations.                                                

        

  50.     
         *  @param s The server currently being processed.                                              

        

  51.     
         *  @return The per-server structure created                                                    

        

  52.     
         */                                                                                              

        

  53.     
        void *(*create_server_config) (apr_pool_t *p, server_rec *s);                                    

        

  54.     
        /** Function to allow all modules to merge the per server configuration                          

        

  55.     
         *  structures for two servers.                                                        

        

  56.     
         *  @param p The pool to use for all allocations.

        

  57.     
         *  @param base_conf The directory structure created for the parent directory.

        

  58.     
         *  @param new_conf The directory structure currently being processed.

        

  59.     
         *  @return The new per-directory structure created                                              

        

  60.     
         */                                                                                              

        

  61.     
        void *(*merge_server_config) (apr_pool_t *p, void *base_conf,                                    

        

  62.     
                                      void *new_conf);                                                  

        

  63.     
                                                                                                         

        

  64.     
        /** A command_rec table that describes all of the directives this module                        

        

  65.     
         * defines. */                                                                                  

        

  66.     
        const command_rec *cmds;                                                                        

        

  67.     
                                                                                                         

        

  68.     
        /** A hook to allow modules to hook other points in the request processing.                      

        

  69.     
         *  In this function, modules should call the ap_hook_*() functions to                          

        

  70.     
         *  register an interest in a specific step in processing the current                            

        

  71.     
         *  request.                                                                                    

        

  72.     
         *  @param p the pool to use for all allocations                                                

        

  73.     
         */                                                                                              

        

  74.     
        void (*register_hooks) (apr_pool_t *p);                                                          

        

  75.     
    };                                        

        

3. apache动态加载模块的过程。 动态加载模块时,严格检查模块名,包括httpd.conf中的module name(如: php5_module);对应的apache模块定义所在的文件的文件名(如: mod_php5.c)也是严格检查的。

httpd-2.2.16/modules/mappers/mod_so.c
  1.     
    static const char *load_module(cmd_parms *cmd, void *dummy,

        

  2.     
                                   const char *modname, const char *filename)

        

  3.     
    {

        

  4.     
        apr_dso_handle_t *modhandle;

        

  5.     
        apr_dso_handle_sym_t modsym;

        

  6.     
        module *modp;

        

  7.     
        const char *szModuleFile = ap_server_root_relative(cmd->pool, filename);

        

  8.     
        so_server_conf *sconf;

        

  9.     
        ap_module_symbol_t *modi;

        

  10.     
    //…

        

4. Apache conf 中的配置:
LoadModule php5_module        modules/libphp5.so

5. apr 动态加载模块并获取指定符号(如:php5_module):

apr-1.4.2/dso/beos/dso.c
  1.     
     

        

  2.     
    APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym, apr_dso_handle_t *handle,

        

  3.     
                   const char *symname)

        

  4.     
    {

        

  5.     
        int err;

        

  6.     
     

        

  7.     
        if (symname == NULL)

        

  8.     
            return APR_ESYMNOTFOUND;

        

  9.     
     

        

  10.     
        err = get_image_symbol(handle->handle, symname, B_SYMBOL_TYPE_ANY,

        

  11.     
                 ressym);

        

  12.     
     

        

  13.     
        if(err != B_OK)

        

  14.     
            return APR_ESYMNOTFOUND;

        

  15.     
     

        

  16.     
        return APR_SUCCESS;

        

  17.     
    }

        

6. 一切准备完毕后:
   将模块写入已加载的模块列表中: ap_add_loaded_module(modp, cmd->pool);
   注册相关钩子函数:
     apr_pool_cleanup_register(cmd->pool, modi, unload_module, apr_pool_cleanup_null);
   Finally we need to run the configuration process for the module
     ap_single_module_configure(cmd->pool, cmd->server, modp); 

雁南飞


=========================================


Firefox 中的扩展与插件的区别

插件与扩展是完全不同的东西。

插件(Plugins/Plug-ins):

它通常是第三方应用程序提供给firefox使用的二进制文件。也就是说第三方应用程序把相关功能编译成了二进制的机器指令提供给各类浏览器,方便它们调用。
Firefox需要显示某些自身并不能显示的特定文件类型的时候,就会调用与之相关的第三方应用程序提供给firefox的插件来显示它。插件的作用也在于此。

windows下的firefox插件通常是dll格式,linux下的通常是so格式。

例 如,Adobe提供给firefox的插件“Adobe reader”使其能直接在浏览器里显示网络上的pdf文档,而Adobe提供给firefox 的另一个插件“Adobe shockwave flash”则使其能显示网页中嵌入的flash。Microsoft提供的 “Windows media player firefox plugin”使firefox能播放网页中嵌入的windows媒体(wmv、wma、 asf以及对应的播放列表格式)。

扩展(extensions):

它通常是扩展开发者为了修改或者增强firefox本身的功能而提供的一种打包格式。它通常由包含功能代码的js脚本、包含界面的xul文件以及包含皮肤的css文件和各种图像文件组成。少数特定平台下的扩展可能还会附带一些二进制文件。

扩展的格式都是xpi后缀的。其实是zip格式打包的。

常见的扩展有noscript、adblock plus等。

从形式上讲:插件是已经将代码编译成了机器指令的二进制文件,而扩展是一个源码包。
从作用上讲:插件是为了让firefox能够显示特殊格式的文件而由相应的第三方应用程序(如 windows media player 和 Adobe reader)提供的;而扩展是为了修改或增强firefox本身的功能而由扩展开发者提供的。
从字面意思上讲:插件(plug-ins)的plug是插的意思,一个由外至内的动作;扩展(extensions)的extension是延伸、扩充的意思,一个由内而外的动作。
也就是说:插件是“外部”的,而扩展是“内部”的。

Mozilla文章参考:
http://www.mozilla.org/projects/plugins/
https://developer.mozilla.org/en/Plugins
https://developer.mozilla.org/en/Extensions

快速区分PHP中的函数与结构

PHP中有些类似于函数的东西其实不是函数,如 echo exit  unset  print 等。 怎么可以快速知道这些是函数还是结构呢?

测试脚本:

执行脚本: php test.php

如果被解析为 307 就是函数,否则就可能是结构(或别的什么)。
{当然您也可以直接查手册,手册中基本会提到的}
还可以用function_exists()来测试

—————————————

 

—————————————————-

PHP 一个有意思的报错

下面的一个测试脚本中,B类中在调用A的public方法m时写错了,当做静态的方法来用了,于是A->m()中的$this被解释成了B了。

$this 究竟是谁?  test.php
  1.     
    <?php

        

  2.     
    error_reporting(2047);

        

  3.     
    class A{

        

  4.     
        private $v = 8;

        

  5.     
        public function m() {

        

  6.     
            echo $this->v;

        

  7.     
        }

        

  8.     
    }

        

  9.     
    class B{

        

  10.     
        public function __construct() {

        

  11.     
            A::m();

        

  12.     
        }

        

  13.     
    }

        

  14.     
     

        

  15.     
    new B();

        

  16.     
     

        

  17.     
    exit;

        

出错信息:
PHP Notice:  Undefined property:  B::$v in test.php on line 6

无语…

PHP 中关于资源的释放

问题:
将mysql连接或memcache连接变量直接unset()或赋值为null,连接会立即关闭吗?

测试脚本:

释放资源 test.php
  1.     
    <?php

        

  2.     
    $m = new Memcache();

        

  3.     
    $m->connect("10.55.38.18", 11211);

        

  4.     
    //unset($m);

        

  5.     
    $m = null;

        

  6.     
    sleep(20);

        

执行命令:
strace php test.php
———————————————————–
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
fcntl64(3, F_GETFL)                     = 0x2 (flags O_RDWR)
fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK)  = 0
connect(3, {sa_family=AF_INET, sin_port=htons(11211), sin_addr=inet_addr("10.55.38.18")}, 16) = -1 EINPROGRESS (Operation now in progress)
poll([{fd=3, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 1000) = 1 ([{fd=3, revents=POLLOUT}])
getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
fcntl64(3, F_SETFL, O_RDWR)             = 0
close(3)                                = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({20, 0}, 0xbfa1e114)  
————————————————————————-

测试结果:
对于一个资源,使用unset()或 设置为null,都会立即关闭连接的。
大概关闭连接的操作是在引用变为0时触发的,具体实现还没看呢。