Skip to content

Instantly share code, notes, and snippets.

@p208p2002
Created March 20, 2018 05:51
Show Gist options
  • Select an option

  • Save p208p2002/2bdee9c7495537554f2a4e9e85c972fe to your computer and use it in GitHub Desktop.

Select an option

Save p208p2002/2bdee9c7495537554f2a4e9e85c972fe to your computer and use it in GitHub Desktop.
<?php
namespace App\Services;
use DB;
use Exception;
class ImageService
{
public function store($imgFile){
if($imgFile==null)
return back();
$destinationPath = public_path().'/img/';
$filename = $imgFile->getclientoriginalname();
$filetype=$imgFile->getMimeType();
if($filetype!='image/jpeg'){ //格式jpg
throw new Exception("圖片格式錯誤");
}
$unique_name = md5($filename. time()).'.jpg';
$imgFile->move($destinationPath,$unique_name);
//store
$fileurl='/img/'.$unique_name;
DB::table('imgtable')->insert(
['filename' => $unique_name,
'filename_origin' => $filename,
'path'=> $fileurl]
);
return $fileurl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment