关于 php:使用 laravel 将裁剪后的图像保存在 public/uploads 文件夹中 | 珊瑚贝

Save cropped image in the public/uploads folder with laravel


我正在尝试将裁剪的图像上传到 laravel 中的 public/uploads 文件夹。
我可以将图像路径保存到数据库,但无法将裁剪后的图像保存到 public/uploads 文件夹。

这就是我的代码的样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 $slideshow = \\App\\Slideshow::find($id);
        //This is the path to the image that’s in the database.
        $pieces = explode(“/”, $slideshow->image);
        $image = $pieces[0].“/public/”. $pieces[1].“/”. $pieces[2];
        $new_image = $pieces[0].“/public/”. $pieces[1].“/”. $pieces[2];
        $image_quality = ’95’;
        list( $current_width, $current_height ) = getimagesize($new_image);

        //This is the data that gets send from the blade form
        $x1 = $request->input(‘x1’);
        $y1 = $request->input(‘y1’);
        $x2 = $request->input(‘x2’);
        $y2 = $request->input(‘y2’);
        $width = $request->input(‘width’);
        $height = $request->input(‘height’);

        //This function is cropping the image
        $crop_width = 50;
        $crop_height = 50;
        $new = imagecreatetruecolor( $crop_width, $crop_height );
        $current_image = imagecreatefromjpeg( $new_image );
        imagecopyresampled( $new, $current_image, 0, 0, $x1, $y1, $crop_width, $crop_height, $width, $height );
        imagejpeg( $new, $new_image, $image_quality );
        $final_image = imagejpeg( $new, $new_image, $image_quality );

            if($final_image == true) {
                $image = ‘uploads/’ . $image[‘image’];
                $final_image = ‘uploads/crop_’. $pieces[2];
                $destination = ‘../uploads/’;
                $complete = $destination.$final_image;

                $slideshow->cropped_image = $complete;
                $slideshow->save();

                Storage::copy($image, $final_image);
                dd(Storage::copy($image, $final_image));

                return redirect()->back();
            } else {
                dd(‘Doesn’t work‘);
            }

在裁剪功能之后,我尝试将裁剪后的图像放入变量中,然后将图像放入 public/uploads 文件夹中。

  • 欢迎来到堆栈溢出! :) 我认为这个问题可能有一些答案,而不会对你有所帮助。我相信 Laravel 的 Storage 外观默认将文件存储在 storage/app 目录中。


我真的会推荐使用图像干预包,它在这样的实现中非常方便和干净。

安装请看这里,非常简单。

然后进行裁剪,您可以轻松使用软件包的裁剪功能:

1
2
3
4
5
6
7
8
9
10
11
<?php

    $originalImagePath = ‘//your_image_path’
    $x = $request->input(‘x’);
    $y = $request->input(‘y’);
    $width = $request->input(‘width’);
    $height = $request->input(‘height’);

    $croppedImage = Image::make($originalImagePath);              
    $croppedImage->crop($width, $height, $x, $y);
    $croppedImage->save(‘//new_path_here’);


来源:https://www.codenong.com/55166005/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9295.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?