How to create thumbnail image in laravel using Intervention in live server? - laravel

My primary domain is xyz.com. I have created a folder named "test" outside public_html folder and uploaded the whole laravel set up there except the public folder which I have moved to public_html folder.
My folder structure is like this.
test -> contains the laravel project without public folder.
public_html -> contains the public folder.
However I cannot upload images. Error is showing like "Image source not readable". Please help.
if ($request->hasFile('frontimage')) {
$file_frontimage = $request->file('frontimage');
$actual_filename_frontimage = $file_frontimage->getClientOriginalName();
$filename_frontimage = time() . '_' . $actual_filename_frontimage;
$file_frontimage->storeAs('images', $filename_frontimage, 'public');
}

Related

How Change laravel public_path

I changed my laravel installation folder structure by moving all files and folders from the root directory to a new folder 'core'.
The goal is, to have only the folders in the root directory of the project
core
public
I have updated the index.php in public folder but I have the following problem:
when I die dump the paths, all are correct except the public path
dd(app_path()); // somepath\project\core\app
dd(storage_path()); // somepath\project\core\storage
dd(base_path()); // somepath\project\core
dd(public_path()); // somepath\project\core\public
I want to retain all other paths and change only the public path to somepath\project\public instead of somepath\project\core\public , how can I do this, online docs I've reference focus on changing public_html to public but I don't want to change the public_html, just to tell laravel that the public path is somepath\project\public
This particularly needful for me as I use the laravel storage for handling uploads.
Below is my folder structure
project
|-core
|- every other folders and files
|-public
|-every other asset files
You need to rewrite the base path in the app/Providers/AppServiceProvider.php like so:
public function boot()
{
$this->app->bind('path.public', function () {
return dirname(getcwd(), 1) . '/public';
});
}

Using public and resources folder as alias in laravel

I split a laravel project into 2 parts, backend and frontend, I created 2 repos on GitHub.
In the frontend repo, there are only 2 folders, public and resources folder.
So, when I pull the changes on frontend, I would like all files to replace in backend folder too.
------ backend folder
---- public folder -> I want to it be an alias
---- resources folder -> I want to it be an alias
----- frontend folder
---- public folder
---- resources folder
When I create an alias of the public and resources folder in frontend, laravel won't work.
If I create a symlink of those frontend folders, laravel works but index.php file doesn't work as it doesn't follow symlinked path
How can I get it working?
To change public folder directory go to root index.php
Under the $app = require_once __DIR__.'/../bootstrap/app.php'; add below line:
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
To change the resource of view folder directory go to config/view.php and change your desire path.
'paths' => [
realpath(base_path('resources/views'))
],

Laravel symlink and cPanel

On my Laravel website I'm using symlink to store and show the images from storage.
With
php artisan storage:link
I had created the symlink and everytime when I upload a new news article, the image is uploaded in the main Storage and with symlink it's setup to the public folder and I'm displaying the image properly.
So far so good, but when I've created a copy of the website, a problem appears...
When I've created a copy of the website with cPanels File Manager, and move to a new location, the storage symlink in the public directory has become a folder, not a symlink.
After that when I try to upload a new news article, I can see it's uploaded in the main Storage folder, but not in the public/storage, so as a result the image is not displaying. That's because it's not a symlink anymore, but now it's a folder.
I've deleted the storage folder from the public directory, with SSH I've used the command again
php artisan storage:link
and I've created a new news article and the image is displaying properly, but now all other images are gone.
Is there any command that will regenerate the paths, so all other images will be display again?
I'm using Laravel 5.5
You can solve it in another way to create a symlinkexample.php file
into your public folder and run the file path into browser.
Then Storage folder will be created into public folder. Folder path
will be public/storage with linked to your public folder.
Code below for symlinkexample.php :
<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';
?>
Try this:
In route/web.php add the following code:
Route::get('/storage', function(){
\Artisan::call('storage:link');
return "Se han vinculado las imágenes";
});
If for some reason you are using different file structure and for some reason don't have terminal access, \Artisan::call('storage:link') will fail since it won't find public path.
Route::get('cmd', function(){
$process = new Process(['ln', '[symlink-here]','[target-folder]' ]);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
});
read about Process class here

Laravel: How to save images to public folder?

I am having trouble uploading a user image to my public folder. The file name generates correctly, and saves the name to my database, except the iamge itself refuses to get saved into my public folder. What am I doing wrong?
public function update_avatar(Request $request) {
if($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
$filename = time() . "." . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save(public_path('/uploads/'.$filename)); ==> This is causing me errors
user = Auth::user();
$user->avatar = $filename;
$user->save();
}
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments.
To create the symbolic link, you may use the storage:link Artisan command:
php artisan storage:link
https://laravel.com/docs/5.5/filesystem
I think you should try this:
$destinationPath = public_path('uploads');
Image::make($avatar)->resize(300,300)->save($destinationPath.'/'.$filename);
First you must move your image to destination directory and then resize it.
$avatar->move(public_path('/uploads/'.$filename));
Image::make(public_path('/uploads/'.$filename))->resize(300,300)->save(public_path('/uploads/'.$filename));
Note
Check the directory that you moving your file there is already exist.

Uploading Laravel Project onto Web Server

I am trying to upload my Laravel project onto my web server, but my past two attempts have been unsuccessful. I believe I am not uploading the files to the right location.
This is my web server's structure ->
Am I correct to say that I need to upload ALL of my laravel files into public_html?
This is my Laravel project's directory :
EDIT : I have now added all the files onto the root folder, and public into public_html, however none of my routes seem to work. (They work perfectly on localhost). Everything throws a 404
No, but you have a couple of options:
The easiest is to upload all the files you have into that directory you're in (i.e. the cPanel user home directory), and put the contents of public into public_html. That way your directory structure will be something like this (slightly messy but it works):
/
.composer/
.cpanel/
...
app/ <- your laravel app directory
etc/
bootstrap/ <- your laravel bootstrap directory
mail/
public_html/ <- your laravel public directory
vendor/
artisan <- your project's root files
You may also need to edit bootstrap/paths.php to point at the correct public directory.
The other solution, if you don't like having all these files in that 'root' directory would be to put them in their own directory (maybe 'laravel') that's still in the root directory and then edit the paths to work correctly. You'll still need to put the contents of public in public_html, though, and this time edit your public_html/index.php to correctly bootstrap the application. Your folder structure will be a lot tidier this way (though there could be some headaches with paths due to messing with the framework's designed structure more):
/
.composer/
.cpanel/
...
etc/
laravel/ <- a directory containing all your project files except public
app/
bootstrap/
vendor/
artisan
mail/
public_html/ <- your laravel public directory
I believe - your Laravel files/folders should not be placed in root directory.
e.g. If your domain is pointed to public_html directory then all content should placed in that directory. How ? let me tell you
Copy all files and folders ( including public folder ) in public_html
Copy all content of public folder and paste it in document root ( i.e. public_html )
Remove the public folder
Open your bootstrap/paths.php and then changed 'public' => __DIR__.'/../public', into 'public' => __DIR__.'/..',
and finally in index.php,
Change
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
into
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/start.php';
Your Laravel application should work now.
If you are trying to host your Laravel app on a shared hosting, this may help you.
Hosting Laravel on shared hosting #1
Hosting Laravel on shared hosting #2
If you want PHP 5.4 add this line to your .htaccess file or call your hosting provider.
AddType application/x-httpd-php54 .php
In Laravel 5.x there is no paths.php
so you should edit public/index.php and change this lines in order to to pint to your bootstrap directory:
require __DIR__.’/../bootstrap/autoload.php’;
$app = require_once __DIR__.’/../bootstrap/app.php’;
for more information you can read this article.
Had this problem too and found out that the easiest way is to point your domain to the public folder and leave everything else the way they are.
PLEASE ENSURE TO USE THE RIGHT VERSION OF PHP. Save yourself some stress :)
All of your Laravel files should be in one location.
Laravel is exposing its public folder to server. That folder represents some kind of front-controller to whole application. Depending on you server configuration, you have to point your server path to that folder. As I can see there is www site on your picture. www is default root directory on Unix/Linux machines.
It is best to take a look inside you server configuration and search for root directory location. As you can see, Laravel has already file called .htaccess, with some ready Apache configuration.

Resources