2012-04-04 83 views
0

我沒有太多的時間來寫,所以會告訴你我的問題,我有一個PHP代碼,裁剪圖像的好處是,它裁剪圖像真的很好,當我把$ thumb_width和$ thumb_height具有相同的值,但在這種情況下,我不是我想要的圖像與此分辨率(200 * 150) 這裏的代碼,我找不到錯誤。作物圖像php變形,扭曲

<?php 
function create_thumb($directory, $image, $destination) { 
$image_file = $image; 
$image = $directory.$image; 

if (file_exists($image)) { 

$source_size = getimagesize($image); 

if ($source_size !== false) { 

    $thumb_width = 200; 
    $thumb_height = 150; 

    switch($source_size['mime']) { 
    case 'image/jpeg': 
     $source = imagecreatefromjpeg($image); 
    break; 
    case 'image/png': 
     $source = imagecreatefrompng($image); 
    break; 
    case 'image/gif': 
     $source = imagecreatefromgif($image); 
    break; 
    } 

    $source_aspect = round(($source_size[0]/$source_size[1]), 1); 
    $thumb_aspect = round(($thumb_width/$thumb_height), 1); 

    if ($source_aspect < $thumb_aspect) { 
    $new_size = array($thumb_width, ($thumb_width/$source_size[0]) * $source_size[1]); 
    $source_pos = array(0, ($new_size[1] - $thumb_height)/2); 
    } else if ($source_aspect > $thumb_aspect) { 
    $new_size = array(($thumb_width/$source_size[1]) * $source_size[0], $thumb_height); 
    $source_pos = array(($new_size[0] - $thumb_width)/2, 0); 
    } else { 
    $new_size = array($thumb_width, $thumb_height); 
    $source_pos = array(0, 0); 
    } 

    if ($new_size[0] < 1) $new_size[0] = 1; 
    if ($new_size[1] < 1) $new_size[1] = 1; 

    $thumb = imagecreatetruecolor($thumb_width, $thumb_height); 
    imagecopyresampled($thumb, $source, 0, 0, $source_pos[0], $source_pos[1], $new_size[0],                $new_size[1], $source_size[0], $source_size[1]); 

    switch($source_size['mime']) { 
    case 'image/jpeg': 
     imagejpeg($thumb, $destination.$image_file); 
    break; 
    case 'image/png': 
      imagepng($thumb, $destination.$image_file); 
    break; 
    case 'image/gif': 
     imagegif($thumb, $destination.$image_file); 
    break; 
} 


} 

} 
} 
?> 
在這個例子中

,如果我把(533px * 300像素),它的變形的圖像,包裹「對不起,我的英語=)」,請我需要幫助與此有關。如果你找到解決方案,我會很開心。

謝謝,Matias。

回答

1

This id dilli。

在第46行中,您給出了更多空間,因此您需要刪除該行中不需要的空格。

+0

請問白色空間物質? – 2012-10-18 06:46:11

0

是的。即使我們提供了多個空格,通常也只有一個空格。但是在這裏存在多個「& nbsp」的空間。

語法錯誤:imagecopyresampled($拇指,$源,0,0,$ source_pos [0],$ source_pos [1],$ new_size [0],                                                      $ new_size [1],$ source_size [0],$ source_size [1]);

正確語法:imagecopyresampled($ thumb,$ source,0,0,$ source_pos [0],$ source_pos [1],$ new_size [0],$ new_size [1],$ source_size [0],$ source_size [1]);