关闭

图像重采样调整大小

1840人阅读 评论(1) 收藏 举报
//-----------------------------------------------------------------------------------------------------
//图像重采样调整大小,如果成功,返回一个数组,有两个值,第一个是调整后的宽度,第二个是调整后的高度,
//参数的含义:
//imageurl:    要调整的图像文件名
//$seze:    调整目标大小的像素数,一个参数,表示一个正方形的区域
//$quality:    如果是JPEG图像,则可以指定,目标图像的品质,范围:[0,100]
//------------------------------------------------------------------------------------------------------

function imageresize($imageurl,$size=800,$quality=75)
{
    
$size=intval($size);
    
if($size<=0)
        
$size=1024;

    
$quality=intval($quality);
    
if($quality<0)
        
$quality=0;
    
if($quality>100)
        
$quality=100;
    
    
if(!file_exists($imageurl))
    {
        
return array(-1,-1);
    }
else
    {
        
$ext=strtolower(substr($imageurl,strrpos($imageurl,".")+1));
        
if($ext=='jpg'||$ext=='jpeg'||$ext=='gif'||$ext=='png'||$ext=='bmp')
        {
            
$imagewh=getimagesize($imageurl);
            
$Ws=$imagewh[0];
            
$Hs=$imagewh[1];
            
            
if($Ws>=$Hs)
            {
                
if($Ws<=$size)
                    
return getimagesize($imageurl);
                
else
                {
                    
$Wt=$size;
                    
$Ht=intval(($Hs/$Ws)*$size);
                }
            }
else


            {
                
if($Hs<=$size)
                    
return getimagesize($imageurl);
                
else
                {
                    
$Wt=intval(($Ws/$Hs)*$size);
                    
$Ht=$size;
                }
            }
    
            
switch($ext)
            {
                
case 'jpg':
                
case 'jpeg':
                    
$imgs=@imagecreatefromjpeg($imageurl);
                    
$imgt=@imagecreatetruecolor($Wt,$Ht)
                          or 
die ("Cannot Initialize new GD image stream");
                    imagecopyresampled(
$imgt,$imgs,0,0,0,0,$Wt,$Ht,$Ws,$Hs);
                    imagejpeg(
$imgt,$imageurl);
                    imagedestroy(
$imgs);
                    imagedestroy(
$imgt);
                    
return array($Wt,$Ht);
                
break;
                
                
case 'bmp':
                    
$imgs=@imagecreatefromjpeg($imageurl);
                    
$imgt=@imagecreatetruecolor($Wt,$Ht)
                          or 
die ("Cannot Initialize new GD image stream");
                    imagecopyresampled(
$imgt,$imgs,0,0,0,0,$Wt,$Ht,$Ws,$Hs);
                    imagewbmp(
$imgt,$imageurl);
                    imagedestroy(
$imgs);
                    imagedestroy(
$imgt);
                    
return array($Wt,$Ht);
                
break;
    
                
case 'gif':
                    
$imgs=@imagecreatefromjpeg($imageurl);
                    
$imgt=@imagecreatetruecolor($Wt,$Ht)
                          or 
die ("Cannot Initialize new GD image stream");
                    imagecopyresampled(
$imgt,$imgs,0,0,0,0,$Wt,$Ht,$Ws,$Hs);
                    imagegif(
$imgt,$imageurl);
                    imagedestroy(
$imgs);
                    imagedestroy(
$imgt);
                    
return array($Wt,$Ht);
                
break;
    
                
case 'png':
                    
$imgs=@imagecreatefromjpeg($imageurl);
                    
$imgt=@imagecreatetruecolor($Wt,$Ht)
                          or 
die ("Cannot Initialize new GD image stream");
                    imagecopyresampled(
$imgt,$imgs,0,0,0,0,$Wt,$Ht,$Ws,$Hs);
                    imagepng(
$imgt,$imageurl);
                    imagedestroy(
$imgs);
                    imagedestroy(
$imgt);
                    
return array($Wt,$Ht);
                
break;
                
                
default:
                    
return array(-1,-1);
            }
        }
else
        {
            
return array(-1,-1);
        }
    }
}
//------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6039次
    • 积分:104
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:3篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档