Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

morrelinko/simple-photo

Repository files navigation

SimplePhoto

Handling photos in your web application has never been so simple.

Build Status

Installation

Through Composer

{
    "require": {
        "morrelinko/simple-photo": "0.*"
    }
}

Create the database using the schema below for the data store you will be using

Uploading Photo

$photoId = $simplePhoto->uploadFromPhpFileUpload($_FILES["image"]);
// Or
$photoId = $simplePhoto->uploadFromFilePath("/path/to/photo.png");

With support for accepting uploads from different sources.

$photoId = $simplePhoto->upload(new YourUploadSource($imageData));

The two upload methods shown above actually are aliases/shortcuts for doing this

$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...

  • 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);

Get photos ( Transformation)

If you want to get a re-sized photo, use the "transform" options of the second argument

$photo = $simplePhoto->get($photoId, [
	'transform' => [
		'size' => [200, 200]
	]
]);

The default transformation options available...

[
    'size' => [$width, $height]
    'rotate' => [$angle, ($background)]
]

You could implement your own transformer and add more transformation options

Arguments in parenthesis are optional

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

// 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,

you specify a callback as third argument to push()

$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

  • Add MongoDB Data Store

Credits

This code is principally developed and maintained by [Laju Morrison] (https://github.com/morrelinko)

Licence

The MIT License (MIT). Please see License File for more information.

Supported by http://contactlyapp.com

About

Handling photos in your web application has never been so simple.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages