简体   繁体   English

图片上传到目录并裁剪

[英]Image upload to directory and cropping it

session_start();

require '../vendor/autoload.php';

use App\Users;

$user = new Users();


?>

   <html>
   <head>
       <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
             integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
             crossorigin="anonymous">
   </head>
   <form method="post" enctype="multipart/form-data">
       <br>
       <input type="file" name="userimage" class="btn btn-outline-secondary"><br><br>
       <input type="submit" name="verzendphoto" value="Verzend" class="btn btn-primary">
   </form>
   </html>


<?php

if (isset($_POST['verzendphoto'])) {
   $target_dir = "C:/xampp/htdocs/eduflow/code/uploads/";
   $target_file = $target_dir . basename($_FILES["userimage"]["name"]);
   $uploadOk = 1;
   $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

   if ($imageFileType != "png") {
       echo "Sorry, alleen PNG bestanden.";
       $uploadOk = 0;
   }
   if (file_exists($target_file)) {
       echo "Sorry, bestand bestaat al.";
       $uploadOk = 0;
   }
   if ($uploadOk == 0) {
       echo "Sorry, je bestand is niet geupload.";
// if everything is ok, try to upload file.
   } else {
       // Moves the file to the directory.

       if (move_uploaded_file($_FILES["userimage"]["tmp_name"], $target_file)) {
           echo "The file " . basename($_FILES["userimage"]["tmp_name"]) . " has been uploaded.";
           $image = imagecreatefrompng($_FILES['userimage']['tmp_name']);
           $values = [
             'user_image' => '/eduflow/code/uploads/' . imagecrop($image, ['x' => 0, 'y' => 0, 'width' => 54, 'height' => 54]  ),
           ];
           $user->updateUserPhoto($_SESSION['user_id'], $values);
       } else {
           echo "Sorry, er was een fout bij het uploaden van uw foto.";
       }
   }
}

The file php7AD6.tmp has been uploaded.文件 php7AD6.tmp 已上传。

I get this errors我得到这个错误

Warning: imagecreatefrompng(C:\xampp\tmp\php7AD6.tmp): failed to open stream: No such file or directory in C:\xampp\htdocs\eduflow\code\admin\photo_import.php on line 51 Warning: imagecreatefrompng(C:\xampp\tmp\php7AD6.tmp): failed to open stream: No such file or directory in C:\xampp\htdocs\eduflow\code\admin\photo_import.php on line 51

Warning: imagecrop() expects parameter 1 to be resource, bool given in C:\xampp\htdocs\eduflow\code\admin\photo_import.php on line 53警告:imagecrop() 期望参数 1 是资源,布尔值在 C:\xampp\htdocs\eduflow\code\admin\photo_import.php 第 53 行中给出

The photo does upload but after that nothing happens.照片确实上传了,但之后没有任何反应。 What I would like to happen is upload it to the directory and database, after that I return it on another page.我想要发生的是将它上传到目录和数据库,然后我在另一个页面上返回它。

Because you are calling not existing file因为您正在调用不存在的文件

$image = imagecreatefrompng($_FILES['userimage']['tmp_name']);

change to改成

$image = imagecreatefrompng($target_file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM