2016-05-29 161 views
0

我想上传,转换并存储在Laravel使用图像Magick图像。存储上传的图像与Laravel 5

App\Http\Controllers\ArticleController

$image = $this->storeMainImage($request->file('thumbnail')); 

功能:

private function storeMainImage($file) { 
    $folder = 'uploads/images/'; <--- ????? 
    $code = uniqid(); 
    $thumb_code = $folder . 'thumb_' . $code . '.jpg'; 
    $image_code = $folder . $code . '.jpg'; 
    if(@is_array(getimagesize($file))){ 
    exec('convert '.$file.' -thumbnail 225x225^ -gravity center -extent 225x225 -compress JPEG -quality 70 -background fill white -layers flatten -strip -unsharp 0.5x0.5+0.5+0.008 '.$thumb_code); 
    exec('convert '.$file.' -compress JPEG -quality 70 -background fill white -layers flatten -strip -unsharp 0.5x0.5+0.5+0.008 '.$image_code); 
    return $image_code; 
    } else { 
    return false; 
    } 
} 

我没有得到任何这方面的错误,但我不知道,如果它的实际上传的文件,并在那里abouts它的存储它。

+1

试图改变自己的'$ folder'为'public_path ('uploads/images /')',然后在'p'中查找图像ublic/uploads/images'目录。 –

+0

或者,您可以将它们存储在您的存储目录中,并使用路线将它们显示回来,如果您不希望它们可公开访问的话。 – Ohgodwhy

回答

0

要使用Image Magick,首先必须确定服务器是否具有该模块。否则你可以安装它。 See this to install imagemagick

否则,您可以通过在配置文件夹中配置image.php文件来使用gd。 GD是默认提供

因为$folder = 'uploads/images/'; <--- ?????也没有指定的起点,起点将是你在哪里运行脚本时。因此,要确保存储路径,如果要分别存储在storagepublic文件夹中,则应使用storage_path()public_path()来定义路径。根据您使用的版本,您需要使用Check here for more paths available。给定链接是为Laravel 5.2。您可以更改页面右上角的版本。

1

$请求 - >文件()可以返回:\的Symfony \分量\ HttpFoundation \文件\ UploadedFile的阵列

你应该在处理之前检查它。只是的var_dump($文件)DD($文件)倾倒。不确定,但它不应该是字符串。

使用public_path()为$文件夹变量,它将帮助您在将来避免任何问题。

而且还检查了这真棒包Laravel:http://image.intervention.io/getting_started/introduction

+0

那个包太棒了!不知道如何在Mac OSX上安装ImageMagick,但默认情况下使用GD,所以很好。真的很容易使用,谢谢。 – frosty

0

我做了一个脚本,支持上传中Laravel,它使用Image Intervention封装中的多个图像/文件。这可能对你和其他人有用。我添加了一些评论并删除了不必要的代码,以更好地理解发生的事情。

HTML标记

<form method="post" action="{{ route('admin.upload.store') }}" enctype="multipart/form-data"> 
    {{!! csrf_field() !!}} 
    <input type="file" name="files[]" accept="image/gif, image/jpeg, image/png"> 
    <button type="submit">Upload image(s)</button> 
</form> 

UploadController

使用Laravel的内置REST的资源控制路线

/** 
* Store a newly created resource in storage. 
* Supports uploading multiple files at once. 
* 
* @param Request $request 
* @return Response 
*/ 
public function store(Request $request) 
{ 
    // Loop through the files array 
    foreach($request->file('files') as $file) { 

     // Validate each file, we want images only 
     $validator = Validator::make(compact('file'), [ 
      'files' => 'mimes:jpeg,bmp,gif,png' 
     ]); 

     if ($validator->fails()) { 
      return redirect()->route('admin.upload.index')->withErrors($validator); 
     } 

     // Create a new upload model for the file 
     $upload = new Upload([ 
      'name'  => pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) . '_' . Str::random(2), 
      'extension' => strtolower($file->getClientOriginalExtension()), 
      'mimetype' => $file->getMimeType(), 
      'size'  => $file->getSize(), 
     ]); 

     // Create the image 
     $file = Image::make($file)->widen(1200, function($constraint) { 
      $constraint->upsize(); // Prevent upsizing the image if doesn't exceed the maximum width 
     })->encode($upload->extension); 

     // Store it within 'storage/app/uploads' 
     Storage::disk('uploads')->put($upload->fullName(), $file); 

     // Save the upload file details in the database 
     $upload->save(); 
    } 

    return redirect()->route('admin.upload.index')->with(['success' => 'Files uploaded']); 
}