wuminghuo
新手上路

初级会员
UID 68697
精华
0
积分 38
帖子 62
金钱 38 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-2-18 来自 郑州--北京
状态 离线
|
[推荐阅读] 关于session跨页面传值问题
我这里有个办法,是先按照最大的比例缩略(例如:要求50*50的图,原图200*600,就先缩略为50*150)
,然后再切去多余的部分,这样最大程度的保留了图片,而且不失真。
这里用的是ImageMagick,效率不错,用GD改改也可以的
<?php class cutImgSvc {/*{{{*/ const WIDTH = '80'; const HEIGHT = '80'; public static function cut($url,$articleID) {/*{{{*/ if(empty($url) || empty($articleID)) return false; $imgData = self::curlExec($url); self::saveImg($imgData,$articleID); self::makeSmallPic($articleID.".jpg",$articleID."_s.jpg",self::WIDTH,self::HEIGHT); }/*}}}*/ static function curlExec($url, $timeout=1) { /*{{{*/ $ch = curl_init(); curl_setopt($ch, CURLOPT_VERBOSE, 1 ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_URL, $url); $r = curl_exec($ch); curl_close($ch); return $r; }/*}}}*/ static function saveImg($cutData,$articleID) {/*{{{*/ self::writeFile($cutData,$articleID.".jpg"); }/*}}}*/ static function writeFile($string,$fname) {/*{{{*/ $fp = fopen($fname,'w'); if($fp) { if(flock($fp, LOCK_EX)) { fwrite($fp, $string); flock($fp, LOCK_UN); return true; } } return false; }/*}}}*/ static function makeSmallPic($image,$small_pic,$thumbw,$thumbh) {/*{{{*/ $size = getimagesize($image); // 获取原图大小 $orig_w=$size[0]; $orig_h=$size[1]; $width=$thumbw; $height=$thumbh; $source_image = $image; $resize_image = $small_pic; $commands = ''; if($orig_w>$orig_h) { $resized_w = ($height/$orig_h) * $orig_w; $move = ($resized_w - $width); if($move < 0) { $commands .= ' -resize "'.$width.'x"'; $commands .= ' -crop "'.$width.'x'.$height.'+0+0"'; } else { $commands .= ' -geometry "x'.$height.'"'; $commands .= ' -crop "'.$width.'x'.$height.'+'.round($move/2).'+0"'; $commands .= ' +profile "*"'; } } else { $resized_h = ($width/$orig_w) * $orig_h; $move = ($resized_h - $height); if($move < 0) { $commands .= ' -resize "x'.$height.'"'; $commands .= ' -crop "'.$width.'x'.$height.'+0+0"'; } else { $commands .= ' -geometry "'.$width.'x"'; $commands .= ' -crop "'.$width.'x'.$height.'+0+'.round($move/2).'"'; $commands .= ' +profile "*"'; } } $exec_str = "/usr/local/bin/convert".$commands." ".$source_image." ".$resize_image; $r = exec($exec_str); }/*}}}*/ }/*}}}*/ class ImageSvc {/*{{{*/ public $width = 200; public $height = 200; public $fileDir = "./"; const EXT = 'thum'; public function __construct($width,$height,$fileDir) {/*{{{*/ $this->width = $width; $this->height = $height; $this->filedir = $fileDir; }/*}}}*/ static function instance($width=200,$height=200,$fileDir="./") {/*{{{*/ return new ImageSvc($width,$height,$fileDir); }/*}}}*/ public function getThumImgByUrl($imgUrl,$imgID) {/*{{{*/ if(empty($imgUrl) || empty($imgID)) return false; $sourceImg = $this->filedir.$imgID.".jpg"; $thumImg = $this->filedir.$imgID."_".self::EXT."_".$this->width."_".$this->height.".jpg"; if(file_exists($thumImg)) return ''; $r = self::saveImg($imgUrl,$sourceImg); if(!$r) { print('no img file'); return false; } magickImgSvc::cutImage($sourceImg,$thumImg,$this->width,$this->height); }/*}}}*/ static function saveImg($imgUrl,$imgFile) {/*{{{*/ $imgData = self::curlExec($imgUrl); return self::writeFile($imgData,$imgFile); }/*}}}*/ static function curlExec($url, $timeout=1) { /*{{{*/ $ch = curl_init(); curl_setopt($ch, CURLOPT_VERBOSE, 1 ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_URL, $url); $r = curl_exec($ch); curl_close($ch); return $r; }/*}}}*/ static function writeFile($string,$fname) {/*{{{*/ $fp = fopen($fname,'w'); if($fp) { if(flock($fp, LOCK_EX)) { fwrite($fp, $string); flock($fp, LOCK_UN); return true; } } return false; }/*}}}*/ }/*}}}*/ class magickImgSvc {/*{{{*/ const IMAGEMAGICK = '/usr/local/bin'; static function cutImage($source_image,$resize_image,$width,$height) {/*{{{*/ $size = getimagesize($source_image); $orig_w=$size[0]; $orig_h=$size[1]; $commands = ''; if($orig_w>$orig_h) { $resized_w = ($height/$orig_h) * $orig_w; $move = ($resized_w - $width); if($move < 0) { $commands .= ' -resize "'.$width.'x"'; $commands .= ' -crop "'.$width.'x'.$height.'+0+0"'; } else { $commands .= ' -geometry "x'.$height.'"'; $commands .= ' -crop "'.$width.'x'.$height.'+'.round($move/2).'+0"'; $commands .= ' +profile "*"'; } } else { $resized_h = ($width/$orig_w) * $orig_h; $move = ($resized_h - $height); if($move < 0) { $commands .= ' -resize "x'.$height.'"'; $commands .= ' -crop "'.$width.'x'.$height.'+0+0"'; } else { $commands .= ' -geometry "'.$width.'x"'; $commands .= ' -crop "'.$width.'x'.$height.'+0+'.round($move/2).'"'; $commands .= ' +profile "*"'; } } $exec_str = self::IMAGEMAGICK."/convert".$commands." ".$source_image." ".$resize_image; exec($exec_str); }/*}}}*/ static function thumbnail($source_image,$resize_image,$width,$height) {/*{{{*/ $str = self::IMAGEMAGICK."/convert -geometry ".$width."x".$height." ".$source_image." ".$resize_image; exec($str); }/*}}}*/ } /*}}}*/
$url[] = "http://forum.ubuntu.org.cn/files/thumbs/t_102_1920_14454.jpg"; $url[] = "http://forum.ubuntu.org.cn/files/thumbs/t_124215196_05e1b29c11_b_41530.jpg";
$width = 150; $height = 113; $imgSvc = ImageSvc::instance($width,$height,"./"); foreach($url as $key=>$img) {/*{{{*/ $imgID = $key+1; $imgSvc->getThumImgByUrl($img,$imgID); echo "<img src='./".$imgID."_thum_".$width."_".$height.".jpg'>"; }/*}}}*/ ?>
|  胸口别朵大红花:http://max.ersha.cn |
|