The easiest thumb

it would Seem that the typical task is to populate the images directory, to prepare for each several sizes and apply random filters, but not in modx suitable, simple and user-friendly tool.

Simple implementation phpThumb generator based on a single tv and set parameters.


Example call
the
[!thumb?path=`[+thumb+]`&size=`320x240`&tpl=`thumb`!]

The original image is set the template variable [+thumb+]. The snippet [[thumb]] of the original file name (e.g. image.jpg) and settings (320x240) creates a single new file name (image.320x240.jpg). When you access the catalogue of images in accordance with the rule RewriteRule apache forwards the request handler index.php, which splits the file name back to settings and using phpThumb generates the desired image.

Snippet [[thumb]]
Takes one mandatory and two optional parameters
path path to picture file
[size] size of the generated images
[tpl] template fromline

Depending on the template, snippet or returns furnished in accordance with the pattern result or the string resulting by path

the
<?php 
if (!empty($path)) { 
$size = (empty($size)) ? $size : '320x240'; 

$path = explode('.', $path); 
array_splice($path, -1, 0, $size); 
$output = $path = implode('.', $path); 

if (!empty($tpl)) { 
$params['path'] = $path; 
$output = $modx- > parseChunk($tpl, $params, '[+', '+]'); 
} 
} 
return $output; 
?>

chunk {{thumb}}
One takes only a placeholder [+path+]

the
<img src="[+path+]">

request Handler index.php
Takes the address of the image, divides the address of the arguments and in accordance with the specified parameters generates and returns the result.

the
<?php 
if (!empty($_GET['path'])) { 
if (!file_exists($path = $_GET['path'])) { 
$image = explode('.', $path); 
$size = end(array_splice($image, -2, 1)); 
if (in_array($size, array('100x50', '200x100', '300x150'))) { 
$image = implode('.', $image); 
if (file_exists($image)) { 
list($width, $height) = explode('x', $size); 
require('phpthumb.class.php'); 
$phpThumb = new phpThumb(); 
$phpThumb- > setSourceFilename($image); 
$phpThumb- > setParameter('w', $width); 
$phpThumb- > setParameter('h', $height); 
$phpThumb- > setParameter('zc', '1'); 
$phpThumb- > setParameter('q', '100'); 

if ($phpThumb- > GenerateThumbnail()) { 
if ($phpThumb- > RenderToFile($path)) { 
return header('Location: '.$path); 
} 
} 
} 
} 
} 
} 
return header("HTTP/1.0 404 Not Found"); 
?>

the apache redirect Rule .htaccess
Starts the processing procedure of the image

the
<IfModule mod_rewrite.c> 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA] 
</IfModule>
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Briefly on how to make your Qt geoservice plugin

Database replication PostgreSQL-based SymmetricDS

Yandex.Widget + adjustIFrameHeight + MooTools