简体   繁体   English

带有 Laravel 5.3 的 Slim 图像裁剪器

[英]Slim Image Cropper With Laravel 5.3

I have recently been picking up Laravel and getting grips with it, I have made a simple site which allows me to make and edit articles - What I am trying to achieve now is to add an image uploader to it.我最近一直在学习 Laravel 并掌握它,我制作了一个简单的网站,允许我制作和编辑文章 - 我现在想要实现的是向其中添加一个图像上传器。

I have purchased this image cropper which is responsive and is great for mobiles and desktops but when it comes to adding it to my Laravel project I am a bit stuck.我已经购买了这个图像裁剪器,它具有响应性,非常适合手机和台式机,但是当将它添加到我的 Laravel 项目时,我有点卡住了。

In the download there are a few files many being .js files and some PHP files, I read that I need to add the Slim.php file in App/ directory which I have done and I have also added the js files including making the uploader appear in the view.在下载中有一些文件,其中许多是.js文件和一些 PHP 文件,我读到我需要在App/目录中添加Slim.php文件,我已经完成了,我还添加了 js 文件,包括制作上传器出现在视图中。

Where I am stuck is when click I to add the file and click upload, It throws an error and inspecting the network its trying to send the data to articles/async.php which I've not yet added - my question is where does this go in the project as its basically just a script?我被卡住的地方是当点击我添加文件并点击上传时,它会抛出一个错误并检查网络它试图将数据发送到我尚未添加的articles/async.php - 我的问题是这在哪里进入项目,因为它基本上只是一个脚本? The information I have found doesn't tell me where I need to put this.我找到的信息并没有告诉我需要把它放在哪里。

My thoughts are I just take the script and add it to my store function and find where its looking for the script and redirect it to the store but my thoughts are that this wouldn't work as it handles the image and then once its uploaded I send the information for the store.我的想法是我只是将脚本添加到我的商店功能中,然后找到它寻找脚本的位置并将其重定向到商店,但我的想法是这在处理图像时不起作用,然后一旦上传我发送商店的信息。

If anyone is able to help that would be great like I said im a bit of a novice when it comes to Laravel so any help would be great.如果有人能够提供帮助,那就太好了,就像我说的,在 Laravel 方面我有点新手,所以任何帮助都会很棒。

I used Slim with a Laravel 5.4 application.我在 Laravel 5.4 应用程序中使用了 Slim。 In my example I am not using ajax to submit the image, it's a standard form post.在我的示例中,我没有使用 ajax 提交图像,它是一个标准的表单帖子。 Here is what I did to get it to work:这是我为使其正常工作所做的工作:

  • Copy slim.php to App/Classes (I chose to put it in a new directory called Classes, but you can put it wherever)将slim.php复制到App/Classes(我选择放在一个叫Classes的新目录下,但你可以把它放在任何地方)
  • In Slim.php you must namespace it like so: namespace App\\Classes;在 Slim.php 中,你必须像这样命名它: namespace App\\Classes;
  • In your controller, pull in the Slim class like so: use App\\Classes\\Slim;在你的控制器中,像这样拉入 Slim 类: use App\\Classes\\Slim;

For this next part, I will preface by saying I set up my Slim cropper to only handle one image.对于下一部分,我会先说我将 Slim 裁剪器设置为仅处理一张图像。 Thus, I changed the name of my input to 'avatar'.因此,我将输入的名称更改为“头像”。 Here is the HTML I'm using:这是我正在使用的 HTML:

<div class="slim" data-label="Drop profile photo here" data-size="200, 200" data-ratio="1:1">
    @if ( $user->avatar )
    <img src="{{ $user->avatar }}" />
    @endif
    <input type="file" name="avatar" />
</div>

Here is my controller:这是我的控制器:

public function avatar($id, Request $request)
{
    $user = User::findOrFail($id);

    if ( $request->avatar )
    {
        // Pass Slim's getImages the name of your file input, and since we only care about one image, use Laravel's head() helper to get the first element
        $image = head(Slim::getImages('avatar'));

        // Grab the ouput data (data modified after Slim has done its thing)
        if ( isset($image['output']['data']) )
        {
            // Original file name
            $name = $image['output']['name'];

            // Base64 of the image
            $data = $image['output']['data'];

            // Server path
            $path = base_path() . '/public/img/avatars/';

            // Save the file to the server
            $file = Slim::saveFile($data, $name, $path);

            // Get the absolute web path to the image
            $imagePath = asset('img/avatars/' . $file['name']);

            $user->avatar = $imagePath;
            $user->save();
        }
    }

    return redirect()->back()->with('success', "User's profile picture has been updated!");
}

I hope this helps我希望这有帮助

A better way than the Slim class allowing to choose between a local upload or on a remote server like Amazon S3.比 Slim 类更好的方法,允许在本地上传或远程服务器(如 Amazon S3)之间进行选择。

public function upload(Request $request)
    {
        if ($request->hasFile('slim_output_0'))
        {
            $image = $request->file('slim_output_0');

            if ($image->isValid())
            {
                $extension = $image->getClientOriginalExtension();
                $origFileName = Str::slug($image->getClientOriginalName());
                $fileName = Str::slug(Str::replaceFirst($extension, '', $origFileName)).'.jpg';
                $path = '....';

                /**
                 * PROCESS UPLOAD WITH LARAVEL
                 * Permit local upload or Amazon S3 Upload
                 */

                return response()->json([
                    'status' => 'success',
                    'name' => $fileName,
                    'path' => $path.$fileName
                ]);
            }
        }

        return response()->json([
            'status' => 'failure',
            'message' => 'Picture not found'
        ]);
    }

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

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