ActiveRecord class for image versioning

In case somebody finds it useful here is the ActiveRecord class I use for image versioning

The basic idea is you create an instance of Image and attach a file to it




$image = new Image();

$image->fk1 = 1;

$image->fk2 = 1;




$image->attachCUploadedFile($image_path);

// or

$image->attachFile($image_path);



The class then checks if there are predefined image resizes in your config.php, where the key represents the resize and the value represents the quality:





'params'=>array(

		'adminEmail'=>'webmaster@example.com',

        'imageResizes' => array(

            '170x110' => 100

        )

),



If there are any, it will pre-create the resizes.

The files are kept in images/{$image->fk1}/{$image->fk2}/{$image->image_id}/resize_name.extension

So in our case there will be images/1/1/1/170x110.jpg and images/1/1/1/original.jpg. All the info about the files is kept as JSON in the resizes column of the image table.

To get a resize you can use




echo $image->getResize("original"); // to get the original 

$image->getResize("100xx100"); // returns an image that is cropped to 100x100 with middle offset 


$image->getResize("100xx100 left top"); // returns an image that is cropped to 100x100 with left top offset 


$image->getResize("100x100");

$image->getResize("100x100+");

$image->getResize("100x100-"); 



If there is an existing one, it will be used, otherwise it will be created. You can force resize if you want to.

fk1 and fk2 are there to use in relational queries. By default the original file is constrained to 1920xx1080-.