Recently, about a year ago, our team was faced with the task of choosing a PHP framework for implementing “serious” projects. Why in the end I chose Kohana, I don’t remember, most likely intuitively, bribing its simplicity, lightness and not a high entry threshold. At that time, the current version was 3.0, and we enthusiastically bit into the documentation.
In any “serious” project, the developer is faced with the task of generating reduced copies of user-uploaded images. Having read various HOW TO, we decided to use the generation of previews when requesting an image via url.
First you need to create a route:
Route::set('image_resize', 'image/resize/<width>x<height>(/<file>)', array('width' => '\d+', 'height' => '\d+', 'file' => '.+')) ->defaults(array( 'controller' => 'media', 'action' => 'resize', ));
Sample HTML code for generating a preview:
')
<img src=”/image/resize/200x200/path_to_file” >
As can be seen from the route, the controller will be called Controller_Media with the default method action_resize (). In the action, we accept the request parameters and generate the preview using the
Image module, which needs to be connected to bootstrap.php:
At the very end of the action we need to form the headlines and return the thumbnail itself:
$this->response->body(file_get_contents($file_preview)); $this->response->headers('Content-Type', File::mime_by_ext($pathinfo['extension'])); $this->response->headers('Content-Length', filesize($file_preview)); $this->response->headers('Last-Modified', date('r', filemtime($file_preview)));
Naturally, after a while, I wanted to look for ready-made solutions (at that time we had already switched to the version of Kohana 3.2). As a result, it turned out that the documentation has
examples on working with the Image module (in 3.0 this
section was empty) and there was a wonderful
Imagefly module.
This module uses the same principle of generating previews via url. For example, the link
/imagefly/w100-h150-c/path/to/image.jpg
, reduces the image in width to 100px and in height to 150px, and then makes the resulting image crop, saving it to the folder for thumbnails specified in the module configuration . By the way, Imagefly contains a lot of interesting settings, one of which is caching, which was not in our implementation.
Thus, once again we are convinced that it is easier to use ready-made developments, but, damn it, it is sometimes so pleasant to invent your own ugly, ugly, but working great ones.
And what techniques do you use to generate thumbnails?
PS A small series of articles on Kohana is planned: the organization of the structure of controllers, the “autogeneration” of the admin panel for working with typical data objects, the use of smarty, etc.