• Linux下php动态添加扩展(续)


    接上次的php动态添加扩展http://www.cnblogs.com/stratrail/archive/2013/03/23/2976802.html

    为了实现在完成扩展的基础上的图片操作...

    思路:先将图片流存成图片,然后利用扩展库在本地打开图片再操作完再保存...

    1、下载开源库

      Jpeg库libJpeg,png库libpng,GD库 gd2 (可能需要freetypezlib)

    2、安装库(主要Jpeg库与gd库) 

      Jpeg库需要自己建目录,安装如下:

    mkdir -p /usr/local/jpeg/include
    mkdir -p /usr/local/jpeg/lib
    mkdir -p /usr/local/jpeg/bin
    mkdir -p /usr/local/jpeg/man/man1  
    ./configure --prefix=/usr/local/jpeg --enable-shared 
    #make
    #make install   

      GD库需要指定ljpeg库等地址,如下:

    ./configure --prefix=/usr/local/gd2 --with-png --with-freetype=/usr/local/freetype --with-jpeg=/usr/local/libjpeg
    #make
    #make install

      重新编译生成PHP,如下:

    ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql --with-zlib --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr/local/png --with-freetype-dir=/usr/local/freetype --enable-ftp --enable-sockets --with-gd=/usr/local/gd2 --enable-gd-native-ttf --with-ttf --enable-magic-quotes --with-iconv -enable-mbstring=all
    #make
    #make install

    3、编译处理图片的php的C扩展库

    #include"jpeglib.h"
    //打开图片
    void loadJPEG(char* path,BYTE** pData,int* nWidth,int* nHeight){   struct jpeg_decompress_struct cinfo;   struct jpeg_error_mgr jerr;   cinfo.err = jpeg_std_error(&jerr);   jpeg_create_decompress(&cinfo);   FILE *f = fopen(path,"rb");   if (f==NULL)   {    return;   }   jpeg_stdio_src(&cinfo, f);   jpeg_read_header(&cinfo, TRUE);   int nCount=cinfo.image_width*cinfo.image_height*cinfo.num_components;   BYTE* data = (BYTE*)malloc(nCount*sizeof(BYTE));//图片数据   jpeg_start_decompress(&cinfo);   JSAMPROW row_pointer[1];   *nWidth=cinfo.output_width;//图片宽   *nHeight=cinfo.output_height;//图片高   while (cinfo.output_scanline < cinfo.output_height)   {    row_pointer[0] = &data[(cinfo.output_height - cinfo.output_scanline-1)*cinfo.image_width*cinfo.num_components];    jpeg_read_scanlines(&cinfo,row_pointer ,1);   }   jpeg_finish_decompress(&cinfo);   jpeg_destroy_decompress(&cinfo);   fclose(f);   *pData=data; //图片数据 }
    //保存图片
    void saveJPEG(char* path,BYTE* pDataConv,int nWidth,int nHeight){   struct jpeg_compress_struct jcs; struct jpeg_error_mgr jem; jcs.err = jpeg_std_error(&jem); jpeg_create_compress(&jcs);   FILE* f=fopen(path,"wb"); if (f==NULL) { printf("Save file error!\n"); return ; } jpeg_stdio_dest(&jcs, f); jcs.image_width = nWidth; jcs.image_height = nHeight; jcs.input_components = 3;//3为RGB,可为1GRAY模式,space也要一起变化 jcs.in_color_space = JCS_RGB; jpeg_set_defaults(&jcs);   jpeg_set_quality (&jcs, 80, TRUE); //保存图片质量80可选 jpeg_start_compress(&jcs, TRUE); JSAMPROW row_pointer[1]; int row_stride; row_stride = jcs.image_width*3; while (jcs.next_scanline < jcs.image_height) {
         //图片数据从尾部开始写入 row_pointer[
    0] = & pDataConv[(nHeight-jcs.next_scanline) * row_stride]; jpeg_write_scanlines(&jcs, row_pointer, 1); } jpeg_finish_compress(&jcs);   jpeg_destroy_compress(&jcs);
      fclose(f);
    }
    char* Gray(char* path){
      int nWidth,nHeight;
      BYTE* pData=NULL;
      loadJPEG(path,&pData,&nWidth,&nHeight); 
      if(pData==NULL)
        return "failed";
      ChangToGray(pData,nWidth,nHeight);
      saveJPEG(path,pData,nWidth,nHeight); 
      
    return "success";
    }

    编译时候同上一篇类似,除编译命令加上-ljpeg指明调用库 用到算法库需要另外加-lm

    与之前对应的在最后生成php扩展库时编译命令也要加上-lJjpeg,如下:

    $ make LDFLAGS="-lgray -lJjpeg" //调用一开始放入配置的动态链接库与ljpeg库
    $ make install                     //这时会编译出 gary/modules/gray.so 

    4.测试数据
      $ gedit $apache2/htdocs/test.php  //编写测试php文件 

     <?php
        echo gray("122.jpg");
      ?>

    在浏览器调用后,浏览器输出success.

    查看图片变成灰度图.

  • 相关阅读:
    zbb20181207 springboot @ConfigurationProperties使用
    zbb20181206 logback,lombok 默认日志logback配置解析
    Spring Boot (8) 全局异常处理
    Spring Boot (7) JdbcTemplate访问数据库
    Spring Boot (6) Spring Data JPA
    Spring Boot (4) 静态页面和Thymeleaf模板
    Spring Boot (3) 热部署devtools
    Spring Boot (2) Restful风格接口
    Spring Boot (1) 构建第一个Spring Boot工程
    idea使用maven搭建ssm框架实现登陆商品增删改查
  • 原文地址:https://www.cnblogs.com/stratrail/p/3035688.html
Copyright © 2020-2023  润新知