如何优化此功能?

问题描述:

我创建了一个函数,用于在用户通过Web表单上传图像时生成并保存多个图像大小。我在想,我怎么能更好地优化我的代码(减少线的数量,同时仍然易于阅读)如何优化此功能?

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 2011-03-07 16:44:52

+0

这属于Code Review。 – Shoe 2011-03-07 17:02:01

首先,它没有被使用。

$size = getimagesize($tmpName);

那么,为什么调用一个功能,在不使用时将它分配其价值。

其次获得的宽度和高度,你不必做

$imagex = imagesx($img);

$imagey = imagesy($img);

所以我建议你用一个单一的一个

替换此代码中提到的3线

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

最后,不是复制代码c使用传递的参数reate一个函数,并调用上面注释中显示的函数。

也注意到,你发送“大”,即图像大小作为参数,那么你为什么通过拇指和med案件运行。建议使用开关情况下,像改变保存函数

功能save_image($ _ FILES [ '图像'],$ _GET [ 'member_id'],250,300,$类型= “大”)

和然后在$ type上使用开关。

你有近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'],你为什么要这样做?您将它作为参数传入。如果($ image)应该是if(isset($ file ['name'])),因为它会做一个软读取而不是一个硬读取,如果它存在的话。 – judda 2011-03-07 16:41:21

很多代码都是多余的。

你可以做一个小功能:

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);