2012-03-18 143 views
0

所以我想要一个方形图像并将其大小调整为300px * 400px(例如)。调整图像大小(image_lib),保持宽高比,但缩放到最小可能超过宽度和高度

我想保持宽高比,但是要使图片超出或匹配两个维度。因此,在原始方形图像的情况下,生成的图像将为400px * 400px。

这有道理吗?我想要这个,因为然后我会继续裁剪图像,使其精确到300px * 400px。

设置$config['maintain_ratio'] = false;使图像变形。

谢谢你的启发。

回答

2

试试这个....这将帮助..

if(file_exists('test.jpg')) 
     unlink('test.jpg'); 

     $dest='test.jpg'; 


    $image='fg.jpg'; 

$list=getimagesize($image); 
$width=$list[0]; 
    $height=$list[1]; 


    $newwidth=65; 
    $newheight=95; 



    $int_x=10; 
$int_y=0; 




define('DESIRED_IMAGE_WIDTH', 65); 
define('DESIRED_IMAGE_HEIGHT', 95); 
$source_gdim = imagecreatefromjpeg($image); 

$source_aspect_ratio = $width/$height; 
$desired_aspect_ratio = DESIRED_IMAGE_WIDTH/DESIRED_IMAGE_HEIGHT; 

// For Wide Image 
if ($source_aspect_ratio > $desired_aspect_ratio) 
    { 
$temp_height = DESIRED_IMAGE_HEIGHT; 
$temp_width = (int) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio); 
    } 
    // For Tall Image 
else 
    { 
    $temp_width = DESIRED_IMAGE_WIDTH; 
    $temp_height = (int) (DESIRED_IMAGE_WIDTH/$source_aspect_ratio); 
    } 


    $temp_gdim = imagecreatetruecolor($temp_width, $temp_height); 
    imagecopyresampled($temp_gdim,$source_gdim,0, 0,0, 0,$temp_width, $temp_height,$width, $height); 


$x0 = ($temp_width - DESIRED_IMAGE_WIDTH)/2; 
    $y0 = ($temp_height - DESIRED_IMAGE_HEIGHT)/2; 

    $desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT); 
    imagecopy($desired_gdim,$temp_gdim,0, 0,$x0, $y0,DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT); 


     imagejpeg($desired_gdim,'test.jpg',100); 
+1

您好,感谢这一点。它不是codeigniter特定的,但我想我可以使用一些代码来将我的图像调整为特定的尺寸,然后相应地裁剪。 :) 多谢,伙计。 – 2012-03-19 11:24:13

相关问题