I want to have a Route that takes image Dimension and source and return Cropped and resized image via timthumb php script.
I put timthumb.php file in a folder in public directory and I wrote this Route:
Route::get('/showImage/{w}/{h}/{src}', function ($w , $h , $src) {
$img = 'public/plugins/timthumb/timthumb.php?src='.$src.'&w='.$w.'&h='.$h;
return Response::make($img, 200, array('Content-Type' => 'image/jpeg'));
})->where('src', '[A-Za-z0-9\/\.\-\_]+');
But nothing happen.
How can I access to timthumb.php file and send it required parameters and get result image?
Update:
this is structure of Public directory and placement of timthumb and images folder :
According to , I try :
$img = 'public/plugins/timthumb/timthumb.php?src=../../'.$src.'&w='.$w.'&h='.$h;
And :
$img = 'public/plugins/timthumb/timthumb.php?src=public/'.$src.'&w='.$w.'&h='.$h;
But none of them does not work for bellow URL:
http://localhost:8000/showImage/100/200/upload/slideshow/slide2.jpg
I suggest using league/glide. Watch Using Glide in Laravel to get started.
The better way to use timthumb image in Laravel using intervention/image
Install this package.
Update your Route:
Route::get('showImage/{w}/{h}/{src}', function ($w , $h , $src)
{
$img_path = public_path().'/'.$src;
$img = Image::make($img_path)->resize($w, $h);
return $img->response('jpg');
})->where('src', '[A-Za-z0-9\/\.\-\_]+');
OR Image Cache
Install intervention/imagecache package
Route::get('showImage/{w}/{h}/{src}', function ($w , $h , $src)
{
$img_path = public_path().'/'.$src;
$img = Image::cache(function($image)use($w,$h,$img_path) {
return $image->make($img_path)->resize($w, $h);
});
return Response::make($img, 200, ['Content-Type' => 'image/jpeg']);
})->where('src', '[A-Za-z0-9\/\.\-\_]+');
Image URL would be as below:
http://<< Domain Name >>/showImage/800/400/Desert.jpg
Related
I'm trying to return query data and storage Images from laravel controller. My code looks like following :
class ClaimController extends Controller{
.....
public function show(Claim $claim)
{
$front_image = Storage::disk('do_spaces')->get($claim->images->front_image); //image file
$back_image = Storage::disk('do_spaces')->get($claim->images->back_image); //image
// return $front_image [works]
$claim = Claim::all();
//this throws error
return response()->json([
'claim' => $claim,
'images' => [$front_image, $back_image]
]);
}
}
Now I far I understand return->response()->json([]) doesn't send image file. How I can return all data together to frontend app ?
you have the option to return the images from original server which they are stored in or you can return an encoded version of the image as string and in front end reconstruct it.
$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
And then use it in front like this :
var image = new Image();
image.src = 'data:image/png;base64,iVBORw0K...';
document.body.appendChild(image);
This is the link for second part.
You can use Storage::url()
$st=\Illuminate\Support\Facades\Storage::disk('do_spaces')->url($claim->images->front_image);
dd(url($st));
Storage::url() return image path and then url() will return full url path
"http://127.0.0.1:8000/storage/imagefolder/filename"
Make sure to symbolic link to storage folder by running following command
php artisan storage:link
Also you can do the following without storage url
asset('storage/'.$claim->images->front_image)
this is my scenario:
I have a Laravel 6.x application and I'm using the FlySystemAzureBlobStorage package to store images on Azure Blob Storage.
Then I want to use the InterventionImageCache package to get and cache the images for faster client's downloads in different sizes.
I've already done it in this way:
public static function getImageResponseForApi($storageDiskName, $imagePath, $width = null, $height = null)
{
//check if the image exists on disk
$exists = empty($storageDiskName) ?
Storage::exists($imagePath) :
Storage::disk($storageDiskName)->exists($imagePath);
if ($exists) {
$image = empty($storageDiskName) ?
Storage::get($imagePath) :
Storage::disk($storageDiskName)->get($imagePath);
if (!empty($width) || !empty($height)) {
//use the image cache function to get the cached image if exists
$image = \Image::cache(function ($ima) use ($image, $width, $height) {
//check if height and with are higher than original or not
$ima->make($image)->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
});
}
$sizes = getimagesizefromstring($image);
$headers = [
'Content-Length' => strlen($image),
'Content-Type' => $sizes["mime"]
];
return \Response::make($image, 200, $headers);
} else {
return null;
}
}
There is a problem working in this way: I must download the image from azure blob storage before the system can check if a resized cached version of it exists.
The Azure package i'm using doesn't provide the possibility to get the image paths, so I can't find another solution to my problem.
So, is there a way to achieve the image caching without I have to download the file every time?
I use a separate route for showing/caching the image in my blade.
I store the image as BLOB in MYSQL PRODUCT table identified by product_id
BLADE:
<img src="/dbimage/{{$product->id}}.png" >
ROUTE:
Route::get('/dbimage/{id}',[ProductImageController::class, 'getImage']);
CONTROLLER:
class ProductImageController extends Controller
{
public function getImage($prodcutImageID){
$productID=explode(".",$prodcutImageID);
$rendered_buffer= Product::all()->find($productID[0])->image;
$response = Response::make($rendered_buffer);
$response->header('Content-Type', 'image/png');
$response->header('Cache-Control','max-age=2592000');
return $response;
}
}
This makes the browsers cache the images
Im using laravel-tinymce-simple-imageupload so that is possible to have the tinymce plugin with image upload.
But Im not understanding where to change the url path of the uploaded images so that is used an absolute path url?
As you can see, the plugin use a controller :
https://github.com/petehouston/laravel-tinymce-simple-imageupload/blob/master/src/TinymceController.php
public function uploadImage(Request $request)
{
$image = $request->file('image');
$filename = 'image_'.time().'_'.$image->hashName();
$image = $image->move(public_path('img'), $filename);
return mce_back($filename);
}
So as explained here : https://github.com/petehouston/laravel-tinymce-simple-imageupload#some-notes
You just have to create a new Controller Action in your application, route it and call it as explained :
#include('mceImageUpload::upload_form', ['upload_url' =>'YOUR_URL_FOR_HANDLING_IMAGE_UPLOAD'])
Hi I'm trying to get thumbnails with the same TimThumbs, I'm using Intervention Image and Intervention Cache.
My thumbnails now have the url structure:
thumbs/122x88?src=upload/news/news_40942c468090384b8135a6bbb79f631d.png
How do I get an address like:
/thumbs/upload/news/122x88-news_40942c468090384b8135a6bbb79f631d.png
I would like to remove this "?src="?
My route code is as follows:
Route::get('thumbs/{imgw}x{imgh}/', function ($imgw,$imgh)
{
$src = \Input::get('src', 1);
$width = $imgw;
$height = $imgh;
$cacheimage = \Image::cache(function($image) use ($src,$width,$height) {
return $image->make($src)->resize($width,$height);
}, 1, false);
return Response::make($cacheimage, 200, array('Content-Type' => 'image/jpeg'));
});
I tried to remove "\Input::get", but It's not working.
Check it out how I get the pictures on my website:
https://www.verhuisdieren.nl/
So on imagecache.php ( config file ), i just setup the route for all the images ( lets say 'route' => 'images' ), and when I upload images, I upload them on the storage folder and rename them before I save them. From Laravel 5.3 you can save it with the FileUploader class like this:
$request->get('image')->save('new-name');
and this then loads it without any prefix.
So recap: don't make a route for it, you already have that on your imagecache.php file. You can then get the route something like this:
url(config('imagecache.route'), ['YOUR-SIZE', 'FILENAME']);
EDITED:
config file example ( the config file imagecache.php ):
https://pastebin.com/sf5CusYJ
I'm creating an API in which I have a model called OnlineAds(), it has these columns title, full_discription, short_discription and image, I also created an input form to fill in these fields and upload an image, in the image field I add the image's name and store the image in this path: storage/uploads. I was able to retrieve the image and display it, but I still didn't figure out how to resize it before displaying (lets say I want it to be 300 x 300), can anybody show me how ?
Note: I tried to use Stapler and Imagine but they seemed to give multiple bugs and failed!
This is my controller:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$filetype = File::type($path);
$response = Response::make(File::get($path), 200);
$response->header('Content-Type', $filetype);
return $response;
}
}
You could look into using Intervention to take care of the image processing as well as handling the response. It can be as easy as this:
public function show()
{
$img = OnlineAds::find(50)->image;
$path = storage_path('uploads') . '/' . $img;
if (File::exists($path)) {
$img = Image::make($path);
$img->resize(300, 300);
return $img->response();
}
}
There are detailed instructions on how to install the Intervention library and integrate it with Laravel, and it shouldn't take more than a few minutes to get it up and running.