-1

我有这段代码可以帮助我创建 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);
4

2 回答 2

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);
于 2012-08-29T19:35:29.633 回答
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);
}
于 2012-08-29T19:37:25.510 回答