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 文件夹中。
我真的会推荐使用图像干预包,它在这样的实现中非常方便和干净。
安装请看这里,非常简单。
然后进行裁剪,您可以轻松使用软件包的裁剪功能:
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'); |