[英]Edit and copy image in Laravel [ Can't write image data to path ]
我為項目創建了以下4個模型及其表結構:
Media.php 在應用程序中上傳圖像
媒體表結構
id | 路徑
路徑示例:uploads / images / media / food-1542110154.jpg
Post.php 創建帖子
帖子表結構
id | 標題| 內容
FeaturedImage.php 特色圖片發布
帖子表結構
id | POST_ID | 路徑
帖子模型和FeaturedImage模型是一對一的關系
UploadImage.php 調整上載圖像的大小並將其移動到另一個目錄。 該模型沒有遷移和控制器
來自PostsController.php
代碼片段來創建帖子
use App\UploadImage;
use App\Media;
class PostController extends Controller
{
private $imagePath= "uploads/images/post/";
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
$media = Media::find($request->featured);
if (!File::exists($this->imagePath)) {
File::makeDirectory($this->imagePath);
}
$upload = new UploadImage;
$image= $upload->uploadSingle($this->banner, $media->path, 400,300);
$post->image()->save(new FeaturedImage([
'path' => $image
]));
}
Session::flash('success', 'Post created sucessfully !');
return redirect()->route('post.index');
}
UploadImage.php
代碼段
use Intervention\Image\Facades\Image;
use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;
use Illuminate\Database\Eloquent\Model;
class UploadImage extends Model
{
public function uploadSingle($savePath, $image,$width,$height)
{
Image::make(public_path($image))->fit($width, $height)->save($savePath);
ImageOptimizer::optimize($savePath);
return $savePath;
}
}
在我的Laravel應用程序,我想編輯寫在法已上傳的圖片的尺寸UploadImage.php
並保存編輯后的圖像在post
目錄,並保存其路徑featured_images
表。
但是我一直在**無法將圖像數據寫入路徑**錯誤。
如果有人能指出我犯的錯誤,我將非常感謝。
請不要將此內容標記為重復內容。 由於我瀏覽了幾乎所有與此類錯誤有關的帖子,因此對我沒有幫助。
在我的UploadImage.php模型中調整了幾行,然后解決了。
public function uploadSingle($savePath, $image,$width,$height)
{
$filename = basename($image);
$location = $savePath . $filename;
Image::make($image)->save($location);
ImageOptimizer::optimize($location);
return $location;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.