C实现PHP扩展《Image_Tool》图片常用处理工具类

该扩展是基于ImageMagick基础实现的,图片操作调用的是ImageMagick API。

一、类文档说明

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);
}

二、使用案例

$img1 = new Image_Tool("C:/Users/Administrator/Desktop/1.jpg");
$font = array('name'=>'Simsun', 'color'=>'red', 'size'=>20, 'weight'=>900);
$img1->annotate('Good Bye!', 0.1, IMAGETOOL_NORTHWEST, $font); //打上文字水印
// $img1->rotate(90);
$img1->flipV();//垂直翻转
// $img1->resize(250, 250, 0);
$img1->resizeRatio(0.5, 1);
$img_arr = $img1->getImageAttr();//图片属性 宽、高、类型
$img1->improve();//除噪点


$img2 = new Image_Tool();
$img2->setData(file_get_contents("C:/Users/Administrator/Desktop/3.png"));

$background_img = new Image_Tool("C:/Users/Administrator/Desktop/2.png");
$background_img->composite($img1, 200, 100); //以background_img为画布,基于左上角0,0坐标开始向右200、向下100像素将图片img1合成
$background_img->composite($img2, 0, 0); //将background_img与img1合成后的图片,基于左上角0,0点合成img2
$background_img->exec("C:/Users/Administrator/Desktop/composite.jpg");//生成三张图片合并后的新图片

三、扩展实现

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          |
  | [email protected] 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(dl
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值