Home  >  Article  >  Backend Development  >  Using C to implement the PHP extension Image_Tool and the use of common image processing tool classes

Using C to implement the PHP extension Image_Tool and the use of common image processing tool classes

高洛峰
高洛峰Original
2017-01-16 14:42:081346browse

1. Class Documentation Description

class Image_Tool{
    /**
     * 构造Image_Tool对象
     * @param string|array $img_data
     * $img_data可以为图片的路径
     */
 function __construct($img_data="");

 /**
  * 添加文字注解,或用于文字水印
  * @access public
  * @param string $txt UTF8编码的文本
  * @param float $opacity 设置透明度
  * @param constant $gravity 
  * 设置文字摆放位置:
  * NorthWest,North,NorthEast,West, Center,East,SouthWest,South,SouthEast,Static
  * @param array $font 字体数组可以设置如下属性:
  * name,常量,字体名称,如果需要添加中文注解,请使用中文字体,否则中文会显示乱码。
     *        支持的字体:SimSun(宋体,默认)、SimKai(楷体)、SimHei(正黑)、MicroHei(微米黑)、Arial
     * weight,字体宽度,int
     * size,字体大小,int
     * color,字体颜色,例如:"blue", "#0000ff", "rgb(0,0,255)"等,默认为"black";
     * @return boolean
  */
 function annotate($txt, $opacity, $gravity, array $font);

 /**
  * 将对象的数据重新初始化,用于多次重用一个Image_Tool对象
  * @access public
  * @return void
  */
 function clean();

 *
  * 图片合成,可以进行多张图片的合成,也可以做图片水印用
  * @access public
  * @param int $src_img 合成的目标图片,可以为ImageTool对象或图片二进制数据
  * @param int $x 合成在画布的X坐标
  * @param string $y 合成在画布的Y坐标
  * @return boolean
  * 
 function composite($src_img, $x, $y);

 /**
  * 返回错误信息
  * @access public
  * @return string
  */
 function errmsg();

 /**
  * 返回错误编号
  * @access public
  * @return int
  */
 function errcode();

 /**
  * 进行图片处理操作
  * @access public
  * @param string $format
  * @param boolean $display
  * @return boolean|string 若设置$display为true,返回void,否则返回图片二进制数据。失败时返回false
  */
 function exec($format, $display=false);

 /**
  * 水平翻转
  * @access public
  * @return boolean
  */
 function flipH();

 /**
  * 垂直翻转
  * @access public
  * @return boolean
  */
 function flipV();

 /**
  * 取得图片属性
  * @access public
  * @return array|boolean 错误返回false
  */
 function getImageAttr();

 /**
  * 去噪点,改善图片质量,通常用于exec之前
  * @access public
  * @return boolean
  */
 function improve();

 /**
  * 缩放图片,只指定width或者height时,将进行等比缩放
  * @access public
  * @param int $width
  * @param int $height
  * @param boolean $thumbnail 是否清除图片附加信息
  * @return boolean
  */
 function resize($width, $height, $thumbnail=true);

 /**
  * 按比例缩放.1为原大小
  * @access public
  * @param float $ratio
  * @param boolean $thumbnail 是否清除图片附加信息
  * @return boolean
  */
 function resizeRatio($ratio, $thumbnail=true);

 /**
  * 顺时针旋转图片
  * @access public
  * @param int $degree 旋转度数(0 - 360)
  * @return boolean
  */
 function rotate($degree=90);

 /**
  * 设置要处理的图片二进制数据
  * @access public
  * @param string $img_blob
  * @return boolean
  */
 function setData($img_blob);
}

3. Extension Implementation
1.php_fetch_url.h

/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2012 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/
/* $Id$ */
#ifndef PHP_IMAGE_TOOL_H
#define PHP_IMAGE_TOOL_H
extern zend_module_entry image_tool_module_entry;
#define phpext_image_tool_ptr &image_tool_module_entry
#ifdef PHP_WIN32
# define PHP_IMAGE_TOOL_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define PHP_IMAGE_TOOL_API __attribute__ ((visibility("default")))
#else
# define PHP_IMAGE_TOOL_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
#define FETCH_THIS Z_OBJCE_P(getThis()), getThis()
#define IMAGETOOL_MAGICKWAND_RSRC_NAME "MagickWand"
#define IMAGETOOL_PIXELWAND_RSRC_NAME "PixelWand"
#define IMAGETOOL_NORTHWEST 1
#define IMAGETOOL_NORTH     2
#define IMAGETOOL_NORTHEAST 3
#define IMAGETOOL_WEST      4
#define IMAGETOOL_CENTER    5
#define IMAGETOOL_EAST      6
#define IMAGETOOL_SOUTHWEST 7
#define IMAGETOOL_SOUTH     8
#define IMAGETOOL_SOUTHEAST 9
#define IMAGETOOL_STATIC    10
#define IMAGETOOL_TOP_LEFT      1
#define IMAGETOOL_TOP_CENTER    2
#define IMAGETOOL_TOP_RIGHT     3
#define IMAGETOOL_CENTER_LEFT   4
#define IMAGETOOL_CENTER_CENTER 5
#define IMAGETOOL_CENTER_RIGHT  6
#define IMAGETOOL_BOTTOM_LEFT   7
#define IMAGETOOL_BOTTOM_CENTER 8
#define IMAGETOOL_BOTTOM_RIGHT  9
#define GET_MAGICK_WAND(zval, magick_wand) zval = zend_read_property(FETCH_THIS, ZEND_STRL("magick_wand"), 0 TSRMLS_CC);\
        ZEND_FETCH_RESOURCE_NO_RETURN(magick_wand, MagickWand*, &zval, -1, IMAGETOOL_MAGICKWAND_RSRC_NAME, le_image_wand);
PHP_MINIT_FUNCTION(image_tool);
PHP_MSHUTDOWN_FUNCTION(image_tool);
PHP_RINIT_FUNCTION(image_tool);
PHP_RSHUTDOWN_FUNCTION(image_tool);
PHP_MINFO_FUNCTION(image_tool);

#ifdef ZTS
#define IMAGE_TOOL_G(v) TSRMG(image_tool_globals_id, zend_image_tool_globals *, v)
#else
#define IMAGE_TOOL_G(v) (image_tool_globals.v)
#endif
#endif /* PHP_IMAGE_TOOL_H */

More related to the use of C to implement PHP extension Image_Tool commonly used image processing tool classes Please pay attention to the PHP Chinese website for articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn