사용자가 웹 양식을 통해 이미지를 업로드 할 때 여러 이미지 크기를 생성하고 저장하는 기능을 만들었습니다. 나는 내가 더 잘 내 코드 (여전히 쉽게 읽을하면서 라인의 수를 줄일 수)어떻게이 기능을 최적화 할 수 있습니까?
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;
}
와우, 공백은 친구입니다. – ryeguy
이것은 코드 검토에 속합니다. – Shoe