1

我有一个上传脚本,效果很好,但我希望它上传两次,一种是原始格式,另一种是 thubm 大小。

我在 google 和 stackoverflow 上做了 allready 搜索,我尝试了 allready 的东西,但我没有得到它的工作。

我的上传脚本

    // If you want to ignore the uploaded files, 
    // set $demo_mode to true;

$demo_mode = false;
$upload_dir = 'uploads/';
$allowed_ext = array('jpg','jpeg','png','gif');

include('./../includes/core.php');

if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
    exit_status('Error! Wrong HTTP method!');
}


if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){

    $pic = $_FILES['pic'];

    if(!in_array(get_extension($pic['name']),$allowed_ext)){
        exit_status('Alleen '.implode(',',$allowed_ext).' bestanden zijn toegestaan');
    }   

    if($demo_mode){

        // File uploads are ignored. We only log them.

        $line = implode('       ', array( date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name']));
        file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);

        exit_status('Uploads are ignored in demo mode.');
    }


    // Move the uploaded file from the temporary 
    // directory to the uploads folder:

    $name = $pic['name'];
    $sname = hashing($name);
    $datum = date("d-m-Y"); 
    if(move_uploaded_file($pic['tmp_name'], $upload_dir.$sname)){
        mysql_query("INSERT INTO foto VALUES ('','".$pic['name']."','".$sname."', '".$datum."', '0')");
        exit_status('Bestand succesvol geupload');
    }

}

exit_status('Er is iets mis gegaan!');


// Helper functions

function exit_status($str){
    echo json_encode(array('status'=>$str));
    exit;
}
function hashing($naam){
    $info = pathinfo($naam);
    $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
    $hash = basename($naam, $ext);
    $hash = $hash . genRandomstring();
    $hash = md5($hash);
    $hash = $hash . '-' . genRandomstring();
    return  $hash . $ext;
}
function genRandomString() {
$length = 5;
$string = "";
$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-+!@"; // change to whatever characters you want
while ($length > 0) {
    $string .= $characters[mt_rand(0,strlen($characters)-1)];
    $length -= 1;
}
return $string;
}



function get_extension($file_name){
    $ext = explode('.', $file_name);
    $ext = array_pop($ext);
    return strtolower($ext);
}
?>

如果有人可以帮助我?我会很高兴,因为我已经尝试了一个星期,我可以把它拿出来。

谢谢,克里斯

4

2 回答 2

0

如前所述,您无需上传两次,上传后复制并调整图像大小,如下所示:

    $pathToImages = "path/to/images"
    $pathToThumbs = "path/to/thumbs"
    $fname = "image-file-name";
    $new_fname = "thumb-file-name";
    $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
    $width = imagesx( $img );
    $height = imagesy( $img );
    $thumbWidth = //someValue//;
    $thumbHeight = //someValue//;

// calculate thumbnail size
    $new_height = floor($height * ($thumbWidth/$width));
    $new_width = $thumbWidth;

    // create a new temporary image
    $tmp_img = imagecreatetruecolor($thumbWidth, $thumbHeight);


    // copy and resize old image into new image 
    imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height, $width, $height );

    // save thumbnail into a file
    imagejpeg( $tmp_img, "{$pathToThumbs}{$new_fname}" );
于 2013-03-04T16:20:06.917 回答
0
  1. 您将图像上传一次,这就是您所需要的,然后使用它来创建第二个缩略图文件。
  2. 尝试查看GD 文档
于 2013-03-04T16:06:10.850 回答