2012-10-05 4 views
0

나는 현재의 화면비를 유지하면서 사용자 정의에 이미지 크기를 조정하고있다 :이미지의 수직 및 수평 중앙 버전을 자르는 방법은 무엇입니까?

class ImgResizer { 
var $originalFile = '$newName'; 
function ImgResizer($originalFile = '$newName') { 
    $this -> originalFile = $originalFile; 
} 
function resize($newWidth, $targetFile) { 
    if (empty($newWidth) || empty($targetFile)) { 
     return false; 
    } 
    $src = imagecreatefromjpeg($this -> originalFile); 
    list($width, $height) = getimagesize($this -> originalFile); 
    $newHeight = ($height/$width) * $newWidth; 
    $tmp = imagecreatetruecolor($newWidth, $newHeight); 
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    if (file_exists($targetFile)) { 
     unlink($targetFile); 
    } 
    imagejpeg($tmp, $targetFile, 95); 
} 
} 

사용법 :

$work = new ImgResizer($path); 
$work -> resize(200, $path); 

하지만 난 이미지 오 200x200px 버전을 좀하고 싶습니다. 그리고 그것은 수직으로 amd (기본적으로 이미지의 200px를 얻으십시오)

은 가능합니까?

-EDIT-

function resize($newWidth, $targetFile) { 
    if (empty($newWidth) || empty($targetFile)) { 
     return false; 
    } 
    $src = imagecreatefromjpeg($this -> originalFile); 
    list($width, $height) = getimagesize($this -> originalFile); 

    $newHeight = $newWidth; 


     if ($width > $newWidth){ 
     $srcx = $width/2 - $newWidth/2; 
     $destx = 0; 
    } 
    else{ 
     $srcx = 0; 
     $destx = $newWidth/2 - $width/2; 
    } 
    if ($height > $newHeight){ 
     $srcy = $height/2 - $newHeight/2; 
     $desty = 0; 
    } 
    else{ 
     $srcy = 0; 
     $desty = $newHeight/2 - $height/2; 
    } 


    $tmp = imagecreatetruecolor($newWidth, $newHeight); 
    imagecopyresampled($tmp, $src, $destx, $desty, $srcx, $srcy, $newWidth, $newHeight, $width, $height); 


    if (file_exists($targetFile)) { 
     unlink($targetFile); 
    } 
    imagejpeg($tmp, $targetFile, 95); 
} 

예상치 못한 뭔가 만들겠습니까 : HTTP://209.51.221.243/integracion/files/uploads/1_050.JPG

답변

1

if ($width > $newWidth){ 
    $srcx = $width/2 - $newWidth/2; 
    $destx = 0; 
    $w = $newWidth; 
} 
else{ 
    $srcx = 0; 
    $destx = $newWidth/2 - $width/2; 
    $w = $width; 
} 
if ($height > $newHeight){ 
    $srcy = $height/2 - $newHeight/2; 
    $desty = 0; 
    $h = $newHeight; 
} 
else{ 
    $srcy = 0; 
    $desty = $newHeight/2 - $height/2; 
    $h = $keight; 
} 
imagecopyresampled($tmp, $src, $destx, $desty, $srcx, $srcy, $w, $h, $w, $h); 
+0

가 편집을 확인하시기 바랍니다 시도를, 당신은을 의미합니까? –

+0

@ToniMichelCaubet 지금 시도하십시오 – Musa

+0

http://209.51.221.243/integracion/files/uploads/1_2012-09-26%2023.11.55.jpg 어쨌든 왜 검은 틈이 있습니까? –

관련 문제