2015-07-03 24 views
5

Tôi đang cố gắng lưu một hình ảnh được điều khiển mà tôi sẽ đẩy chúng vào s3.Không thể lưu hình ảnh can thiệp hình ảnh. NotWritableException trong dòng Image.php 138

Mã của tôi mà làm việc Mã này lưu ảnh trực tiếp trong thư mục công cộng *

public function store(Filesystem $filesystem) 
{ 

    $request = Input::all(); 

    $validator = Validator::make($request, [ 
     'images' => 'image' 
    ]); 

    if ($validator->fails()) { 
     return response()->json(['upload' => 'false']); 
    } 

    $postId = $request['id']; 

    $files = $request['file']; 

    $media = []; 

    $watermark = Image::make(public_path('img/watermark.png')); 

    foreach($files as $file) { 

     $image = Image::make($file->getRealPath()); 
     $image->crop(730, 547); 
     $image->insert($watermark, 'center'); 
     $image->save($file->getClientOriginalName()); 

    } 

} 

gì tôi muốn đạt được là để có thể lưu nó trong một thư mục riêng của nó. Thứ nhất là nơi tốt nhất để lưu trữ hình ảnh cho một bài viết trên blog là gì, trong lưu trữ của công thư mục? Nhưng dù sao khi tôi làm điều này:

$image->save('blogpost/' . $postId . '/' . $file->getClientOriginalName()); 

// Or this 

$image->save(storage_path('app/blogpost/' . $postId . '/' . $file->getClientOriginalName())); 

tôi nhận được lỗi:

folder within public

NotWritableException in Image.php line 138: Can't write image data to path (blogpost/146/cars/image.jpg)

or

storage path

NotWritableException in Image.php line 138: Can't write image data to path /code/websites/blog/storage/app/blogpost/146/image.jpg

Tôi đã thử

cd storage/app/ 
chmod -R 755 blogpost 

Và nó vẫn sẽ không làm việc

Cảm ơn bạn đã đọc này

Trả lời

4

Ok vì vậy đây là làm thế nào tôi giải quyết nó, tôi làm thư mục đầu tiên trước khi lưu trữ,

Storage::disk('local')->makeDirectory('blogpost/' . $postId); 

Khi thư mục được tạo ra i sau đó đi để lưu trữ các hình ảnh được thao tác như vậy:

$image->save(storage_path('app/blogpost/' . $postId . '/' . $imageName)); 

Sau đó đẩy hình ảnh vào S3

$filesystem->put('blogpost/' . $postId . '/' . $imageName, file_get_contents(storage_path('app/blogpost/' . $postId . '/' . $imageName))); 

này đã làm việc

1

Larave l 5 nhu cầu quyền ghi vào toàn bộ thư mục Storage vì vậy hãy thử tiếp theo,

sudo chmod 755 -R storage 

nếu 755 không làm việc cố gắng 777.

+0

Bạn có nghĩ điều này thực sự cần không? – ihue

2

Am cải thiện @Amol Bansode câu trả lời.

Bạn đang nhận được lỗi này vì $postId thư mục không tồn tại trong đường dẫn mà bạn chỉ định.

Bạn có thể làm điều đó như thế này:

//I suggest you store blog images in public folder 
//I assume you have created this folder `public\blogpost` 
$path = public_path("blogpost/{$postId}"); 

//Lets create path for post_id if it doesn't exist yet e.g `public\blogpost\23` 
if(!File::exists($path)) File::makeDirectory($path, 775); 

//Lets save the image 
$image->save($path . '/' . $file->getClientOriginalName()); 
1

Bạn có thể giải quyết nó bằng cách đúc biến Intervation/Image đến một dòng dữ liệu sử dụng chức năng stream. Sau đó, sử dụng mặt tiền Larama Storage để lưu hình ảnh.

$img = Image::make('path-to-the-image.png')->crop(...)->insert->stream('jpg', 90) Storage::put('where_I_want_the_image_to_be_stored.jpg', $img);

0

Trong trường hợp của tôi, tôi đã di chuyển một dự án từ của Windows 10 đến Parrot (Debian- Linux) và tôi đã cùng một vấn đề và quay ra chém là dấu gạch chéo ngược và Linux giải thích chúng một cách khác nhau. Không giống như Windows, nó không thực sự quan trọng.

Windows Code: 
$image_resize->save(public_path('\storage\Features/' .'Name'.".".'png')); 



Linux Code (Working): 
$image_resize->save(public_path('storage/Features/' .'Name'.".".'jpg')); 
Các vấn đề liên quan