Modules

Controller_Resize
extends Controller
extends Kohana_Controller

Abstract controller class. Controllers should only be created using a Request.

Controllers methods will be automatically called in the following order by the request:

$controller = new Controller_Foo($request);
$controller->before();
$controller->action_bar();
$controller->after();

The controller action should add the output it creates to $this->response->body($output), typically in the form of a View, during the "action" part of execution.

package
Kohana
category
Controller
author
Kohana Team
copyright
© 2008-2012 Kohana Team
license
http://kohanaframework.org/license

Class declared in MODPATH/gleez/classes/controller/resize.php on line 3.

Properties

public $height

public $image_src

public Request $request

Request that created the controller

public $resize_type

public $resized_image

public $resized_image_type

public Response $response

The response that will be returned from controller

public $width

Methods

public action_image( ) (defined in Controller_Resize)

Source Code

public function action_image()
{
	$this->resize_type = $this->request->param('type', 'crop');
	$dimensions  	   = $this->request->param('dimensions', '80x80');
	list($this->width, $this->height) = explode('x', $dimensions);

	$image_src  	   = $this->request->param('file', NULL);
	$this->image_src   = (isset($_REQUEST['s']) AND !empty($_REQUEST['s'])) ? $_REQUEST['s'] : $image_src;

	$this->cache();
	if( !$this->resized_image ) return;

	// Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
	$this->response->check_cache(sha1($this->request->uri()).filemtime($this->resized_image), $this->request);

	$this->response->headers('content-type',  $this->resized_image_type);
	$this->response->body( Image::factory($this->resized_image)->render() );
	$this->response->headers('last-modified', date('r', filemtime($this->resized_image)));

} // action_image

public before( ) (defined in Controller_Resize)

Automatically executed before the controller action. Can be used to set class properties, do authorization checks, and execute other custom code.

Return Values

  • void

Source Code

public function before()
{
	$this->image_folder = DOCROOT . 'media';

	ACL::Required('access content');
	parent::before();
}

public __construct( Request $request , Response $response ) (defined in Kohana_Controller)

Creates a new controller instance. Each controller must be constructed with the request object that created it.

Parameters

  • Request $request required - Request that created the controller
  • Response $response required - The request's response

Return Values

  • void

Source Code

public function __construct(Request $request, Response $response)
{
	// Assign the request to the controller
	$this->request = $request;

	// Assign a response to the controller
	$this->response = $response;
}

public after( ) (defined in Kohana_Controller)

Automatically executed after the controller action. Can be used to apply transformation to the request response, add extra output, and execute other custom code.

Return Values

  • void

Source Code

public function after()
{
	// Nothing by default
}

private cache( ) (defined in Controller_Resize)

Source Code

private function cache()
{
	// is it a remote image?
	if($this->is_remote())
	{
		$path = $this->image_folder . '/imagecache/original';
		$image_original_name = "$path/".preg_replace('/\W/i', '-', $this->image_src);

		if(!file_exists($image_original_name))
		{
			//make sure the directory(s) exist
			System::mkdir($path);

			// download image
			copy($this->image_src, $image_original_name);
		}

		unset($path);
	}
	else
	{
		// $image_original_name = Route::get('media')->uri(array('file' => $this->image_src));
		$image_original_name = Kohana::find_file('media', $this->image_src, FALSE);
	}

	//if image file not found stop here
	if( !$this->is_valid($image_original_name) ) return FALSE;
	$this->resized_image = "$this->image_folder/imagecache/$this->resize_type/{$this->width}x{$this->height}/$this->image_src";

	if(!file_exists($this->resized_image))
	{
		//make sure the directory(s) exist
		$path = pathinfo($this->resized_image, PATHINFO_DIRNAME);
		System::mkdir($path);

		// Save the resized image to the public directory for future requests
		$image_function = ($this->resize_type === 'crop') ? 'crop' : 'resize';
		Image::factory($image_original_name)->$image_function($this->width, $this->height)->save($this->resized_image, 85);
	}

	return TRUE;

} // cache

private is_remote( ) (defined in Controller_Resize)

Source Code

private function is_remote()
{
	return strpos( strtolower($this->image_src), 'http://') !== false ;
}

private is_valid( ) (defined in Controller_Resize)

Source Code

private function is_valid($image_path)
{
	try
	{
		// get the size and MIME type of the requested image
		$size	= GetImageSize($image_path);
	} catch(Exception $e) {}

	// make sure that the requested file is actually an image
	if(!isset($size) OR !is_array($size) OR substr($size['mime'], 0, 6) != 'image/')
	{

		if($this->is_remote()) unlink($image_path);

		$this->response->status(404);
		$this->response->body('Error: requested file is not an accepted type: ' . $this->image_src);
		return false;
	}

	$this->resized_image_type = $size['mime'];

	return true;
}
Documentation comments powered by Disqus