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

时间:2012-08-29 19:22:52

标签: 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 = $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);

2 个答案:

答案 0 :(得分:1)

在我看来,您在创建图片资源之前使用的是$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);

答案 1 :(得分:0)

你的意思是你想这样:

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