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
Related
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'])
I'm writing an app that lets the user upload an Excel file. It checks the file for errors, then, if no errors are found, it uploads the contents to a database. If it does find errors, the cells containing errors are colored red, then the file is saved. I then want to create a download link to this file so the user can check where they made mistakes.
The problem is that I am not sure how to create this link and where to store the file. I modify the file like this:
foreach ($badCells as $bcell) {
$sheet->getStyle($bcell)->applyFromArray(array(
'fill' => array(
'type' => \PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'FF4444')
)
));
}
And then save it with
$objWriter->save($dldir . $formData['upload']['name']);
$dldir is created with
$dldir = "/download/";
if (file_exists($dldir)) {
if (!is_dir($dldir)) {
unlink($dldir);
mkdir($dldir);
}
} else {
if(!is_dir($dldir)) {
mkdir($dldir);
}
}
Is this even the right way to do this? Can I store the files in any old folder or do they have go somewhere specific? How do I create the link to the specific file in the view for the user and make it accessible so they can download it?
I may be wrong .
For file upload i use this library Gargron/fileupload
The function bellow help to upload file to a specific folder and return full link of the file. You can save the link in DB
function uploadFile ($file){
$validator = new \FileUpload\Validator\Simple('5M');
$pathresolver = new \FileUpload\PathResolver\Simple($_SERVER['DOCUMENT_ROOT'].'/upload_folder');
$filesystem = new \FileUpload\FileSystem\Simple();
$fileupload = new \FileUpload\FileUpload(file, $_SERVER);
$fileupload->setPathResolver($pathresolver);
$fileupload->setFileSystem($filesystem);
$fileupload->addValidator($validator);
$md5Generator = new \FileUpload\FileNameGenerator\MD5 (true);
$fileupload->setFileNameGenerator($md5Generator);
list($files) = $fileupload->processAll();
foreach($files as $file){
if ($file->completed) {
return $_SERVER['DOCUMENT_ROOT'].'/upload_folder/'.$file->getFileName()) ;
}
}
}
Thanks for the help, I managed to figure it out over the weekend. Here's how I did
it:
$filename = join(DIRECTORY_SEPARATOR, array($dldir, $formData['upload']['name']));
$objWriter->save(str_replace(__FILE__,$filename,__FILE__));
This is how I save the file, using DIRECTORY_SEPARATOR so it will work correctly on both Windows and Linux.
return $this->redirect()->toRoute('import', ['action' => 'reject'],['query' => ['q' => 'file', 'name' => $filename]]);
In the controller, I redirect the route to the correct action and pass the file name on to it via a query URL.
public function rejectAction()
{
$filename = $this->getRequest()->getQuery('name', null);
return new ViewModel(array('filename' => $filename));
}
There, I obtain said file name through getRequest()->getQuery() and pass it on to the viewmodel.
Right-click here and choose 'Save As' to download your file.
And finally, this is how it shows the link in reject.phtml.
Downloading only works with right click and save as, I suspect I will have to write some sort of file handler to make ZF produce the correct headers for a normal left click download.
I am using laravel Filesystem to store images, the image will be uploaded inside the default storage, example:
/home/user/Projects/mywebapp/storage/app/images/posts/1/e392e17926559e7cc4e7934f.png
I didnt change the config/filesystem.php and here is my controller method to upload an image,
public function storeImage( Request $request ) {
$image = $request->file( 'image' )->store( 'images/posts/'.Auth::user()->id );
echo Storage::url( $image ); // <- print the url of the uploaded image
}
This would print the image url like this,
http://localhost:8000/storage/images/posts/1/e392e17c6a8d7926559e8e7cc4e7934f.png
But the image file is not accessable from the browser!
I dont want to use php to dynamically fetch the images, i just wanted to make them publicly accessable over http. How to display the image?
you just have to make a change in config\filesystems.php form
'default' => local,
to 'default' => public,
then in console run php artisan storage:link
then try the above code and it will work
For me it worked when I added "../" to the url of the file:
#foreach($arquivos as $i)
<li class="list-group-item"> <img src="{{ asset('img/pdf.png') }}" /> {{ $i -> nome }} </li>
#endforeach
And I am saving the file like this:
$ext = $request->arquivo->extension();
$path = $request->arquivo->storeAs('/relatorios', $nameoffile. "." . $ext);
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
I have a Laravel 4 application that displays images from another PHP application that is located on a remote server.
I would like to know what is the best solution to cache the images from the remote server on my local Laravel App.
Please note that it's almost impossible to copy/past the entire images directory from the remote server, since there are more than 150k images that are regurlary updated (every minutes) and there is a database correlation too (filename with column value in database).
I was about to use Rsync to sync the two directories (distant with local), but I also need to resize images before displaying them and organize the images subdirectories in a different way than it is on the remote server.
First I have installed the Intervention Image Class package on Laravel and I have created a Route:
Route::get('photos/item/{size}/{reference}/{photo}', 'ImgController#showImg');
And in the ImgController:
public function showImg( $size, $reference, $photo )
{
switch ( $size ) {
case '300x225':
$jpg = Image::make( 'http://myremoteapp.com/uploads/' . $reference . '/' . $photo )->resize( 300, 225 )->response( 'jpg' );
break;
}
return $jpg;
}
This works but it doesn't keep the images in the browser's cache and it also creates a performance issue, since the images have to be downloaded and resized every time the page is opened.
I have heard about the Intervention Image Cache but I am not sure if it works with images taken from URLs.
Any advice and suggestions on how to resolve this in a proper way would be greatly appreciated.
You can use cache route as:
Route::filter('cache', function($route, $request, $response = null) {
$key = 'route-' . safe_title(URL::full());
if( is_null($response) && Cache::has($key) ) {
return Cache::get($key);
} elseif( !is_null($response) && !Cache::has($key) ) {
// Code this here
Cache::put($key, $response->getContent(), 30);
}
});
Route::group(array( 'before' => 'cache', 'after' => 'cache' ), function() {
Route::get('photos/item/{size}/{reference}/{photo}', 'ImgController#showImg');
});
attention :
Route::filter() was wiped away from Laravel since > 5.0