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

使用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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)