簡體   English   中英

上傳前調整圖像大小

[英]Resize image before upload

所以我有一個圖像上傳腳本,問題是我想在上傳之前調整圖像的大小。 這是我現在擁有的代碼,

<?php
session_start();
$username = $_SESSION['user'];
include_once 'db.php';

define("UPLOAD_DIR", "uploads/");

$fileType = exif_imagetype($_FILES["myFile"]["tmp_name"]);
$allowed = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
if (!in_array($fileType, $allowed)) {
    header('Location: edit.php');
    exit();
} 

if (!empty($_FILES["myFile"])) {
    $myFile = $_FILES["myFile"];

    if ($myFile["error"] !== UPLOAD_ERR_OK) {
        echo "<p>An error occurred.</p>";
        exit;
    }

    // ensure a safe filename
    $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

    //Start resize 
    $filename = $myFile;

    $width = 200;
    $height = 200;

    header('Content-Type: image/jpeg');

    list($width_orig, $height_orig) = getimagesize($filename);


    $ratio_orig = $width_orig/$height_orig;

    if ($width/$height > $ratio_orig) {
        $width = $height*$ratio_orig;
    } else {
            $height = $width/$ratio_orig;
    }

    $image_p = imagecreatetruecolor($width, $height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

    imagejpeg($image_p, null, 100);


    // don't overwrite an existing file
    $i = 0;
    $parts = pathinfo($name);
    while (file_exists(UPLOAD_DIR . $name)) {
        $i++;
        $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
    }

    // preserve file from temporary directory
    $success = move_uploaded_file($myFile["tmp_name"],
        UPLOAD_DIR . $name);
    if (!$success) { 
        echo "<p>Unable to save file.</p>";
        exit;
    }
    // set proper permissions on the new file
    chmod(UPLOAD_DIR . $name, 0644);

    $upload_url = 'http://localhost/uploads/'.$name;

    $stmt = $con->prepare("UPDATE users SET profile_picture=:picture WHERE username=:username;");
    $stmt->bindValue(':picture', $upload_url, PDO::PARAM_STR);
    $stmt->bindValue(':username', $username, PDO::PARAM_STR);
    $stmt->execute();
}
?>
<a href="<?php echo $upload_url ?>">Click here</a>

這就是我得到的

警告:session_start():無法發送會話緩存限制器-行2上/Users/matt/Desktop/Likers/upload.php中已發送的標頭(輸出從/Users/matt/Desktop/Likers/upload.php:1開始)

警告:無法修改標頭信息-第32行的/Users/matt/Desktop/Likers/upload.php中已經發送過的標頭(輸出始於/Users/matt/Desktop/Likers/upload.php:1)

警告:getimagesize()期望參數1為字符串,在第34行的/Users/matt/Desktop/Likers/upload.php中給出的數組

警告:在第37行的/Users/matt/Desktop/Likers/upload.php中被零除

警告:imagecreatetruecolor():第45行的/Users///Desktop/Likers/upload.php中的圖像尺寸無效

警告:imagecreatefromjpeg()期望參數1為字符串,在第46行的/Users///Desktop/Likers/upload.php中給出的數組

警告:imagecopyresampled()期望參數1為資源,在第47行的/Users///Desktop/Likers/upload.php中給出布爾值

警告:imagejpeg()期望參數1為資源,在第49行的/Users///Desktop/Likers/upload.php中提供布爾值

我不太確定該怎么辦。 我這樣做正確嗎? 有一個更好的方法嗎? 任何幫助都會很棒。

此錯誤說明了問題:

Warning: getimagesize() expects parameter 1 to be string, array given in /Users/matt/Desktop/Likers/upload.php on line 34

您要提供整個$_FILES["myFile"]而不是文件名。 檢查第27行:

$filename = $myFile;

它應該是:

$filename = $myFile['tmp_name'];

希望能幫助到你 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM