搜索
首页后端开发php教程PHP图片处理扩展php_imagick类库使用

一个好用的封装PHP扩展php_imagick的类库

imagick提供了极其丰富的处理图片方法,下面介绍一个封装php_imagick的类库:

class lib_image_imagick
{
	private $image = null;
	private $type = null;
 
	// 构造函数
	public function __construct(){}
 
 
	// 析构函数
	public function __destruct()
	{
	    if($this->image!==null) $this->image->destroy(); 
	}
 
	// 载入图像
	public function open($path)
	{
		$this->image = new Imagick( $path );
		if($this->image)
		{
		    $this->type = strtolower($this->image->getImageFormat());
		}
		return $this->image;
	}
	
	public function cropimage($width=0, $height=0){
		if($width<=0 || $height<=0) return;
		$this->image->cropThumbnailImage($width, $height);
	}
	
	public function thumbnailImage($width=0, $height=0){
		if($width<=0) return;
		$this->image->thumbnailImage($width, NULL);
	}
 
	public function crop($x=0, $y=0, $width=null, $height=null)
	{
	    if($width==null) $width = $this->image->getImageWidth()-$x;
	    if($height==null) $height = $this->image->getImageHeight()-$y;
	    if($width<=0 || $height<=0) return;
	    
	    if($this->type==&#39;gif&#39;)
	    {
            $image = $this->image;
	        $canvas = new Imagick();
	        
        	$images = $image->coalesceImages();
    	    foreach($images as $frame){
    	        $img = new Imagick();
    	        $img->readImageBlob($frame);
                $img->cropImage($width, $height, $x, $y);
 
                $canvas->addImage( $img );
                $canvas->setImageDelay( $img->getImageDelay() );
                $canvas->setImagePage($width, $height, 0, 0);
            }
            
            $image->destroy();
	        $this->image = $canvas;
	    }
	    else
	    {
	        $this->image->cropImage($width, $height, $x, $y);
	    }
	}
 
	/*
	* 更改图像大小
	$fit: 适应大小方式
	&#39;force&#39;: 把图片强制变形成 $width X $height 大小
	&#39;scale&#39;: 按比例在安全框 $width X $height 内缩放图片, 输出缩放后图像大小 不完全等于 $width X $height
	&#39;scale_fill&#39;: 按比例在安全框 $width X $height 内缩放图片,安全框内没有像素的地方填充色, 使用此参数时可设置背景填充色 $bg_color = array(255,255,255)(红,绿,蓝, 透明度) 透明度(0不透明-127完全透明))
	其它: 智能模能 缩放图像并载取图像的中间部分 $width X $height 像素大小
	$fit = &#39;force&#39;,&#39;scale&#39;,&#39;scale_fill&#39; 时: 输出完整图像
	$fit = 图像方位值 时, 输出指定位置部分图像 
	字母与图像的对应关系如下:
	
	north_west   north   north_east
	
	west         center        east
	
	south_west   south   south_east
	
	*/
	public function resize_to($width = 100, $height = 100, $fit = &#39;center&#39;, $fill_color = array(255,255,255,0) )
	{
	    
	    switch($fit)
	    {
	        case &#39;force&#39;:
        	    if($this->type==&#39;gif&#39;)
        	    {
        	        $image = $this->image;
        	        $canvas = new Imagick();
        	        
        	        $images = $image->coalesceImages();
            	    foreach($images as $frame){
            	        $img = new Imagick();
            	        $img->readImageBlob($frame);
                        $img->thumbnailImage( $width, $height, false );
 
                        $canvas->addImage( $img );
                        $canvas->setImageDelay( $img->getImageDelay() );
                    }
                    $image->destroy();
	                $this->image = $canvas;
        	    }
        	    else
        	    {
        	        $this->image->thumbnailImage( $width, $height, false );
        	    }
	            break;
	        case &#39;scale&#39;:
	            if($this->type==&#39;gif&#39;)
        	    {
        	        $image = $this->image;
        	        $images = $image->coalesceImages();
        	        $canvas = new Imagick();
            	    foreach($images as $frame){
            	        $img = new Imagick();
            	        $img->readImageBlob($frame);
                        $img->thumbnailImage( $width, $height, true );
 
                        $canvas->addImage( $img );
                        $canvas->setImageDelay( $img->getImageDelay() );
                    }
                    $image->destroy();
	                $this->image = $canvas;
        	    }
        	    else
        	    {
        	        $this->image->thumbnailImage( $width, $height, true );
        	    }
	            break;
	        case &#39;scale_fill&#39;:
	            $size = $this->image->getImagePage(); 
	            $src_width = $size[&#39;width&#39;];
	            $src_height = $size[&#39;height&#39;];
	            
                $x = 0;
                $y = 0;
                
                $dst_width = $width;
                $dst_height = $height;
 
	    		if($src_width*$height > $src_height*$width)
				{
					$dst_height = intval($width*$src_height/$src_width);
					$y = intval( ($height-$dst_height)/2 );
				}
				else
				{
					$dst_width = intval($height*$src_width/$src_height);
					$x = intval( ($width-$dst_width)/2 );
				}
 
                $image = $this->image;
                $canvas = new Imagick();
                
                $color = &#39;rgba(&#39;.$fill_color[0].&#39;,&#39;.$fill_color[1].&#39;,&#39;.$fill_color[2].&#39;,&#39;.$fill_color[3].&#39;)&#39;;
        	    if($this->type==&#39;gif&#39;)
        	    {
        	        $images = $image->coalesceImages();
            	    foreach($images as $frame)
            	    {
            	        $frame->thumbnailImage( $width, $height, true );
 
            	        $draw = new ImagickDraw();
                        $draw->composite($frame->getImageCompose(), $x, $y, $dst_width, $dst_height, $frame);
 
                        $img = new Imagick();
                        $img->newImage($width, $height, $color, &#39;gif&#39;);
                        $img->drawImage($draw);
 
                        $canvas->addImage( $img );
                        $canvas->setImageDelay( $img->getImageDelay() );
                        $canvas->setImagePage($width, $height, 0, 0);
                    }
        	    }
        	    else
        	    {
        	        $image->thumbnailImage( $width, $height, true );
        	        
        	        $draw = new ImagickDraw();
                    $draw->composite($image->getImageCompose(), $x, $y, $dst_width, $dst_height, $image);
                    
        	        $canvas->newImage($width, $height, $color, $this->get_type() );
                    $canvas->drawImage($draw);
                    $canvas->setImagePage($width, $height, 0, 0);
        	    }
        	    $image->destroy();
	            $this->image = $canvas;
	            break;
			default:
				$size = $this->image->getImagePage(); 
			    $src_width = $size[&#39;width&#39;];
	            $src_height = $size[&#39;height&#39;];
	            
                $crop_x = 0;
                $crop_y = 0;
                
                $crop_w = $src_width;
                $crop_h = $src_height;
                
	    	    if($src_width*$height > $src_height*$width)
				{
					$crop_w = intval($src_height*$width/$height);
				}
				else
				{
				    $crop_h = intval($src_width*$height/$width);
				}
                
			    switch($fit)
	            {
			    	case &#39;north_west&#39;:
			    	    $crop_x = 0;
			    	    $crop_y = 0;
			    	    break;
        			case &#39;north&#39;:
        			    $crop_x = intval( ($src_width-$crop_w)/2 );
        			    $crop_y = 0;
        			    break;
        			case &#39;north_east&#39;:
        			    $crop_x = $src_width-$crop_w;
        			    $crop_y = 0;
        			    break;
        			case &#39;west&#39;:
        			    $crop_x = 0;
        			    $crop_y = intval( ($src_height-$crop_h)/2 );
        			    break;
        			case &#39;center&#39;:
        			    $crop_x = intval( ($src_width-$crop_w)/2 );
        			    $crop_y = intval( ($src_height-$crop_h)/2 );
        			    break;
        			case &#39;east&#39;:
        			    $crop_x = $src_width-$crop_w;
        			    $crop_y = intval( ($src_height-$crop_h)/2 );
        			    break;
        			case &#39;south_west&#39;:
        			    $crop_x = 0;
        			    $crop_y = $src_height-$crop_h;
        			    break;
        			case &#39;south&#39;:
        			    $crop_x = intval( ($src_width-$crop_w)/2 );
        			    $crop_y = $src_height-$crop_h;
        			    break;
        			case &#39;south_east&#39;:
        			    $crop_x = $src_width-$crop_w;
        			    $crop_y = $src_height-$crop_h;
        			    break;
        			default:
        			    $crop_x = intval( ($src_width-$crop_w)/2 );
        			    $crop_y = intval( ($src_height-$crop_h)/2 );
	            }
	            
	            $image = $this->image;
	            $canvas = new Imagick();
	            
	    	    if($this->type==&#39;gif&#39;)
        	    {
        	        $images = $image->coalesceImages();
            	    foreach($images as $frame){
            	        $img = new Imagick();
            	        $img->readImageBlob($frame);
                        $img->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
                        $img->thumbnailImage( $width, $height, true );
                        
                        $canvas->addImage( $img );
                        $canvas->setImageDelay( $img->getImageDelay() );
                        $canvas->setImagePage($width, $height, 0, 0);
                    }
        	    }
        	    else
        	    {
        	        $image->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
        	        $image->thumbnailImage( $width, $height, true );
        	        $canvas->addImage( $image );
        	        $canvas->setImagePage($width, $height, 0, 0);
        	    }
        	    $image->destroy();
	            $this->image = $canvas;
	    }
	    
	}
	
 
	
 
	// 添加水印图片
	public function add_watermark($path, $x = 0, $y = 0)
	{
        $watermark = new Imagick($path);
        $draw = new ImagickDraw();
        $draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getimageheight(), $watermark);
 
	    if($this->type==&#39;gif&#39;)
	    {
	        $image = $this->image;
            $canvas = new Imagick();
        	$images = $image->coalesceImages();
    	    foreach($image as $frame)
    	    {
                $img = new Imagick();
    	        $img->readImageBlob($frame);
                $img->drawImage($draw);
                
                $canvas->addImage( $img );
                $canvas->setImageDelay( $img->getImageDelay() );
            }
            $image->destroy();
	        $this->image = $canvas;
	    }
	    else
	    {
	        $this->image->drawImage($draw);
	    }
	}
 
	
	// 添加水印文字
	public function add_text($text, $x = 0 , $y = 0, $angle=0, $style=array())
	{
        $draw = new ImagickDraw();
        if(isset($style[&#39;font&#39;])) $draw->setFont($style[&#39;font&#39;]);
        if(isset($style[&#39;font_size&#39;])) $draw->setFontSize($style[&#39;font_size&#39;]);
	    if(isset($style[&#39;fill_color&#39;])) $draw->setFillColor($style[&#39;fill_color&#39;]);
	    if(isset($style[&#39;under_color&#39;])) $draw->setTextUnderColor($style[&#39;under_color&#39;]);
	    
	    if($this->type==&#39;gif&#39;)
	    {
    	    foreach($this->image as $frame)
    	    {
    	        $frame->annotateImage($draw, $x, $y, $angle, $text);
    	    }
	    }
	    else
	    {
	        $this->image->annotateImage($draw, $x, $y, $angle, $text);
	    }
	}
	
	
	// 保存到指定路径
	public function save_to( $path )
	{
	    if($this->type==&#39;gif&#39;)
	    {
	        $this->image->writeImages($path, true);
	    }
	    else
	    {
	        $this->image->writeImage($path);
	    }
	}
 
	// 输出图像
	public function output($header = true)
	{
	    if($header) header(&#39;Content-type: &#39;.$this->type);
	    echo $this->image->getImagesBlob();		
	}
 
	
	public function get_width()
	{
        $size = $this->image->getImagePage(); 
        return $size[&#39;width&#39;];
	}
	
	public function get_height()
	{
	    $size = $this->image->getImagePage(); 
        return $size[&#39;height&#39;];
	}
 
	// 设置图像类型, 默认与源类型一致
	public function set_type( $type=&#39;png&#39; )
	{
	    $this->type = $type;
        $this->image->setImageFormat( $type );
	}
 
	// 获取源图像类型
	public function get_type()
	{
		return $this->type;
	}
 
 
	// 当前对象是否为图片
	public function is_image()
	{
		if( $this->image )
			return true;
		else
			return false;
	}
	
 
 
	public function thumbnail($width = 100, $height = 100, $fit = true){ $this->image->thumbnailImage( $width, $height, $fit );} // 生成缩略图 $fit为真时将保持比例并在安全框 $width X $height 内生成缩略图片
 
	/*
	添加一个边框
	$width: 左右边框宽度
	$height: 上下边框宽度
	$color: 颜色: RGB 颜色 &#39;rgb(255,0,0)&#39; 或 16进制颜色 &#39;#FF0000&#39; 或颜色单词 &#39;white&#39;/&#39;red&#39;...
	*/
	public function border($width, $height, $color=&#39;rgb(220, 220, 220)&#39;)
	{
		$color=new ImagickPixel();
		$color->setColor($color);
		$this->image->borderImage($color, $width, $height);
	}
	
	public function blur($radius, $sigma){$this->image->blurImage($radius, $sigma);} // 模糊
	public function gaussian_blur($radius, $sigma){$this->image->gaussianBlurImage($radius, $sigma);} // 高斯模糊
	public function motion_blur($radius, $sigma, $angle){$this->image->motionBlurImage($radius, $sigma, $angle);} // 运动模糊
	public function radial_blur($radius){$this->image->radialBlurImage($radius);} // 径向模糊
 
	public function add_noise($type=null){$this->image->addNoiseImage($type==null?imagick::NOISE_IMPULSE:$type);} // 添加噪点
	
	public function level($black_point, $gamma, $white_point){$this->image->levelImage($black_point, $gamma, $white_point);} // 调整色阶
	public function modulate($brightness, $saturation, $hue){$this->image->modulateImage($brightness, $saturation, $hue);} // 调整亮度、饱和度、色调
 
	public function charcoal($radius, $sigma){$this->image->charcoalImage($radius, $sigma);} // 素描
	public function oil_paint($radius){$this->image->oilPaintImage($radius);} // 油画效果
	
	public function flop(){$this->image->flopImage();} // 水平翻转
	public function flip(){$this->image->flipImage();} // 垂直翻转
 
}


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP:服务器端脚本语言的简介PHP:服务器端脚本语言的简介Apr 16, 2025 am 12:18 AM

PHP是一种服务器端脚本语言,用于动态网页开发和服务器端应用程序。1.PHP是一种解释型语言,无需编译,适合快速开发。2.PHP代码嵌入HTML中,易于网页开发。3.PHP处理服务器端逻辑,生成HTML输出,支持用户交互和数据处理。4.PHP可与数据库交互,处理表单提交,执行服务器端任务。

PHP和网络:探索其长期影响PHP和网络:探索其长期影响Apr 16, 2025 am 12:17 AM

PHP在过去几十年中塑造了网络,并将继续在Web开发中扮演重要角色。1)PHP起源于1994年,因其易用性和与MySQL的无缝集成成为开发者首选。2)其核心功能包括生成动态内容和与数据库的集成,使得网站能够实时更新和个性化展示。3)PHP的广泛应用和生态系统推动了其长期影响,但也面临版本更新和安全性挑战。4)近年来的性能改进,如PHP7的发布,使其能与现代语言竞争。5)未来,PHP需应对容器化、微服务等新挑战,但其灵活性和活跃社区使其具备适应能力。

为什么要使用PHP?解释的优点和好处为什么要使用PHP?解释的优点和好处Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

揭穿神话:PHP真的是一种死语吗?揭穿神话:PHP真的是一种死语吗?Apr 16, 2025 am 12:15 AM

PHP没有死。1)PHP社区积极解决性能和安全问题,PHP7.x提升了性能。2)PHP适合现代Web开发,广泛用于大型网站。3)PHP易学且服务器表现出色,但类型系统不如静态语言严格。4)PHP在内容管理和电商领域仍重要,生态系统不断进化。5)通过OPcache和APC等优化性能,使用OOP和设计模式提升代码质量。

PHP与Python辩论:哪个更好?PHP与Python辩论:哪个更好?Apr 16, 2025 am 12:03 AM

PHP和Python各有优劣,选择取决于项目需求。1)PHP适合Web开发,易学,社区资源丰富,但语法不够现代,性能和安全性需注意。2)Python适用于数据科学和机器学习,语法简洁,易学,但执行速度和内存管理有瓶颈。

PHP的目的:构建动态网站PHP的目的:构建动态网站Apr 15, 2025 am 12:18 AM

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP:处理数据库和服务器端逻辑PHP:处理数据库和服务器端逻辑Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

您如何防止PHP中的SQL注入? (准备的陈述,PDO)您如何防止PHP中的SQL注入? (准备的陈述,PDO)Apr 15, 2025 am 12:15 AM

在PHP中使用预处理语句和PDO可以有效防范SQL注入攻击。1)使用PDO连接数据库并设置错误模式。2)通过prepare方法创建预处理语句,使用占位符和execute方法传递数据。3)处理查询结果并确保代码的安全性和性能。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。