图片上传脚本,我如何限制高度和宽度的像素比例

时间:2015-03-14 12:28:48

标签: php file-upload image-uploading

这是我目前的图片上传脚本,如何在此代码块中添加高度的像素比限制和图片的宽度?

我自己并不确定如何做到这一点,所以我真的希望你们中的一些人知道答案,并且可以通过我能理解的方式向我解释。

<?php

$id = $_POST["mon_id"];

$mon_i_img = "";

if($_FILES['mon_img']['error'] == 0){
    $target_dir = "../img/months/";
    $target_file_only = basename($_FILES["mon_img"]["name"]);
    $target_file = $target_dir . $target_file_only;
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image

        $check = getimagesize($_FILES["mon_img"]["tmp_name"]);
        if($check !== false) {
            echo "<div class='success-box'>File is an image - " . $check["mime"] . ".</div>";
            $uploadOk = 1;
        } else {
            echo "<div class='error-box'>File is not an image..</div>";
            $uploadOk = 0;
        }

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "<div class='error-box'>Sorry, file already exists.</div>";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["mon_img"]["size"] > 2000000) {
        echo "<div class='error-box'>Sorry, your file is too large.</div>";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
        echo "<div class='error-box'>Sorry, only JPG, JPEG & PNG files are allowed.</div>";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "<div class='error-box'>Sorry, your file was not uploaded.</div>";
    // if everything is ok, try to upload file
    } else {

        if (move_uploaded_file($_FILES["mon_img"]["tmp_name"], $target_file)) {    
            echo "<div class='success-box'>The file ". $target_file_only . " has been uploaded.</div>";
        } else {
            echo "<div class='error-box'>Sorry, there was an error uploading your file.</div>";
        }

    }

    if ($uploadOk == 1) {
        $mon_i_img = $target_file_only;
    }

}

$mon_i_title = $_POST["mon_title"];
$mon_i_by = $_POST["mon_by"];

if (empty($mon_i_title) || empty($mon_i_by)) {
    header ("Location: panel.php?page=monthspicture.php&create=empty");
    exit;
}

$id = $mysqli->real_escape_string($id);
$mon_i_title = $mysqli->real_escape_string($mon_i_title);
$mon_i_by = $mysqli->real_escape_string($mon_i_by);

if ($mon_i_img != "") {

    $sql_i = "INSERT INTO ************* (mon_title, mon_by, mon_img) VALUES('$mon_i_title', '$mon_i_by', '$mon_i_img')";
    if (!$mysqli->query($sql_i)) {
        echo "Insert failed: (" . $mysqli->errno . ") " . $mysqli->error;
        die();
    }else{
        echo "<div class='success-box'>Your post have been created.</div>";
    }

}else{

        $sql_i = "";
        echo "<div class='error-box'>Your post have not been created, missing image.</div>";

}
?>

我试过这个,但它可能不起作用:

// Check file proportions
$image_info = getimagesize($_FILES["sli_img"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];

echo $image-width;
echo $image-height;

if ($image_width =! 960 && $image_height =! 300) {
    echo "<div class='error-box'>Sorry, your file proportions (MAX: Width = 960px, Height = 300px) are too large.</div>";
    $uploadOk = 0;
}

1 个答案:

答案 0 :(得分:1)

想办法解决我的问题,如果高度和宽度不合适,可以停止上传图片。

// Check file proportions
$image_info = getimagesize($_FILES["mon_img"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];

if ($image_width != 960 && $image_height != 540) {
    echo "<div class='error-box'>Sorry, your file proportions (MAX: Width = 960px, Height = 540px) are too large.</div>";
    $uploadOk = 0;
}