intervention / imagecache Goto Github PK
View Code? Open in Web Editor NEWCaching extension for the Intervention Image Class
Home Page: https://image.intervention.io/v2/usage/cache
License: MIT License
Caching extension for the Intervention Image Class
Home Page: https://image.intervention.io/v2/usage/cache
License: MIT License
This is more of a feature request but would it be possible to add remote storage lookup such as Amazons's S3. Currently its only local paths:
'paths' => array(
public_path('upload'),
public_path('images'),
),
Would be cool to be able to find images in an S3 bucket and then create the cache version based on that.
I'm trying to convert a jpg file into a png and then encode the image into a png formatted data url.
$img = Image::make($feed->data[$i]->images->low_resolution->url);
$img->greyscale();
$img->contrast(7);
$img->gamma(2);
$img->limitColors(12, '#ff9900');
$img->resize(120,120);
$img->encode('png', 2);
array_push(
$feed_imgs,
$feed_imgs_item_arr = [
'id' => $feed->data[$i]->id,
'url' => $img->encode('data-url')->encoded
]
);
However, the first encode command seems to be skipped over when the second encode command runs.
I think explicitly defining the filetype in the 'data-url' encode command function would be awesome - ie:
$img = Image::make('/images/test.jpg');
$jpg_to_png_data_url = $img->encode('data-url/png')->encoded;
When upgrading to 2.0.5 an error is thrown:
local.ERROR: exception 'ErrorException' with message 'is_file() expects parameter 1 to be a valid path, string given'...
which means that file/line:
intervention/imagecache/src/Intervention/Image/ImageCache.php(119)
php -v
PHP 5.6.2 (built: Nov 8 2014 20:00:07)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
Is it possible to invalidate the cache of an image without have to clear all cache files?
I I try to access an image with it's extension I get 'The requested resource /storage/uploads/original/4/female.png was not found on this server.'
When I remove the extension .png on the image and the route, the image can be loaded.
I believe there's a problem with the dot notation (dot extension). Should I change .htaccess or so to allow dots in my route?
This cache package seems very geared towards storing and returning the actual data stored for the image.
The problem is when it comes to filesystem, you really don't want to be reading image files into memory in PHP and then output the data somehow. Your whole framework shouldn't need to be booted up to serve an image. Ideally you want nginx to serve a cached version of the image stored on disk straight up.
With this package, it's extremely difficult. This might be related to issue #38.
It's good that it's general using the Flysystem, but I think there needs to be more consideration for those that don't want PHP to read the images but just have the cache return the stored image path.
Hello,
I try to cache for example from this url:
http://sm.mania-exchange.com/maps/screenshot/normal/6264
I tried to set this in $image->make() but it always returns the error:
"Unable to init from given binary data. "
Also tried with:
http://sm.mania-exchange.com/maps/screenshot/normal/6264?.jpg
It would be amazing if you can tell me an solution for this problem :).
I'm rather new to Laravel and php. so forgive me if this is a very stupid question but how can I use the image cache function to take all variables inside the callback?
I think I understand what this method does stated in your docs but
// pass calls to image cache
$img = Image::cache(function($image) {
return $image->make('public/foo.jpg')->resize(300, 200)->greyscale();
});
In my case everything is a variable: the file path & the sizes; how can I pass these along this function since this is another scope?
I thought this was a solution
$image = Image::make($path)->resize($width, $height,true,true);
$img = Image::cache(function($image) {
return $image;
});
but that does not work and even if it did, I'm pretty sure the image would have been made twice.
can you help me out how to do this? or point my nose in the right direction :)
if it cannot be done, that is ok to I will write my own caching system; just hate reinvent the wheel!
muchos gracias!
Veerle
Hello,
Just wondering if its possible to download an image (based on the original) when using image cache and url manipulation.
For example it would be accessed just like original keyword only with the keyword download instead. Sort of like: http://random.app/media/cache/download/myimage.jpg
Not sure if its the right way but I tried creating a filter but keep getting errors.
The error
ErrorException in ImageCache.php line 319:
Undefined property: Illuminate\Http\Response::$encoded
Part of the filter
public function applyFilter(Image $image)
{
$response = Response::make($image->encode());
$response->header('Content-Type', $image->mime);
return $response;
}
Thanks for your time
Hello, thank you for this great package is really awesome, but i cant get the url manipulation/imagecahing feature to work like expected. The page load time is really slow, as it seems the photos get cached every time a request is made. The resize and everything works great expect for the fact that it runs super slow because it doesn't fetch the already created cache but it recreates it. I can't seem to find anything that can help me because i don't know what the problem might be.
thank you for your time and effort.
Consider the following
return Image::cache(function($image) {
$image->make('test.jpg');
$image->encode('png');
return $image;
}, 43200, true)->response();
On the first run, Image::cache
will return a string, instead of an Image
object like it's supposed to.
After it's cached however, Image::cache
will return an Image
object like it's supposed to.
Hello, it seems that possible recent version number changes have caused a hiccup in installation through composer. Upon updating through composer I get the following.
intervention/imagecache dev-master requires intervention/image 2.* -> no matching package found.
Hello, if file name contains space, the URL based image manipulation will not work.
Eg: http://yourhost.com/{route-name}/{template-name}/foobar.jpg -> working
http://yourhost.com/{route-name}/{template-name}/foo%20bar.jpg -> Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
And in my case, it redirects to a customized 404 page.
<a class="cboxElement" href="{{ $movieDetails->url }}" title="{{$movieDetails->c00}}"><img class="thumbnail" src="{{ $data }}" alt="..."></a>
im trying to cache a image from a url them i want to display it inside that image tag im having trouble i get just raw data on the browser like this..
I found that ImageServiceProviderLaravel4 is not working properly, the commented out code shown below was preventing to get this functionality to work, I fixed it by using the same flow that the ImageCacheController is using:
/*
if (is_callable($callback)) {
// image manipulation based on callback
$content = $app['image']->cache(function ($image) use ($image_path, $callback) {
return $callback($image->make($image_path));
}, $config->get('imagecache::lifetime'));
} else {
// get original image file contents
$content = file_get_contents($image_path);
}
// define mime type
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
// return http response
return new IlluminateResponse($content, 200, array(
'Content-Type' => $mime,
'Cache-Control' => 'max-age='.($config->get('imagecache::lifetime')*60).', public',
'Etag' => md5($content)
));
*/
$manager = new ImageManager;
$content = $manager->cache(function ($image) use ($callback, $image_path) {
if ($callback instanceof Closure) {
// build from closure callback template
$callback($image->make($image_path));
} else {
// build from filter template
$filter = new $callback();
$image->make($image_path)->filter($filter);
}
}, $config->get('imagecache::lifetime'));
// define mime type
$mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
// return http response
return new IlluminateResponse($content, 200, array(
'Content-Type' => $mime,
'Cache-Control' => 'max-age='.($config->get('imagecache.lifetime')*60).', public',
'Etag' => md5($content)
));
Can't really see a way to pass the image $path to
return $image->make($path)->resize(600, 310)->greyscale();
from the example
Probably missing something obvious but would be nice to have it explained in the docs as I'm getting an undefined variable error every time.
Firstly, thanks for both this and the image library, saved me a whole bunch of time on a project I was working on.
The issue I faced, is the imagecache does not function optimally outside of a Laravel application (or an application that supports an app() function). For example, if I want to instantiate the imagecache object graph by hand and use it, subsequent calls to make() will return a new imagecache with a default configuration.
For example take a look at this snippet (gist here)
<?php
$filestore = new \Illuminate\Cache\FileStore(
new \Illuminate\Filesystem\Filesystem(),
'/some/path/to/somewhere/specific/intervention-cache');
$repository = new \Illuminate\Cache\Repository($filestore);
$cache = new \Intervention\Image\ImageCache($repository);
$watermark = $cache->make($watermarkFile)->get(0, true);
$watermarkedImage = $cache->make($srcFile)->insert($watermark, $xPos, $yPos, $anchor)->get(0, true);
return $watermarkedImage;
Now the problem here, is that the call to $cache->make sees a new ImageCache object instantiated inside of ImageCache::make() and it instantiates that object without any parameters, therefore the returned object does not have your carefully crafted cache configuration.
To me it seems like a simple solution would be to rather than instantiating a new ImageCache, clone(this) and call ImageCache::clearCalls() on the newly cloned image cache. That way your initial cache configuration is maintained.
Maybe there's a better way to achieve what I am trying to do, but curious for your feedback.
Anyway, thanks again for the package.
So sorry to feel the need to ask. I'm just struggling a bit with whether what I've done is a good way to implement image caching or not - and there doesn't seem to be a whole heap of examples around for how to utilise imagecache. Thank you for your excellent work BTW!
/*
|--------------------------------------------------------------------------
| Imagecache Routes
|--------------------------------------------------------------------------
*/
Route::any('imagecache/{style}/{filepath}', array('as' => 'imagecache', function($style, $filepath)
{
// Get the full path to the image (locked to the 'public/assets' folder).
$filepath = public_path() . "/assets/{$filepath}";
// 404 if the image doesn't exist.
if (!File::isFile($filepath))
{
App::abort(404, 'File not found');
}
// Produce the cached image.
$image = Image::cache(function($image) use ($style, $filepath)
{
// Switch on the passed image style preset.
switch ($style)
{
case 'square_thumbnail':
return $image->make($filepath)->resize(100, 100, false)->greyscale();
case 'thumbnail':
return $image->make($filepath)->resize(100, 100, true)->greyscale();
case 'medium':
return $image->make($filepath)->resize(220, 220, true);
case 'large':
return $image->make($filepath)->resize(480, 480, true);
case 'preview':
return $image->make($filepath)->resize(640, null, true);
// 404 if there's no such image style preset.
default:
App::abort(404, 'Image style not found');
}
}, 10, true);
// Return an image response.
return Response::make($image, 200, array('content-type' => 'image/jpg'));
}))->where('filepath', '.*');
My concerns are;
/../../
doesn't work - thank you Laravel) but maybe I'm overlooking something?Any advice or comments would be very much appreciated, thank you.
Do you have any plans to enable the use of other storage engines for the cache, perhaps utilising L5's new storage abstraction?
It'd be useful to be able to host cached images in S3 etc. At the moment, my load balanced application caches the image on each node which is obviously not ideal.
Hey,
Is there a way to do this:
$data = file_get_contents('http://cloudserver.com/image.jpg');
Image::cache(function($image) use ($data) {
return $image->make($data);
}, 10);
without having to save the image to the server first?
I get the error:
file_exists() expects parameter 1 to be a valid path, string given
as it's trying to find the file on the server.
make($data) do not support mixed type.
but 2.0.4 support.
when i added the two lines to composer.json
"intervention/image": "dev-master",
"intervention/imagecache": "dev-master"
i getting below error
ali@ali-UX31A:~/dev/l4$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for intervention/imagecache dev-master -> satisfiable by intervention/imagecache dev-master.
- Conclusion: remove intervention/image dev-master
- intervention/imagecache dev-master requires intervention/image 1.3.x -> satisfiable by intervention/image 1.3.0, intervention/image 1.3.1, intervention/image 1.3.10, intervention/image 1.3.11, intervention/image 1.3.2, intervention/image 1.3.3, intervention/image 1.3.4, intervention/image 1.3.5, intervention/image 1.3.6, intervention/image 1.3.7, intervention/image 1.3.8, intervention/image 1.3.9.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.0.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.1.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.10.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.11.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.2.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.3.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.4.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.5.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.6.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.7.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.8.
- Can only install one of: intervention/image dev-master, intervention/image 1.3.9.
- Installation request for intervention/image dev-master -> satisfiable by intervention/image dev-master.
we have must define version number such as 1.3.*
Hi All,
Yesterday I've updated to version 2.* and now I'm having a problem. The image is not created/showed properly (I just get a non image icon in the browser).
I'm just doing:
$img = Image::cache(function($image) {
return $image->make($GLOBALS['file'])->widen(250);
});
return Response::make($img, 200, array('Content-Type' => 'image/jpeg'));
It use to work in version 1.*
Cheers
It would be interesting to have a way to specify an alternate image (expecially when using Laravel routing) to serve if the intended image is not found. A classic example would be an user's avatar.
Hey I have a couple of questions which the documentation does not address.
--1. What is a good method of returning an image to the user?
I am currently using (in Laravel 4):
$image = Image::make('public/images/myphoto.jpg');
$image->resize(200, 200);
$image->save();
return Response::download( $image->dirname . '/' . $image->basename );
However this does not seem ideal. There is no full image path property? I can't print $image
?
What are other methods of returning the file to the browser?
--2. How do I return an image from the cache package?
Using the above code wrapped in cache:: closures does not work. Where is the cache stored? How do I return a cached file to the browser?
Some examples in the documentation would be great.
Thank you this is an awesome package :)
What am I doing wrong? If I save image without extension in storage path and call it like localhost/images/small/someimage it is working, return image in small size.
If I save it as someimage.jpg and try to call localhost/images/small/someimage.jpg it returns requested resource not found on this server
Is there any way it can integrate it with Amazon AWS? Laravel 5 has some cool updates to the filesystem (https://laracasts.com/series/whats-new-in-laravel-5/episodes/6) which makes it redicuisly easy to use a cloud.
Really curios about this.
Contents of "app/config/packages/intervention/imagecache/config.php":
'templates' => array(
'small' => 'Intervention\Image\Templates\Small',
'medium' => 'Intervention\Image\Templates\Medium',
'large' => 'Intervention\Image\Templates\Large',
'test' => 'App\Filters\Test'
),
Placed file with my custom preset under "app/filters/filters.php":
namespace App\Filters;
use Intervention\Image\Image;
use Intervention\Image\Filters\FilterInterface;
class Test implements FilterInterface
{
public function applyFilter(Image $image)
{
return $image->fit(30, 30);
}
}
But it doesn't work. If i use in my url "imagecache/test/image.jpg" it displays in original size, seems like laravel not seeing this namespace.
Hello, it seems that URL manipulated images don't get updated when we make changes to the original file. Eg:
I call imagecache/135x135/images/test.jpg and I get a nice 135x135 thumbnail from my custom template.
But if I modify the original image...
Image::make('images/test.jpg')->greyscale()->save();
... and the hit again imagecache/135x135/images/test.jpg I'm going to get the exact same thumbnail as the beginning since it's going to load the cached version of the thumbnail, ignoring the changes on the original image.
Maybe it shouldn't load the cached version when its modification time is older than the original's file one.
Thanks for your time and effort with this package!
Hey there,
I was able to install intervention/image 2.0.17 but trying to install intervention/imagecache 2.0.6 gives the following error:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for intervention/imagecache 2.0.6 -> satisfiable by intervention/imagecache[2.0.6].
- Conclusion: remove jeremeamia/SuperClosure 2.0.0
- laravel/framework 5.0.x-dev requires jeremeamia/superclosure ~2.0 -> satisfiable by jeremeamia/SuperClosure[2.0.0].
- laravel/framework 5.0.x-dev requires jeremeamia/superclosure ~2.0 -> satisfiable by jeremeamia/SuperClosure[2.0.0].
- Conclusion: don't install jeremeamia/SuperClosure 2.0.0
- Installation request for laravel/framework 5.0.*@dev -> satisfiable by laravel/framework[5.0.x-dev].
Is there any current workaround or best to wait until L5 is fully released?
i think with this feature, user can manage their cache. And where User's want to update the content of the cache, the can manage like a charm
I tried to create response with cached image and also resize with closure (constrain aspect ration)
1. Symfony\Component\Debug\Exception\FatalErrorException
…/vendor/intervention/imagecache/src/Intervention/Image/ImageCache.php152
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'Jeremeamia\SuperClosure\SerializableClosure' not found
my code in laravel is
$url = 'https://s3-ap-northeast-1.amazonaws.com/tmp-photos/12c9f90f559f6ef65f3cfe5854c3574b/1404207756862.jpg';
$cache_image = Image::cache(function($img) use ($url) {
return Image::make($url)->resize(200,200)->greyscale();
},10,true);
return Response::make($cache_image,200, ['Content-Type' => 'image/jpeg']);
but it alway show me
Call to undefined method Intervention\Image\ImageManager::encode()
could somebody help me??
When trying to use the artisan command php artisan route:cache
, I get this error:
The ImageCache route from the config/imagecache.php also throws an error when I run the artisan command php artisan config:cache
and then try to access a page of my app:
Here is the line 268:
The content of the array isn't cached properly. (Otherwise, my imagecache installation works fine when I don't use the cached config file).
Last but not least, and I'm not sure it's related or not, everytime I access a cached image, I get the Undefined index: _sf2_meta
:
Here is the compiled.php on line 10981:
It seems to be a recurring problem on laravel 5 due to the fact that you can't change session drivers, but I have no clue how to fix this.
When using image cache you cant get the properties of an image, such as width or height.
For example, this works:
$img = Image::make($file)->resize(850, null, true);
header('Content-Type: text/json');
print json_encode([
$img->width,
$img->height,
]);
exit;
But if you try that within an image cache call:
// lets manipulate the image and cache it for 10 minutes
$img = Image::cache(function($image) use ($file) {
$img = $image->make($file)->resize(850, null, true);
// header('Content-Type: text/json');
print json_encode([
$img->width,
$img->height,
]);
exit;
return $img;
}, 10, true);
You get the following error: Undefined property: Intervention\Image\ImageCache::$width
Hi,
I updated (composer update) to intervention/image 2.1.4 then my url based image manipulation was broken, with message "unable to load Intervention/Image/ImageCacheRouteController"
After searching for file permission and so on, i give up and finally turn back to intervention image 2.1.3. Is anyone know what happened in intervention image 2.1.4 ?
This is my imagecache config.php
localhost routes:
live site routes:
Note: GD library is installed on live site, and is supported.
On my localhost, the url routing works great, with no problem at all. However, when I try to run on live site, the url routing doesn't work at all. Why? can anyone tell me, what I am missing?
Imagecache::checksum()
can't use serialize
to map image state, if Closures where passed.
File names containing % or ( ) are not being processed correctly when using the URL manipulation method.
Is there anyway I can choose Laravel Cache Store for imagecache? Because I'm using Redis as default cache store and it's a big resources wasting if use Redis as Image Cache.
I am upgrading my system with a fresh start in Laravel 5. Imagecache for L4 came with a config file, so I am assuming L5 version has one as well. When I do a "php artisan vendor:publish" I am receiving a "Noting to publish" error. Am I missing something in the setup?
When attempting to set this up on the latest version of Laravel 4, the following error gets thrown...
Call to undefined method Intervention\Image\Facades\Image::getTable()
Any thoughts?
I've some problem with imagecache using transparent images (in my case a PNG) ... I give you some example:
Without cache:
$image = Image::open($path);
return Response::make($image, 200, ['Content-Type' => $image->mime]);
With cache:
$cached_img = Image::cache(function($image) use ($path)
{
$image->open($path);
return $image;
}, 10, true);
return Response::make($cached_img, 200, ['Content-Type' => $cached_img->mime]);
Without cache:
$image = Image::open($path);
$image->resize(null, 70);
return Response::make($image, 200, ['Content-Type' => $image->mime]);
With cache:
$cached_img = Image::cache(function($image) use ($path)
{
$image->open($path);
$image->resize(null, 70);
return $image;
}, 10, true);
return Response::make($cached_img, 200, ['Content-Type' => $cached_img->mime]);
In my examples, the property $cached_img->mime returns NULL and not the mimetype
I just installed the library. Hitting the route:
{route}/{template}/{filename}
Is extremely slow. It takes 500ms or so, compared to direct image source which is 20ms or so.
I wonder if the image is even cached. I suspect not. Using memcached as driver on a Laravel 5 installation.. How to debug further?
Is it possible to do recursive searches on the given storage paths?
would be cool if we could inject the L4 cache instance to use this outside L4
Laravel 4.3 has the awesome new filesystem driver config! Can can we do to leverage s3 as our filesystem / cache?!
If I run:
php artisan cache:clear
url based image manipulation is broken.
Is there a safe way to reset the cache?
Closures in imagecache config breaks laravel 5's config cache (and it would break zf2's config cache, too).
I figured it's serialisation issue. Is it really necessary to setup resize logic in config file?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.