繁体   English   中英

如何优化此功能?

[英]How can I optimize this function?

我创建了一个函数,用于在用户通过网络表单上传图像时生成并保存多种图像尺寸。 我想知道如何才能更好地优化代码(减少行数,同时仍易于阅读)

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

首先,我注意到您创建了一个变量,但尚未使用它。

$size = getimagesize($tmpName);

那么,为什么不使用某个函数时就调用它并为其赋值。

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

$imagex = imagesx($img);

$imagey = imagesy($img);

因此,我建议您将代码中提到的3行替换为一个

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

最后,不要复制代码,而是创建一个带有传递参数的函数,然后调用函数,如上面的注释所示。

还注意到您发送“大”即图像大小作为参数,然后为什么要通过拇指和中药盒。 建议使用切换情况,例如将保存功能更改为

函数save_image($ _ FILES ['image'],$ _GET ['member_id'],250,300,$ type =“ large”)

然后在$ 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');

您应该可以轻松地为此创建函数,这是一个好的开始。

您的许多代码都是多余的。

您可以执行一些功能:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM