Skip to content
This repository has been archived by the owner on Dec 9, 2023. It is now read-only.

Added variables to the template classes in the config file #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/Intervention/Image/ImageCacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Intervention\Image;

use Closure;
use Illuminate\Http\Request;
use Intervention\Image\ImageManager;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Response as IlluminateResponse;
Expand All @@ -18,8 +19,10 @@ class ImageCacheController extends BaseController
* @param string $filename
* @return Illuminate\Http\Response
*/
public function getResponse($template, $filename)
public function getResponse(Request $request, $filename)
{
$template = $request->segment(substr_count(config('imagecache.route'), '/')+2);

switch (strtolower($template)) {
case 'original':
return $this->getOriginal($filename);
Expand All @@ -41,19 +44,19 @@ public function getResponse($template, $filename)
*/
public function getImage($template, $filename)
{
$template = $this->getTemplate($template);
list($template, $args) = $this->getTemplate($template);
$path = $this->getImagePath($filename);

// image manipulation based on callback
$manager = new ImageManager(Config::get('image'));
$content = $manager->cache(function ($image) use ($template, $path) {
$content = $manager->cache(function ($image) use ($template, $args, $path) {

if ($template instanceof Closure) {
// build from closure callback template
$template($image->make($path));
$template($image->make($path), $args);
} else {
// build from filter template
$image->make($path)->filter($template);
$image->make($path)->filter($template, $args);
}

}, config('imagecache.lifetime'));
Expand Down Expand Up @@ -98,16 +101,24 @@ public function getDownload($filename)
*/
private function getTemplate($template)
{
$template = config("imagecache.templates.{$template}");
$args = array();
$data = config('imagecache.templates.'.$template);

if(is_array($data)){
$class = $data['class'];
$args = isset($data['args'])?$data['args']:array();
}else{
$class = $data;
}

switch (true) {
// closure template found
case is_callable($template):
return $template;
case is_callable($class):
return [$class, $args];

// filter template found
case class_exists($template):
return new $template;
case class_exists($class):
return [new $class, $args];

default:
// template not found
Expand Down