imagemagick

ImageMagick之圖片裁剪 php

convert 原始圖片 -crop widthxheight+x+y 目標圖片

imagemagick的convert命令經過crop參數,能夠把一幅大圖片分紅若干塊大小同樣的圖片,同時也能夠在大圖上截取一塊圖片來。命令格式爲 html

其中widthxheight是目標圖片的尺寸,+x+y是原始圖片的座標點,這兩組值至少要出現一組,也能夠同時存在。另外該命令也可以使用gravity來從新定義座標系統。關於更多gravity的信息
this

convert src.jpg -crop 100x100 dest.jpg

假設src.jpg的大小是300x200,執行命令後將獲得名爲dest-0.jpg、dest-1.jpg...dest-5.jpg
的6張大小爲100x100的小圖片。注意若是尺寸不是目標圖片的整數倍,那麼右邊緣和下

利用ImageMagicK的convert命令,能很方便的實現圖片的放大縮小,能夠進行等比例縮放,也能縮放到指定的固定大小。縮放的參數resize,由它來指定縮放後圖片的寬高,好比「200×100」。 url

  • 等比縮放 例如把圖片a.jpg縮放到200×100的尺寸,能夠用命令:


convert -resize 200x100 src.jpg dest.jpg

http://www.netingcn.com/category/imagemagick spa


下面是我本身寫的類 .net

<?php
class imagemagick{



	function new_name($filename){
		$ext = pathinfo($filename);
		$ext = strtolower($ext['extension']);
		if ($ext=='jpg'||$ext=='gif'||$ext=='png')
		{
			$name = basename($filename,$ext);
			$name = md5($name.time()).'.'.$ext;
			return $name;
		}
		else
		{
			return;
		}
	}	
					//圖片類型
			function findImgType($file) {
				$img_arr = stristr($file,'.');
				$type = explode('.',$img_arr);
				return end($type);  
			}
			
			function movefile($oldurl, $newurl){
				set_time_limit(0);
				$command = "convert ".$oldurl.$newurl;
				exec($command);				 
			}
			
			function upFile($size, $oldurl, $newurl){
				set_time_limit(0);
				$command = "convert ".$oldurl." -resize '".$size."' ".$newurl;
				exec($command);				 
			}

		//論壇列表頁用的縮略圖生成方法		
		function thumb($width,$height, $oldurl,$newurl,$name){
			set_time_limit(0);			
			if(strtolower($this->findImgType($oldurl))=="gif"){
				$name = basename($name,".gif");
				$name = $name.'.jpg';			 					
				$command ="convert ".$oldurl."[0] -resize x".$height." -gravity center -crop ".$width."x".$height."+0+0 ".$newurl.$name;
			}  
			else  
			{
				$command ="convert ".$oldurl." -gravity center -crop ".$width."x".$height."+0+0 ".$newurl."".$name; 
			}
			exec($command);
			return $newurl.$name;
		} 
		
		//標準圖
				function resizeImage($width,$height, $oldurl,$newurl,$name)
		{
		set_time_limit(0); 
		$command = "convert ".$oldurl." -resize '".$width."x".$height.">' ".$newurl."".$name;		
		  //$command = "convert ".$oldurl." -resize ".$width."x	".$height." ".$newurl."".$name;
		exec($command);
		}
		
		function bbs_resizeImage($width,$oldurl,$newurl)
		{
		set_time_limit(0);  
		$command = "convert ".$oldurl." -resize '".$width."x>' ".$newurl;	

		exec($command);
		}
		
		
		
		//頭像16*16
		function touxiang_thumb($width,$height, $oldurl,$newurl,$name)
		{
			set_time_limit(0);
			if(strtolower($this->findImgType($oldurl))=="gif"&&$width=='16'&&$height=='16')
			{			
				$name = basename($name,".gif");
				$name = $name.'.jpg';			
				$command ="convert ".$oldurl."[0] -resize ".$width."x".$height." ".$newurl."".$name;
			}
			else
			{				
				$command = "convert ".$oldurl." -resize ".$width."x".$height." ".$newurl."".$name;	
				
			}
			exec($command);
			return '/'.$newurl.$name;
		} 
		
		//頭像標準圖和大圖
		function touxiang_resize($width,$height,$x1,$y1,$oldurl,$newurl,$name)
		{ 
		$command ="convert ".$oldurl." -crop ".$width."x".$height."+".$x1."+".$y1." ".$newurl."".$name; 
		exec($command);
		return $newurl.$name;
		}

		
			function resizeImage2($width,$height, $oldurl,$newurl)
		{
		set_time_limit(0); 
		//$command = "convert ".$oldurl." -resize ".$width."x".$height."> ".$newurl."/".$name;		
		  $command = "convert ".$oldurl." -resize ".$width."x".$height." ".$newurl;
		  $command ="convert ".$oldurl." -resize  x".$height." -gravity center -crop ".$width."x".$height."+0+0 ".$newurl;
		exec($command);
		}

		
		
										
}
?>
相關文章
相關標籤/搜索