-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31f6413
commit 0df7163
Showing
8 changed files
with
173 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\Image; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Http\Request; | ||
use App\Handlers\ImageUploadHandler; | ||
use App\Http\Resources\ImageResource; | ||
use App\Http\Requests\Api\ImageRequest; | ||
|
||
class ImagesController extends Controller | ||
{ | ||
public function store(ImageRequest $request, ImageUploadHandler $uploader, Image $image) | ||
{ | ||
$user = $request->user(); | ||
|
||
$size = $request->type == 'avatar' ? 416 : 1024; | ||
$result = $uploader->save($request->image, Str::plural($request->type), $user->id, $size); | ||
|
||
$image->path = $result['path']; | ||
$image->type = $request->type; | ||
$image->user_id = $user->id; | ||
$image->save(); | ||
|
||
return new ImageResource($image); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
class ImageRequest extends FormRequest | ||
{ | ||
public function rules() | ||
{ | ||
|
||
$rules = [ | ||
'type' => 'required|string|in:avatar,topic', | ||
]; | ||
|
||
if ($this->type == 'avatar') { | ||
$rules['image'] = 'required|mimes:jpeg,bmp,png,gif|dimensions:min_width=200,min_height=200'; | ||
} else { | ||
$rules['image'] = 'required|mimes:jpeg,bmp,png,gif'; | ||
} | ||
|
||
return $rules; | ||
} | ||
|
||
public function messages() | ||
{ | ||
return [ | ||
'image.dimensions' => '图片的清晰度不够,宽和高需要 200px 以上', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class ImageResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
class Image extends Model | ||
{ | ||
protected $fillable = ['type', 'path']; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
database/migrations/2020_08_06_234823_create_images_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateImagesTable extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('images', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->integer('user_id')->index(); | ||
$table->string('type')->index(); | ||
$table->string('path'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists('images'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters