doukuo9116 2019-03-14 15:11 采纳率: 100%
浏览 427
已采纳

使用laravel将裁剪后的图像保存在public / uploads文件夹中

I am trying to upload the image I cropped to the public/uploads folder in laravel. I am able to save the image path to the database, but I am not able to save the cropped image to the public/uploads folder.

This is how my code looks like:

 $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');
            }

After the crop function I am trying to put the cropped image into a variable and then putting the image in the public/uploads folder.

  • 写回答

1条回答 默认 最新

  • dou70260 2019-03-14 15:49
    关注

    I would really recommend using image intervention package which is extremely handy and clean in such implementations.

    See here for installation, its pretty straightforward.

    And then to crop you can easily use crop functions of the package :

    <?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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上
  • ¥20 100元python和数据科学实验项目
  • ¥15 根据时间在调用出列表
  • ¥15 R 包chipseeker 安装失败
  • ¥15 Veeam Backup & Replication 9.5 还原问题
  • ¥15 vue-print-nb
  • ¥15 winfrom的datagridview下拉框变成了黑色,渲染不成功
  • ¥20 利用ntfy实现短信推送