如何更改功能以接受不同类型的文件


How to change function in order to accept different types of files?

我有这段代码,它可以帮助我筛选JPEG图像,现在,我需要的是创建一个图像、PNG JPG或其他任何东西!

我做了一些修改,这是最后的结果:

function tamano_nuevo_foto($im_or, $ancho_nv, $dir_nv) {
    $ext   = pathinfo($im_or, PATHINFO_EXTENSION);
    $datos = getimagesize($im_or);
    $ancho = $datos[0];
    $alto = $datos[1];
    if ($ancho > $ancho_nv) { //Si la imagen no lelga al máximo no la tocamos.
        $prop    = $alto / $ancho;
        $alto_nv = round($ancho_nv * $prop); //Sacamos la nueva altura
    } else {
        $ancho_nv = $ancho;
        $alto_nv = $alto;
    }
    $im_nv = imagecreatetruecolor($ancho_nv, $alto_nv);
    imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);
    switch ($ext) {
        case 'jpg':
            $img = imagecreatefromjpeg($im_or);
            break;
        case 'jpeg':
            $img = imagecreatefromjpeg($im_or);
            break;
        case 'png':
            $img = imagecreatefrompng($im_or);
            break;
        case 'gif':
            $img = imagecreatefromgif($im_or);
            break;
        default:
            $img = imagecreatefromjpeg($im_or);
    }
    imagedestroy($im_nv);
}

而原始代码是:

function tamano_nuevo_foto($im_or, $ancho_nv, $dir_nv) {
    $img   = imagecreatefromstring($im_or);
    $datos = getimagesize($im_or);
    $ancho = $datos[0];
    $alto  = $datos[1];
    if ($ancho > $ancho_nv) { //Si la imagen no lelga al máximo no la tocamos.
        $prop    = $alto / $ancho; /* Calculo la proporcion entre la or y la nv (lo miltiplicamos por mil para evitar problemas con decimales */
        $alto_nv = round($ancho_nv * $prop); //Sacamos la nueva altura
    } else {
        $ancho_nv = $ancho;
        $alto_nv  = $alto;
    }
    $im_nv    = imagecreatetruecolor($ancho_nv, $alto_nv);
    imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);
    imagejpeg($im_nv, $dir_nv);
    imagedestroy($im_nv);
}

该函数创建如下:

$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetPath =  str_replace('//','/',$targetPath);
$targetFile =  $targetPath . $_FILES['Filedata']['name'];
tamano_nuevo_foto($tempFile, 800, $targetFile);

在我看来,您在创建图像资源之前使用的是$img变量:

    imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);
    //^^ using $img 
    switch ($ext)
    {//creating img here...
        case 'jpg':
            $img = imagecreatefromjpeg($im_or);
            break;
        case 'jpeg':
            $img = imagecreatefromjpeg($im_or);
            break;
        case 'png':
            $img = imagecreatefrompng($im_or);
            break;
        case 'gif':
            $img = imagecreatefromgif($im_or);
            break;
        default:
            $img = imagecreatefromjpeg($im_or);
    }

只需将imagecopyresampled向下移动,就在开关之后。顺便说一句,这个开关可以写得体积小得多:

    switch ($ext)
    {//creating img here...
        case 'png':
            $img = imagecreatefrompng($im_or);
            break;
        case 'gif':
            $img = imagecreatefromgif($im_or);
            break;
        default://no cases for jpg or jpeg means default, which creates from jpeg
            $img = imagecreatefromjpeg($im_or);
    }
    imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);

如果你出于某种原因想保留那些jpeg的情况,不需要写两次相同的代码,这要归功于失败:

    switch ($ext)
    {//creating img here...
        case 'jpeg':
            //you can even add some code specifically for the jpeg
            // extention, that will not be executed if the extention is jpg
            //if you omit the break at the end, the next case will be executed, too 
        case 'jpg':
            $img = imagecreatefromjpeg($im_or);
        break;
        case 'png':
            $img = imagecreatefrompng($im_or);
            break;
        case 'gif':
            $img = imagecreatefromgif($im_or);
            break;
        default://no cases for jpg or jpeg means default, which creates from jpeg
            $img = imagecreatefromjpeg($im_or);
    }
    imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);

你的意思是你想要这样:

function tamano_nuevo_foto($im_or, $width_nv, $dir_nv) {
    list($width, $height, $type, $attr) = getimagesize($im_or);
    $x_ratio = $width_nv / $width; 
    if($width <= $width_nv) { 
        $width_nv = $width; 
        $height_nv = $height; 
    } else { 
        $height_nv = ceil($height * $x_ratio);
    } 
    $im_nv = imagecreatetruecolor($width_nv, $height_nv);
    switch ($type) {
        case '3':
            imagealphablending($im_nv, false);
            imagesavealpha($im_nv, true); 
            $im_or = imagecreatefrompng($im_or);
            imagealphablending($im_or, true);
            header("Content-Type: image/png");
            imagecopyresampled($im_nv, $im_or, 0, 0, 0, 0, $width_nv, $height_nv, $width, $height);
            imagepng($im_nv, $dir_nv);
            break;
        case '1':
            $im_or = imagecreatefromgif($im_or);
            header("Content-Type: image/gif");
            imagecopyresampled($im_nv, $im_or, 0, 0, 0, 0, $width_nv, $height_nv, $width, $height);
            imagegif($im_nv, $dir_nv);
            break;
        default:
            $im_or = imagecreatefromjpeg($im_or);
            header("Content-Type: image/jpeg");
            imagecopyresampled($im_nv, $im_or, 0, 0, 0, 0, $width_nv, $height_nv, $width, $height);
            imagejpeg($im_nv, $dir_nv, 100);
    }

    imagedestroy($im_nv);
}