Php 如何更改函数以接受不同类型的文件?

Php 如何更改函数以接受不同类型的文件?,php,Php,我有这段代码,它可以帮助我创建一个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 =

我有这段代码,它可以帮助我创建一个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);
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);
}

在我看来,在创建图像资源之前,您正在使用
$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
案例,则无需编写两次相同的代码,这要感谢fall-through:

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

你是说你想要这样:

$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);
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);
}
函数tamano\u nuevo\u foto($im\u或,$width\u nv,$dir\u nv){
列表($width、$height、$type、$attr)=getimagesize($im_或);
$x_比率=$width_nv/$width;

如果($width,谢谢!现在已经完成了格式设置,您能看看我的问题吗?:)是的,新的格式化代码,我创建的新函数,它不会创建图像..没有显示错误,但没有创建图像,我对原始代码进行了一些修改,但我不知道我做错了什么!是的,我是说我解释了为什么我修改代码,以便能够创建不同类型的图像,但它不起作用我已经把它放好了!我想知道哪里可能错了!我想知道您是否需要所有这些函数(
imagecreatefrom[x]
)单独定义,或者如果你可以修改一个可以接受扩展名的文件。是的,很好,但我只能这样理解。我不知道如何修改它。不幸的是,当我插入一个png文件时,它会上传一个黑色文件。我的意思是它不能与png一起工作。我尝试过这个,嗯,这无法识别扩展名,请让我告诉您我是否获得了扩展路径ok!$im_或等于:$tempFile=$_FILES['Filedata']['tmp_name'];是的,也请让我更新问题,以便您查看是什么创建了函数!我已经更新了我的问题,我想我在查找文件扩展名时出错了。。