elefant
elefant copied to clipboard
Better image processing and storage for developers
Elefant needs more elegant image processing and storage. Currently, this is up to the implementor and is handled with the Image::resize()
method, often as a template filter. This requires that the original file be left in place, to compare the timestamps and regenerate the thumbnail as needed.
A better solution should integrate with the models themselves, similarly to how Paperclip does with ActiveRecord and Rails.
Building on the $fields
property, which is currently used for model relationships, an image might be defined like this:
<?php
class Member extends Model {
public $fields = array (
'avatar' => array (
'is_a' => 'Image',
'styles' => array (
'default' => array (300, 300, 'stretch', 'png'),
'thumb' => array (120, 120)
),
'keep_original' => false,
'default' => '/files/default_avatar.png'
)
);
}
$m = new Member (123);
// handle an uploaded file
$m->avatar ($_FILES['avatar']);
// ensure the image id is saved
$m->put ();
// get the file id and urls
echo $m->avatar; // -> 456
echo $m->avatar (); // -> '/cache/thumbs/678c...ac52-stretch-300x300.png'
echo $m->avatar ('thumb'); // -> '/cache/thumbs/678c...ac52-cover-120x120.jpg'
?>
The Image
class would be expected to provide an interface to Model
that would handle the image correctly based on the settings in the $fields
array, storing only an ID for that image in the field itself.
Behind the scenes, the Image
class would store the image info in a simple table like this:
create table #prefix#image (
id integer primary key,
added datetime not null,
status int not null,
styles text not null
);
The status could be used to queue images for transfer to S3 or another external storage service. 0 could be unprocessed, 1 could be resizing, 2 could be transferring, and 3 could be processed.
The styles would be a JSON encoded object with links to the local or remote URL of an image for each style, for example:
{
"default": "/cache/thumbs/678c...ac52-stretch-300x300.png",
"thumb": "/cache/thumbs/678c...ac52-cover-120x120.jpg"
}
Image
could even be an ExtendedModel
with styles
as the extended field. With caching, this could be a fast solution for image lookups.
<?php
$i = new Image (456);
echo $i->ext ('default'); // -> '/cache/thumbs/678c...ac52-stretch-300x300.png'
?>
Depending on the settings, image resizing and transfer would be moved out of the current request and into a script that would run from the command line. The Image
class is already part of the filemanager app, and since this pertains to file management, it makes sense for these settings to go in the filemanager's config. Settings might include:
- s3 credentials
- resize_in_queue = On|Off
- transfer_in_queue = On|Off
- tbd...
The new $fields
relationship is_a
could also be used to define other types of processors such as Video
down the road.