【问题标题】:Resizing Image on client-side before submitting to PHP to Upload在提交到 PHP 上传之前在客户端调整图像大小
【发布时间】:2017-10-30 03:51:11
【问题描述】:

嗯,你好。

我目前正在使用来自 here 的这个 javascript 库来调整客户端上的图像大小。我已经使用以下代码成功调整了客户端的图像大小:

document.getElementById('foto_select').onchange = function(evt) {
  ImageTools.resize(this.files[0], {
    width: 623, // maximum width
    height: 203 // maximum height
  }, function(blob, didItResize) {
    // didItResize will be true if it managed to resize it, otherwise false (and will return the original file as 'blob')
    document.getElementById('preview').src = window.URL.createObjectURL(blob);
    var hidden_elem = document.getElementById("foto");
    hidden_elem.value = window.URL.createObjectURL(blob);
    // you can also now upload this blob using an XHR.
  });
};
<script src="https://gist.githubusercontent.com/dcollien/312bce1270a5f511bf4a/raw/155b6f5861e844310e773961a2eb3847c2e81851/ImageTools.js"></script>


<form action="save.php?p=edit_headline" method="post" enctype="multipart/form-data">

<div align="center">
  <input type="file" id="foto_select" name="foto_select" />
  <input type="hidden" id="foto" name="foto" />
  <div class="spacer-20"></div>
  Preview : <br/>
  <img id="preview" width="240" height="240" />
</div>

</form>

好吧,图像已成功上传并在客户端调整大小。但问题是我需要将存储在客户端表单上的文件提交到 php 服务器端处理。 BLOB 数据存储在称为 foto 的隐藏输入中。

这是我的 php 代码:

<?php

    $imgFile = $_FILES['foto']['name'];
    $tmp_dir = $_FILES['foto']['tmp_name'];
    $imgSize = $_FILES['foto']['size'];

    $upload_dir = '../admin/images/headline/'; // upload directory

    $imgExt = strtolower(pathinfo($imgFile, PATHINFO_EXTENSION)); // get image extension

    // valid image extensions
    $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions

    // rename uploading image
    $photo = rand(1000, 1000000) . "." . $imgExt;

    // allow valid image file formats
    if (in_array($imgExt, $valid_extensions)) {
        // Check file size '5MB'
        if ($imgSize < 5000000) {
            move_uploaded_file($tmp_dir, $upload_dir . $photo);
        } else {
            $errMSG = "Sorry, your file is too large.";
            echo "<script>alert('File foto terlalu besar'); window.location ='berita.php' </script>";
        }
    } else {
        $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    }

    if (empty($imgFile)) {
        $photo_save = $foto_old;
    } else {
        $photo_save = $photo;
        unlink("images/headline/". $foto_old); // upload directory
    }

    $query = mysqli_query($con, "UPDATE `headline` SET `photo` = '$photo_save' WHERE `id` = '$id'");
    if ($query) {
        echo "<script>alert('Headline Updated'); window.location ='index.php' </script>";
    } else {
        echo "<script>alert('Failed'); window.location ='index.php' </script>";
    }

?>

照片在 mysql 数据库中更新,但文件没有从客户端输入表单复制到服务器文件夹目标。

我在这个问题上需要帮助,我可能会避免使用 XHR / AJAX 方法,因为它会改变整个代码。任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: javascript php jquery html ajax


    【解决方案1】:

    由于您在 blob 字段中有文件,只需使用 file_put_contents 函数将文件数据保存到所需的目的地,如

    file_put_contents('/path/to/new/file_name', $_POST['foto']);
    

    不需要整个move_uploaded_file 部分。只需将其视为正常请求并使用 foto 字段中的值来保存图像。

    【讨论】:

    • 但是输入表单呢?我是否需要将输入类型隐藏更改为其他内容才能提交文件?
    • 不,隐藏字段的提交与非隐藏字段相同。
    • 我按照你的建议使用了这个代码:$foto = $_POST['foto']; file_put_contents('../admin/images/headline/'.$foto, $foto);文件无法复制到所需位置。
    • 您得到的确切错误是什么? $foto 包含 blob 信息,因此不要将其用作文件名。此外,打开错误报告以检查错误。
    • 这是错误:警告:file_put_contents(../admin/images/headline/blob:localhost/5b830ce6-3eb7-45b7-83f4-8674311cc29c):无法打开流:无效参数。如何将 blob 转换为单个文件并将其复制到服务器文件夹?
    猜你喜欢
    • 2013-11-29
    • 2018-09-20
    • 2015-09-22
    • 2016-01-15
    • 1970-01-01
    • 2011-04-16
    • 2015-12-05
    • 1970-01-01
    • 2011-01-26
    相关资源
    最近更新 更多