简体   繁体   English

如何优化此功能?

[英]How can I optimize this function?

I created a function for generating and saving multiple image sizes when a user uploads an image via a web form. 我创建了一个函数,用于在用户通过网络表单上传图像时生成并保存多种图像尺寸。 I was wondering, how can I better optimize my code (reduce the number of lines while still being easily readable) 我想知道如何才能更好地优化代码(减少行数,同时仍易于阅读)

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

The Function 功能

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

First of all I noticed you create a variable and it has not been used. 首先,我注意到您创建了一个变量,但尚未使用它。

$size = getimagesize($tmpName);

So why call a function and assign its value when it is not being used. 那么,为什么不使用某个函数时就调用它并为其赋值。

Secondly to get the width and height you don't have to do 其次,获得宽度和高度,您不必做

$imagex = imagesx($img);

$imagey = imagesy($img);

So I would suggest you replace the 3 lines mentioned in this code with a single one 因此,我建议您将代码中提到的3行替换为一个

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

Finally instead of duplicating the code create a function with parameters passed and call the function as it is shown in the comments above. 最后,不要复制代码,而是创建一个带有传递参数的函数,然后调用函数,如上面的注释所示。

Also noticed that you send "large" ie image size as the parameter then why are you running through the thumb and med cases. 还注意到您发送“大”即图像大小作为参数,然后为什么要通过拇指和中药盒。 Would suggest use switch cases like change the save function to 建议使用切换情况,例如将保存功能更改为

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

and then use a switch on $type. 然后在$ type上使用一个开关。

You have 3 times almost the same code 您有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');

You should easily be able to make a function for that, that's a good start. 您应该可以轻松地为此创建函数,这是一个好的开始。

A lot of your code is redundant. 您的许多代码都是多余的。

You could make a little function : 您可以执行一些功能:

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

which can be called like this : 可以这样称呼:

repetitiveFunctionForPicture($image, 180, 300,$tmpName, $file);

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

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