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