SimplePhoto
Handling photos in your web application has never been so simple., (*1)
, (*2)
Installation
Through Composer, (*3)
{
"require": {
"morrelinko/simple-photo": "0.*"
}
}
Create the database using the schema below for the data store you will be using, (*4)
Uploading Photo
$photoId = $simplePhoto->uploadFromPhpFileUpload($_FILES["image"]);
// Or
$photoId = $simplePhoto->uploadFromFilePath("/path/to/photo.png");
With support for accepting uploads from different sources., (*5)
$photoId = $simplePhoto->upload(new YourUploadSource($imageData));
The two upload methods shown above actually are aliases/shortcuts for doing this, (*6)
$photoId = $simplePhoto->upload(new PhpFileUploadSource($_FILES["image"]));
// Or
$photoId = $simplePhoto->upload(new FilePathSource("/path/to/photo.png"));
Retrieving Photo
$photo = $simplePhoto->get($photoId);
$photo->id();
$photo->url();
$photo->path();
$photo->fileMime();
$photo->storage();
$photo->fileSize();
$photo->fileExtension();
$photo->filePath();
$photo->createdAt();
...
Setup
SimplePhoto requires..., (*7)
- Storage Manager: For Storing & Managing registered storage adapters.
- Data Store: Database for persisting information about a photo.
use SimplePhoto\Storage\LocalStorage;
use SimplePhoto\StorageManager;
use SimplePhoto\DataStore\SqliteDataStore;
use SimplePhoto\SimplePhoto;
// Create a local storage adapter
$localStorage = new LocalStorage('/path/to/project/root/', 'photos');
// Create a storage manager
$storageManager = new StorageManager();
// Adds one or more registered storage adapters
$storageManager->add('local', $localStorage);
// Create Data Store
$dataStore = new SqliteDataStore(['database' => 'photo_app.db']);
// Create Our Simple Photo Object
$simplePhoto = new SimplePhoto($storageManager, $dataStore);
If you want to get a re-sized photo, use the "transform" options of the second argument, (*8)
$photo = $simplePhoto->get($photoId, [
'transform' => [
'size' => [200, 200]
]
]);
The default transformation options available..., (*9)
[
'size' => [$width, $height]
'rotate' => [$angle, ($background)]
]
You could implement your own transformer and add more transformation options, (*10)
Arguments in parenthesis are optional, (*11)
Collection of photos
$photos = $simplePhoto->collection([2, 23, 15]);
$photos->get(0); // gets photo '2'
$photos->get(1); // gets photo '23'
PhotoCollection come with a handful of methods for manipulating its items, (*12)
// Creates a collection of photos
$photos = $simplePhoto->collection([2, 23, 15, 34, 21, 1, 64, 324]);
// Gets all as array
$allPhotos = $photos->all();
// Uses filter() method.
// This example creates a new photo collection containing only photos in 'local' storage
$localPhotos = $photos->filter(function($photo) {
return $photo->storage() == 'local';
});
var_dump($localPhotos);
Push
// Probably gotten from a db
$users = [
['user_id' => 1, 'name' => 'John Doe', 'photo_id' => 2],
['user_id' => 2, 'name' => 'Mary Alice', 'photo_id' => 5]
];
$simplePhoto->push($users, array('photo_id'));
var_dump($users);
// Sample Output:
[
['user_id' => 1, 'name' => 'John Doe', 'photo_id' => 2, 'photo' => (Object SimplePhoto\PhotoResult)],
['user_id' => 2, 'name' => 'Mary Alice', 'photo_id' => 5, 'photo' => (Object SimplePhoto\PhotoResult)]
];
If you would like complete control on what is pushed to the array from the photo result,, (*13)
you specify a callback as third argument to push(), (*14)
$simplePhoto->push($users, array('photo_id'), function(&$item, $photo, $index, $name) {
$item['photo_url'] = $photo->url();
});
var_dump($users);
// Sample Output:
[
['user_id' => 1, 'name' => 'John Doe', 'photo_id' => 2, 'photo_url' => 'http://example.com/files/2014/xxxxx.png'],
['user_id' => 2, 'name' => 'Mary Alice', 'photo_id' => 5, 'photo_url' => 'http://example.com/files/2014/xxxxx.png']
];
Supported Photo Sources
Supported Data Stores
Supported Storage
TODO
Credits
This code is principally developed and maintained by [Laju Morrison] (https://github.com/morrelinko), (*15)
Licence
The MIT License (MIT). Please see License File for more information., (*16)
Supported by http://contactlyapp.com, (*17)