首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何优化此函数?

如何优化此函数?
EN

Stack Overflow用户
提问于 2011-03-08 00:32:45
回答 3查看 250关注 0票数 1

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

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

函数

代码语言:javascript
复制
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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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上使用开关。

票数 1
EN

Stack Overflow用户

发布于 2011-03-08 00:38:54

你有3次几乎相同的代码

代码语言:javascript
复制
    $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
EN

Stack Overflow用户

发布于 2011-03-08 00:42:44

你的很多代码都是多余的。

你可以做一个小函数:

代码语言:javascript
复制
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');
}

它可以像这样调用:

代码语言:javascript
复制
repetitiveFunctionForPicture($image, 180, 300,$tmpName, $file);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5222248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档