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

Add default image (no image) option #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ $img = Image::cache(function($image) {
}, 10, true);
```

If want to use default image in your project to show that image when main image not found(like no-image thumbnail and ...) should set true related config:

```php
'useDefaultImage' => false,

'defaultImagePath' => 'path-to-default-image.jpg'
```

## License

Intervention Imagecache Class is licensed under the [MIT License](http://opensource.org/licenses/MIT).
38 changes: 23 additions & 15 deletions src/Intervention/Image/ImageCacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Intervention\Image;

use Closure;
use Intervention\Image\ImageManager;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Response as IlluminateResponse;
use Config;
use Illuminate\Http\Response as IlluminateResponse;
use Illuminate\Routing\Controller as BaseController;
use Intervention\Image\ImageManager;

class ImageCacheController extends BaseController
{
Expand All @@ -26,7 +26,7 @@ public function getResponse($template, $filename)

case 'download':
return $this->getDownload($filename);

default:
return $this->getImage($template, $filename);
}
Expand All @@ -42,7 +42,7 @@ public function getResponse($template, $filename)
public function getImage($template, $filename)
{
$template = $this->getTemplate($template);
$path = $this->getImagePath($filename);
$path = $this->getImagePath($filename);

// image manipulation based on callback
$manager = new ImageManager(Config::get('image'));
Expand All @@ -55,7 +55,7 @@ public function getImage($template, $filename)
// build from filter template
$image->make($path)->filter($template);
}

}, config('imagecache.lifetime'));

return $this->buildResponse($content);
Expand Down Expand Up @@ -108,7 +108,7 @@ protected function getTemplate($template)
// filter template found
case class_exists($template):
return new $template;

default:
// template not found
abort(404);
Expand All @@ -127,21 +127,29 @@ protected function getImagePath($filename)
// find file
foreach (config('imagecache.paths') as $path) {
// don't allow '..' in filenames
$image_path = $path.'/'.str_replace('..', '', $filename);
$image_path = $path . '/' . str_replace('..', '', $filename);
if (file_exists($image_path) && is_file($image_path)) {
// file found
return $image_path;
}
}

if (config("imagecache.useDefaultImage")) {
$defaultImagePath = config("imagecache.defaultImagePath");

if (file_exists($defaultImagePath) && is_file($defaultImagePath)) {
return $defaultImagePath;
}
}

// file not found
abort(404);
}

/**
* Builds HTTP response from given image data
*
* @param string $content
* @param string $content
* @return Illuminate\Http\Response
*/
protected function buildResponse($content)
Expand All @@ -150,16 +158,16 @@ protected function buildResponse($content)
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);

// respond with 304 not modified if browser has the image cached
$etag = md5($content);
$etag = md5($content);
$not_modified = isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag;
$content = $not_modified ? NULL : $content;
$status_code = $not_modified ? 304 : 200;
$content = $not_modified ? null : $content;
$status_code = $not_modified ? 304 : 200;

// return http response
return new IlluminateResponse($content, $status_code, array(
'Content-Type' => $mime,
'Cache-Control' => 'max-age='.(config('imagecache.lifetime')*60).', public',
'Etag' => $etag
'Content-Type' => $mime,
'Cache-Control' => 'max-age=' . (config('imagecache.lifetime') * 60) . ', public',
'Etag' => $etag,
));
}
}
18 changes: 18 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@

'lifetime' => 43200,

/*
|--------------------------------------------------------------------------
| Image Cache Default Image
|--------------------------------------------------------------------------
|
| If set true and main image not found it use default image.
|
*/

'useDefaultImage' => false,

/*
|
| If want to use default image should set path of it here.
|
*/

'defaultImagePath' => '',
);