digitaldream / photo
Image manager
Installs: 391
Dependents: 0
Suggesters: 0
Security: 0
Stars: 33
Watchers: 3
Forks: 1
Open Issues: 12
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.8.1
- intervention/image: ^3.5
- jenssegers/imagehash: ^0.4.2
- laravel/framework: ^10.0|^11.0
Suggests
- ext-curl: *
- ext-exif: *
- dev-master
- 4.2.0
- 4.1.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.3.7
- 3.3.6
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.0.1
- 2.0.0
- 1.1.0
- 1.0.0
- dev-dependabot/npm_and_yarn/json5-1.0.2
- dev-dependabot/npm_and_yarn/express-4.18.2
- dev-dependabot/npm_and_yarn/qs-6.5.3
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/loader-utils-1.4.2
- dev-dependabot/npm_and_yarn/minimatch-3.1.2
- dev-dependabot/npm_and_yarn/terser-4.8.1
- dev-dependabot/npm_and_yarn/async-2.6.4
- dev-dependabot/npm_and_yarn/minimist-1.2.6
- dev-dependabot/npm_and_yarn/follow-redirects-1.14.8
- dev-dependabot/npm_and_yarn/dns-packet-1.3.4
- dev-analysis-rdg2gg
- dev-api
This package is auto-updated.
Last update: 2024-11-18 13:08:36 UTC
README
Laravel Photo Manager
Installation
Step one
composer require digitaldream/photo
Step Two Run Migration
php artisan migrate
Step Three
php artisan vendor:publish --provider="Photo/PhotoServiceProvider"
It will publish config, views file. Feel free to edit these files according to your project need.
Step Four
Browse /photo/photos to start using this library
Configure Policy
You can configure who can have what permissions on this photo library.
Create a new class and extends it from Photo\Policies\PhotoPolicy
like below.
namespace App\Policies; use Photo\Policies\PhotoPolicy as Policy; class PhotoPolicy extends Policy { /** * @param \App\Models\User $user * * @return bool */ public function viewAny($user): bool { return $user->isTeacher(); } }
As you can see we override viewAny method. Now a Teacher can view list of all photos.
Other methods like before
,view
,create
,update
,delete
can be override too.
Now to register this Policy class lets change policy
key on config/photo.php
#file config/photo.php
'policy' => \App\Policies\PhotoPolicy::class,
Features
1.Drag and Drop from Web.
2.Drag and Drop from Local machine
3.Crop and Resize
4.Webp conversion
5.Copy image URL and share to the Web
7.Size configurable and thumbnails generation
8.SEO friendly filename.
9.Translation
10.PHPunit test classes included.
How to use in a Model as BelongsTo
First of all you need to put a line on your model migration for example posts.
$table->foreign('photo_id')->references('id')->on('photo_photos')->onDelete('set null');
Secondly you need to define relation on your model.
/** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function photo() { return $this->belongsTo(\Photo\Models\Photo::class, 'photo_id'); }
Third. Lets make upload on controller.
/** * @var \Photo\Repositories\PhotoRepository */ protected $photoRepository; public function __construct(PhotoRepository $photoRepository) { $this->photoRepository = $photoRepository; } public function store(StoreRequest $request) { //Your other code. $post->photo_id = $this->photoRepository->create($file, ['caption' => $data['title']])->id; }
You must resolve \Photo\Repositories\PhotoRepository
via __construction
Dependency injection.
Finally. Its time to render image to view.
{!! $post->photo->renderThumbnails() !!}
This will render following html code.
<picture> <source type="image/webp" srcset="https://YourSite.com/storage/posts/thumbnails/nice-quietly-their-belong-place-on-it-the-appeared-to.webp"> <img src="https://YourSite.com/storage/posts/thumbnails/nice-quietly-their-belong-place-on-it-the-appeared-to.jpeg" alt="Nice, quietly their belong, place on. It the appeared to"> </picture>
Above code will render thumbnails in both webp and uploaded extension. To render larger image do following
{!! $post->photo->render('card-img-top') !!}
Here render method take class name as first argument and style as second.
How to upload file and get file path only.
namesapce App\Repositories; class PostRepository { /** * @var \Photo\Services\PhotoService */ protected PhotoService $photoService; public function __construct(PhotoService $photoService) { $this->photoService = $photoService; } public function store(Request $request) { $post = new Post(); $post->fill($request->all()); $mainImageFolder = "posts" $thumbnailWidth = 220; $thumbnailHheight= 200; $crop="no"; // "yes" will resize image automatically based on your maximum height,width. $thumbnailPath = "thumbnails"; // Thumbnails path are relative to main Image folder. //In this case it will create a folder thumbnails under posts folder. $post->image = $this->photoService ->setDimension($thumbnailWidth, $thumbnailHheight, $thumbnailPath) ->store($mainImageFolder, $request->file('file'), $post->title, $crop) ->convert() ->getStoredImagePath(); $post->save(); } }