我正在尝试将裁剪的图像上传到 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’ ) ;


声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。