2011-03-07 7 views
1

사용자가 웹 양식을 통해 이미지를 업로드 할 때 여러 이미지 크기를 생성하고 저장하는 기능을 만들었습니다. 나는 내가 더 잘 내 코드 (여전히 쉽게 읽을하면서 라인의 수를 줄일 수)어떻게이 기능을 최적화 할 수 있습니까?

save_image($_FILES['image'], $_GET['member_id'], 250, 300, large) //usage example

기능

function save_image($file, $id, $sizex, $sizey, $pre){ 
$image=$_FILES['image']['name']; 
$tmpName = $_FILES['image']['tmp_name']; 
if ($image){ 
    //get the original name of the file from the clients machine 
     $filename = stripslashes($_FILES['image']['name']); 
    //get the extension of the file in a lower case format 
     $extension = getExtension($filename); 
     $extension = strtolower($extension); 
    if (($extension == "jpg") || ($extension == "jpeg")){ 
     $size = getimagesize($tmpName); 
     $max_x = 180; 
     $max_y = 300; 
     $img = imagecreatefromjpeg($file['tmp_name']); 
     $imagex = imagesx($img); 
     $imagey = imagesy($img); 
     $dim = max($imagex/$max_x, $imagey/$max_y); 
     $nx = $imagex/$dim; 
     $ny = $imagey/$dim; 
     $image = imagecreatetruecolor($nx, $ny); 
     imagecopyresampled($image, $img, 0, 0, 0, 0, $nx, $ny, $imagex, $imagey); 
     imagejpeg($image, '../images/uploads/'.$id.'-large.jpg'); 
     //Make the thumb 
     $size = getimagesize($tmpName); 
     $max_x = 120; 
     $max_y = 230; 
     $img = imagecreatefromjpeg($file['tmp_name']); 
     $imagex = imagesx($img); 
     $imagey = imagesy($img); 
     $dim = max($imagex/$max_x, $imagey/$max_y); 
     $nx = $imagex/$dim; 
     $ny = $imagey/$dim; 
     $image = imagecreatetruecolor($nx, $ny); 
     imagecopyresampled($image, $img, 0, 0, 0, 0, $nx, $ny, $imagex, $imagey); 
     imagejpeg($image, '../images/uploads/'.$id.'-med.jpg'); 
     $size = getimagesize($tmpName); 
     $max_x = 60; 
     $max_y = 115; 
     $img = imagecreatefromjpeg($file['tmp_name']); 
     $imagex = imagesx($img); 
     $imagey = imagesy($img); 
     $dim = max($imagex/$max_x, $imagey/$max_y); 
     $nx = $imagex/$dim; 
     $ny = $imagey/$dim; 
     $image = imagecreatetruecolor($nx, $ny); 
     imagecopyresampled($image, $img, 0, 0, 0, 0, $nx, $ny, $imagex, $imagey); 
     imagejpeg($image, '../images/uploads/'.$id.'-thumb.jpg'); 
    }else{ 
     return false; 
    } 
}else{ 
    return false; 
} 
return true; 
} 
+1

와우, 공백은 친구입니다. – ryeguy

+0

이것은 코드 검토에 속합니다. – Shoe

답변

1

먼저 그리고 사용되지 않았습니다.

$size = getimagesize($tmpName);

은 왜 함수를 호출하고 사용하지 않을 때 그 값을 할당합니다.

둘째

너비와 높이를 얻을 수있는 당신은 그래서 당신이 하나 하나

이 코드에 언급 된 3 개 라인을 대체 제안

$imagex = imagesx($img);

$imagey = imagesy($img);

할 필요가 없습니다

list($width, $height, $type, $attr) = getimagesize($tmpName);

마지막으로 코드 c를 복제하는 대신 전달 된 매개 변수로 함수를 재정의하고 위 주석에 표시된대로 함수를 호출하십시오.

매개 변수로 '큰'이미지 크기를 보내면 엄지 손가락과 중간 상자를 돌고있는 이유는 무엇입니까?

함수 save_image ($ _ FILES [ '상', $ _GET [ 'MEMBER_ID'] 250, 300, $ 타입 = "큰")

하고 저장 기능을 변경처럼 사용 스위치 케이스를 제안 $ type에 스위치를 사용하십시오.

0

당신은 3 번 거의 동일한 코드를 최적화 할 수있는 방법, 궁금

$size = getimagesize($tmpName); 
    $max_x = 60; 
    $max_y = 115; 
    $img = imagecreatefromjpeg($file['tmp_name']); 
    $imagex = imagesx($img); 
    $imagey = imagesy($img); 
    $dim = max($imagex/$max_x, $imagey/$max_y); 
    $nx = $imagex/$dim; 
    $ny = $imagey/$dim; 
    $image = imagecreatetruecolor($nx, $ny); 
    imagecopyresampled($image, $img, 0, 0, 0, 0, $nx, $ny, $imagex, $imagey); 
    imagejpeg($image, '../images/uploads/'.$id.'-thumb.jpg'); 

쉽게 기능을 만들 수 있어야합니다. 좋은 시작입니다.

+0

또한 매개 변수를 사용해야합니다 ... 두 번째 줄과 세 번째 줄 $ image = $ _FILES [ 'image'] [ 'name'], 왜 그렇게하고 있습니까? 그것을 매개 변수로 전달 중입니다. if ($ image [if '($ image ['name '])) 대신에 하드 읽기보다는 소프트 읽기를하기 때문에 if ($ image)는 if (isset – judda

0

많은 코드가 중복됩니다.

function repetitiveFunctionForPicture($image, $max_x, $max_y,$tmpName, $file){ 
    $size = getimagesize($tmpName); 
    $img = imagecreatefromjpeg($file['tmp_name']); 
    $imagex = imagesx($img); 
    $imagey = imagesy($img); 
    $dim = max($imagex/$max_x, $imagey/$max_y); 
    $nx = $imagex/$dim; 
    $ny = $imagey/$dim; 
    $image = imagecreatetruecolor($nx, $ny); 
    imagecopyresampled($image, $img, 0, 0, 0, 0, $nx, $ny, $imagex, $imagey); 
    imagejpeg($image, '../images/uploads/'.$id.'-large.jpg'); 
} 

과 같이 호출 할 수 있습니다 :

당신은 약간의 기능을 만들 수있는 당신이 변수를 만들 발견 모든

repetitiveFunctionForPicture($image, 180, 300,$tmpName, $file); 
관련 문제