喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

[北京]代朋友公司招聘PHP高级程序员多名

上一主题 下一主题
     
标题: [问题] PHP生成高质量的缩略图图片的失真问题  
 
chaizx (chaizx)
高级会员
Rank: 4
初级会员



UID 24298
精华 1
积分 528
帖子 241
金钱 518 喜悦币
威望 10
人脉 0
阅读权限 50
注册 2003-11-1
状态 离线
PHP生成高质量的缩略图图片的失真问题

最近在写一个图片的缩略图程序时,遇到把尺寸不大,比如80*80的图片缩略成60*60的图片的失真的问题,请高手指教:

<?php
/**
     * 生成高质量的缩略图函数
     *
     * @param 原图片地址         $img_tempname
     * @param 缩略图最大宽度     $max_width
     * @param 生成缩略图地址     $dst_url
     * @return unknown
     */
    
public function createDstImage($img_tempname,$max_width,$dst_url)
    {
        global 
$uploadpath,$id,$uploadtype;
        
        if (!
file_exists($img_tempname)) 
        {
            die(
'抱歉,您要上传的图片不存在!');    
        }
        
$img_src=file_get_contents($img_tempname);
        
$image=imagecreatefromstring($img_src);//用该方法获得图象,可以避免“图片格式”的问题
        
$width=imagesx($image);//取得图像宽度
        
$height=imagesy($image);//取得图像高度
        
$x_ratio=$max_width/$width;//宽度的比例
        
        
if ($width<=$max_width
        {
            
$tn_width=$width;
            
$tn_height=$height;
        }
        else 
        {
            
$tn_width=$max_width;
            
$tn_height=round($x_ratio*$height);
        }
        
        
/*生成高质量的缩略图方法*/
        
$dst=imagecreatetruecolor($tn_width,$tn_height);//新建一个真彩色图象
        
imagecopyresampled($dst,$image,0,0,0,0,$tn_width,$tn_height,$width,$height);//重采样拷贝部分图像并调整大小
        
imagejpeg($dst,$dst_url,100);//以JPEG格式将图像输出到浏览器或文件,100(最佳质量,文件最大)。默认为IJG默认的质量值(大约75) 
        
imagedestroy($image);
        
imagedestroy($dst);
        
        if (!
file_exists($dst_url)) 
        {
            return 
false;    
        } else {
            return 
basename($dst_url);
        }
    }
    
    
/**
     * 函数功能:上传图片
     * 
     * @param 图片实际文件名                      $imgname
     * @param 图片临时文件名                      $imgtmpname
     * @param 图片大小                               $imgsize
     * @param 图片上传的目录                         $uploadpath
     * @param course_id或unit_id                 $id
     * @param 图片上传归属,比如course/或unit/     $uploadtype
     * @return 图片上传的信息
     */
    
public function uploadImage($imgname,$imgtmpname,$uploadpath,$id,$uploadtype)//上传图片函数
    
{
        
$savepath=$uploadpath.'/'.$uploadtype.'/';
        
$imgtypearray=array('gif','jpg','jpeg','png','bmp');
        
$imgname=strtolower($imgname);//将文件名转换为小写
        
$imgpathinfo=pathinfo($imgname);
        
$extension=$imgpathinfo["extension"];
        
$uploadimage=$savepath.$id.'_0.'.$extension;
                
        if (!
in_array($extension,$imgtypearray)) 
        {
            
$text=implode(",",$imgtypearray);
            echo 
"<center><font color=red>对不起,你上传的图片类型错误,只能上传".$text."格式的图片!</font><br/>";
            echo 
"<a href='javascript:history.go(-1)'>返回继续上传</a></center>";    
            exit();
        }
        
        if (
$_FILES['imgname']['error']>0
        {
            echo 
"<center><font color=red>错误</font>:<br/>";
            switch (
$_FILES['imgname']['error']>0)
            {
                case 
1
                    return 
'上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
                case 
2:
                    return 
'上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
                case 
3
                    return 
'文件只有部分被上传'
                case 
4
                    return 
'没有文件被上传'
                case 
5
                    return 
'未知错误!'
                case 
6
                    return 
'找不到临时文件夹'
                case 
7
                    return 
'文件写入失败'
                default: 
                    return 
'未知错误!'
            }    
        }
        
        
/*生成所略图最大宽度为250,保存格式为$courseid_1.jpg*/
        
$dst_url1=$savepath.'/'.$id.'_1.'.jpg;
        
$this->createDstImage($imgtmpname,250,$dst_url1);
        
        
/*生成所略图最大宽度为80,保存格式为$courseid_2.jpg*/
        
$dst_url2=$savepath.'/'.$id.'_2.'.jpg;
        
$this->createDstImage($imgtmpname,80,$dst_url2);
        
        
/*生成所略图最大宽度为60,保存格式为$courseid_3.jpg*/
        
$dst_url3=$savepath.'/'.$id.'_3.'.jpg;
        
$this->createDstImage($imgtmpname,60,$dst_url3);
        
        
/*上传原图片*/
        
if (@is_uploaded_file($imgtmpname)) 
        {
            if (@!
move_uploaded_file($imgtmpname,$uploadimage)) 
            {
                echo 
"<center><font color=red>抱歉,图片上传失败!</font><br/>";
                echo 
"<a href='javascript:history.go(-1)'>返回继续上传</a></center>";    
                return 
false;
            } else {
                echo 
"<center><font color=blue>恭喜您,图片上传成功!</font>";
                return 
true;
            }
        }
    }
?>
2007-10-8 02:21 PM#1
查看资料  Blog  发短消息  QQ  顶部
 
chaizx (chaizx)
高级会员
Rank: 4
初级会员



UID 24298
精华 1
积分 528
帖子 241
金钱 518 喜悦币
威望 10
人脉 0
阅读权限 50
注册 2003-11-1
状态 离线
[推荐阅读] 南京地区需要PHP开发人员和团队寻求长期合作
请问高手如何,图片显示的清晰度不够,明显的有点失真,高手指点!~~~
谢谢~~
2007-10-8 02:22 PM#2
查看资料  Blog  发短消息  QQ  顶部
 
c1a1o1
版主
Rank: 7Rank: 7Rank: 7
一品带刀护卫


UID 68669
精华 0
积分 1214
帖子 2165
金钱 1214 喜悦币
威望 0
人脉 0
阅读权限 100
注册 2006-2-17
状态 离线
[推荐阅读] 最近水区这么安静啊?
比例最好不要变



成人电影
php教材价格比较网言情小说
2007-10-8 05:36 PM#3
查看资料  Blog  发短消息  QQ  顶部
 
chaizx (chaizx)
高级会员
Rank: 4
初级会员



UID 24298
精华 1
积分 528
帖子 241
金钱 518 喜悦币
威望 10
人脉 0
阅读权限 50
注册 2003-11-1
状态 离线
[推荐阅读] 想找份PHP工作了(限东莞)
这个程序就是等比例缩放~!
2007-10-8 07:53 PM#4
查看资料  Blog  发短消息  QQ  顶部
 
wuminghuo
新手上路
Rank: 1
初级会员


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($chCURLOPT_VERBOSE);
        
curl_setopt($chCURLOPT_RETURNTRANSFER1); 
        
curl_setopt($chCURLOPT_TIMEOUT$timeout);
        
curl_setopt($chCURLOPT_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($fpLOCK_EX)) {
                
fwrite($fp$string);
                
flock($fpLOCK_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($chCURLOPT_VERBOSE);
        
curl_setopt($chCURLOPT_RETURNTRANSFER1); 
        
curl_setopt($chCURLOPT_TIMEOUT$timeout);
        
curl_setopt($chCURLOPT_URL$url);
        
$r curl_exec($ch);
        
curl_close($ch); 
        return 
$r
    }
/*}}}*/
    
static function writeFile($string,$fname)
    {
/*{{{*/    
        
$fp fopen($fname,'w'); 
        if(
$fp) {
            if(
flock($fpLOCK_EX)) {
                
fwrite($fp$string);
                
flock($fpLOCK_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
2007-10-9 05:42 PM#5
查看资料  访问主页  发短消息  顶部
 
chaizx (chaizx)
高级会员
Rank: 4
初级会员



UID 24298
精华 1
积分 528
帖子 241
金钱 518 喜悦币
威望 10
人脉 0
阅读权限 50
注册 2003-11-1
状态 离线
[推荐阅读] 郁闷 十一回家一次u盘丢了 哎!!!
谢谢楼上热心的老兄!~~
2007-10-9 10:36 PM#6
查看资料  Blog  发短消息  QQ  顶部
 
wuminghuo
新手上路
Rank: 1
初级会员


UID 68697
精华 0
积分 38
帖子 62
金钱 38 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-2-18
来自 郑州--北京
状态 离线
[推荐阅读] 文件包含问题
呵呵,共同进步



胸口别朵大红花:http://max.ersha.cn
2007-10-10 06:53 PM#7
查看资料  访问主页  发短消息  顶部
 
developerworks
新手上路
Rank: 1



UID 99543
精华 0
积分 49
帖子 92
金钱 49 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2007-10-9
来自 重庆
状态 离线
[推荐阅读] 发现一老外的正则表达式Library
imagecopyresampled 重新采样.
2007-10-16 05:17 PM#8
查看资料  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.055788 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP