我创建了一个函数,用于在用户通过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;
}发布于 2011-03-08 00:56:49
首先,我注意到你创建了一个变量,但它还没有被使用。
$size = getimagesize($tmpName);
那么为什么调用一个函数并在它不被使用的时候赋值呢?
其次,为了获得宽度和高度,你不需要做
$imagex = imagesx($img);
$imagey = imagesy($img);
因此,我建议您将这段代码中提到的3行替换为一行
list($width, $height, $type, $attr) = getimagesize($tmpName);
最后,不是重复代码,而是创建一个传递了参数的函数,并调用该函数,如上面的注释所示。
我还注意到你发送了“大”即图像大小作为参数,那么为什么你要运行拇指和医学案例。我建议使用切换情况,如将save函数更改为
函数save_image($_FILES‘’image‘,$_GET’‘member_id’,250,300,$type = "large")
然后在$type上使用开关。
发布于 2011-03-08 00:38:54
你有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');你应该可以很容易地为它创建一个函数,这是一个很好的开始。
发布于 2011-03-08 00:42:44
你的很多代码都是多余的。
你可以做一个小函数:
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);https://stackoverflow.com/questions/5222248
复制相似问题