根据代码创建两个缩略图而不是一个缩略图

时间:2012-08-21 20:56:30

标签: php

我有这个代码,它生成一个缩略图,nw,我需要的是它生成我两个... 只是为了使功能翻倍,你能否告诉我如何根据这个来做呢?

谢谢..

function tamano_nuevo_foto($im_or, $ancho_nv, $dir_nv) {
    $img   = imagecreatefromjpeg($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;
        $alto_nv = round($ancho_nv * $prop);
    } 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);
}

if (!empty($_FILES)) {
    $tempFile   = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetPath = str_replace('//', '/', $targetPath);
    $targetFile = $targetPath . basename($_FILES['Filedata']['name'], '.' . $ext) . '_s.';
    tamano_nuevo_foto($tempFile, 120, $targetFile);
    echo str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetFile);
}

2 个答案:

答案 0 :(得分:1)

您可以像下面的示例一样调用该函数两次吗?

if (!empty($_FILES)) {
    $tempFile   = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
    $targetPath = str_replace('//', '/', $targetPath);
    $targetFile = $targetPath . basename($_FILES['Filedata']['name'], '.' . $ext) . '_s.';
    $newTargetFile = // define whatever you want to call your second copy of thumbnail here
    tamano_nuevo_foto($tempFile, 120, $targetFile);
    tamano_nuevo_foto($tempFile, 120, $newTargetFile);
    echo str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetFile);
}

答案 1 :(得分:0)

只需调用该函数两次,或使用copy()

复制该文件