使用move_uploaded_files压缩图像php

时间:2016-06-07 08:33:49

标签: php image compression

我有一个PHP脚本从Ajax调用上传照片,我想上传两次照片,一张标准尺寸,另一张压缩。有谁知道如何使用以下代码?我尝试使用我在stackoverflow上找到的脚本压缩图像,但我无法正确执行。每次都出现黑色照片。

我收到了这段代码:

$data = array();
if (isset($_GET['files'])) {
    $error = false;
    $files = array();
    $uniqid = uniqid();
    $uploaddir = '../../images/'.$uniqid;
    foreach($_FILES as $file) {
        if (move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))) {
            $files[] = $uploaddir.$file['name'];
        } else {
            $error = true;
        }
    }
    $data = ($error) ? array('error' = > 'There was an error uploading your files') : array('files' = > $files);
} else {
    $arr - > image = $file['name'];
    $_SESSION['image'] = "img-".$file['name'];
    $arr - > ok = "ok";
    $data = array('success' = > 'Form was submitted', 'formData' = > $file['name']);
}

谢谢大家!

1 个答案:

答案 0 :(得分:0)

尝试在$ files []数组插入中使用basename():

改变这个:

World Wide Web Publishing

对此:

foreach($_FILES as $file) {
        if (move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))) {
            $files[] = $uploaddir.$file['name'];
        } else {
            $error = true;
        }
    }