I have project in Laravel 5.8.
I have this code:
<a href="{{ route('user', ['url_address' => $user->url_address, 'id' => $user->id]) }}"><img
src="{{ $user->images->first()->path ?? $placeholder }}"
class="img-responsive center" alt="{{ $user->name }}"></a>
This return me:
<img src="upload/images/UserImage/c561b1742783ed038521c488d1cd59a1.jpg" class="img-responsive center" alt="angel">
I need replace this:
upload/images/UserImage/c561b1742783ed038521c488d1cd59a1.jpg
to:
upload/images/UserImage/thumbs3/c561b1742783ed038521c488d1cd59a1.jpg
How can I make it?
You have a few options here. You could update your DB data, add a new column to store the thumb path, or modify the existing path in place.
Here's one way to do it using https://laravel.com/docs/5.8/helpers#method-str-replace-first
<?php
use Illuminate\Support\Str;
...
$search = 'upload/images/UserImage/';
$replace = 'upload/images/UserImage/thumbs3/';
$original = $user->images->first()->path;
$replaced = Str::replaceFirst($search, $replace, $original );
// Now, use `$replaced` as your image src.
<a href="{{ route('user', ['url_address' => $user->url_address, 'id' => $user->id]) }}">
<img src="{{ $replaced ?? $placeholder }}"
class="img-responsive center" alt="{{ $user->name }}">
</a>
I should also mention, that in my experience it is easier to maintain a system where all you store in the DB is the file name of the uploaded document. In this case, c561b1742783ed038521c488d1cd59a1.jpg.
Your views then control which directory to look in: ...public_path('upload/images/thumbs/' . $user->profile_image)
Related
In my Laravel (7.x) application. I am trying to create a component for links:
Component:
<a class="links" href="{{ $route }}" title="{{ $title ?? null }}">
#isset($icon)
<i class="{{ $icon }}"></i>
#endisset
#isset($caption)
<span>{{ $caption }}</span>
#endisset
</a>
<x-link icon="{{ $icon }}" route="{{ route("admin.{$route}.create") }}" />
OR
<x-link icon="{{ $icon }}" route="{{ route("admin." . $route. ".create") }}" />
OR
<x-link icon="{{ $icon }}" route="{!! route("admin.{$route}.create") !!}" />
OR
<x-link icon="{{ $icon }}" route="{!! route("admin." . $route. ".create") !!}" />
And getting this issue:
syntax error, unexpected token "endif", expecting end of file (View: \path)
However, if I do this, then it works:
$url = route("admin.{$route}.create");
...
<x-link icon="{{ $icon }}" route="{{ $url }}"></x-link>
I am not a fan of declaring the variables just for single use, I like to pass the value directly. Therefore, I prefer the first method.
Why is this simple code not working..?
I found that the parameters work better, if the values are bind with them.
<x-link icon="{{ $icon }}" :route="route('admin.' . $route . '.create')" />
Additionally... In case, if the multiple values are required:
<x-link icon="{{ $icon }}" :route="route('admin.' . $route . '.create')" :params="[
'key1' => 'value1',
'key2' => [
...
],
]" />
I tried to display the image in the blade. but it cant load the image, it displays the image of else statement
<?php $user_image_path = 'images/user_images/' . $userDetails['image']; ?>
#if(!empty($userDetails['image']) && file_exists($user_image_path))
<img src=" {{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
#else
<img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">
#endif
I also debug the code above the if statement it fetches the image
<?php $product_image_path = 'images/user_images/' . $userDetails['image']; ?>
<?php
echo "<pre>";
print_r($user_image_path);
die;
?>
From above comments on your question, seems like you are loading the image with no extension. add the image extension
<img src="{{ asset('images/user_images/'.$userDetails['image'].".png") }}" alt="">
or for .jpg
<img src="{{ asset('images/user_images/'.$userDetails['image']).".jpg" }}" alt="">
Try:
<img src="{{ !empty($userDetails['image']) ? asset('images/user_images/'.$userDetails['image']) : asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">
It is the second condition of your if condition that doesn't match. The function of file_exists can't locate the the image file. You either have to provide the full path for the file_exists , or remove it:
#if(!empty($userDetails['image']))
<img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
#else
<img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre">
#endif
Use this code:
<img src=" {{ !empty(asset('images/user_images/'.$userDetails['image'])) ? <img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre"> : <img src="{{ asset('images/product_images/small/no_image.png') }}" alt="Cinque Terre"> }}" alt="Cinque Terre">
#if(!empty($userDetails['image']))
<img src="{{ asset('images/user_images/'.$userDetails['image'])}}" alt="Cinque Terre">
View the src of image inside inspector. Most of the times it gives us the wrong path instead of public directory.
Secondly, there is space in src. Try to remove and run that.
If asset function doesn't give the public directory path, add ASSETS_URL In .env to /public
I'm learning Laravel 5.4 and for now I'm trying to upload image and show uploaded image in html.
It seems files are uploaded successfully but when I want to show the image in Html, there's 404 error for images and nothing will display.
This is my settings and code:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'desc' => 'required|string',
'width' => 'required|numeric',
'height'=> 'required|numeric',
'artist'=> 'required|exists:artists,id',
'photo' => 'required|file'
]);
$artistInfo = Artist::find($request->artist);
$fileExtension = $request->file('photo')->extension();
$originalFileName = $request->file('photo')->getClientOriginalName();
$filePath = Storage::putFile('public', $request->file('photo'), 'public');
Photo::create([
'name' => $request->name,
'origName' => $originalFileName,
'url' => $filePath,
'artist_id' => $request->artist,
'desc' => $request->desc,
'width' => $request->width,
'height' => $request->height
]);
return $filePath;//this for test
}
And my Blade:
#forelse($photos as $photo)
<tr>
<td>{{ $photo->name }}</td>
<td>
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
<td>{{ $photo->artist->name . ' ' . $photo->artist->family }}</td>
<td>{{ $photo->desc }}</td>
<td>{{ $photo->width }}</td>
<td>{{ $photo->height }}</td>
<td class="text-center">
<a href="{{ route('artists.edit', ['id' => $photo->id]) }}">
<i class="material-icons">edit</i>
</a>
</td>
<td class="text-center">
<a href="{{ route('artists.edit', ['id' => $photo->id]) }}">
<i class="material-icons">assignment_turned_in</i>
</a>
</td>
</tr>
#empty
<tr>
<td>There's no photo Yet</td>
<td>—</td>
<td>—</td>
<td>—</td>
<td>—</td>
</tr>
#endforelse
NOTE :
I made a symbol link to my public folder using php artisan storage:link and symlink works fine...
I changed this line in blade:
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
and tried these manners:
<img class="img" src="{{ asset(storage_path() . ($photo->url)) }}" alt="{{ $photo->name }}" /></td>
<img class="img" src="{{ storage_path() . ($photo->url) }}" alt="{{ $photo->name }}" /></td>
$photo->url has image filename for example jDHPOTh7iejFABANSzasNbBWJ09DqLbqJb8ymJhv.png
Would you please let me know which part is my problem?
Thanks in Advance
UPDATE :
I'm using PhpStorm Ide and when I download my public folder to my project -or synchronize this- my symbol link does not appear in public folder list...
This is ls from my public folder :
And this is my phpStorm project view AFTER download a fresh copy of public folders in my host:
And as a note :
I started to develop this website in windows at first and then I did move entire project to my linux remote server...
For anyone looking for answer to this question, please follow this link contains great answer
https://stackoverflow.com/a/30191854/417899
Thanks to all for participating, but in my case there was a tricky issue...
I was logged in to my server using root and when I did type php artisan storage:link, the symbol link was made by root as owner and it seems Apache didn't have permission to access this symbol link and because of that, the files was not accessible in http.
SOLUTION
I have a vds that serve 5 different websites, so
I did simply change the owner of symbol link's owner to my relative username and the problem solved...
chown -R usergroup:username /path/to/link
That's it...
I think the issue is here:
$filePath = Storage::putFile('public', $request->file('photo'), 'public');
here you are putting your file on public folder and here:
<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
you are trying to get it from public/storage/. So change the path like:
<img class="img" src="{{ asset( 'public/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
and try again.
Note: Here uploaded images path is stored in public/image.extension, so fetching it from the same public directory
I am trying to load an image in Laravel. The image is loading in a header.blade.php file. It is working fine on other pages but when it comes to localhost:8000/edit/id, It just doesn't work. I am loading Image like this:
<a href="{!! URL::to('/') !!}">
<img src="images/logo.png" />
</a>
My Route:
Route::get('edit/{id}', array('uses' => 'HomeController#edit'));
My Controller:
public function edit($id) {
$reg = Register::find($id);
$user = User::where('email', Session::get('email'))->first();
if ($reg->user_id == $user->id) {
return View::make('edit')
->with('id', $id);
} else {
return Redirect::to('dashboard')->with('wrong', 'Sorry! Something Went Wrong!');
}
}
My image directory: public/images/logo.png
It is working fine on other pages like localhost:8000/about but it doesn't work on localhost/edit/2, any help?
The problem is that when you are on your url, it is not looking for the image in the root folder of your application, but in the current location.
So when you are visiting localhost/edit/2, it is looking for the image in localhost/edit, instead of in localhost.
Try changing your code by adding a slash in the beginning of the image
<a href="{!! URL::to('/') !!}">
<img src="images/logo.png" />
</a>
to
<a href="{!! URL::to('/') !!}">
<img src="/images/logo.png" />
</a>
Additionally, you should actually try the laravel way, like this:
{{ HTML::image('images/logo.png', 'logo') }}
try
{{ HTML::image('images/logo.png', 'logo') }}
output will be like
<img src="http://your.url/images/logo.png" alt="logo">
also you can check image path using inspect element in browser
<img src="public/storage/cover_images/image_name.format" class="img-resposive">
it will load your picture otherwise check the path to image and previlages of the folders you can load image only from a public path due to security reasons
I am trying to create a dynamic image link (in Laravel 5.0), like this:
#foreach($result as $r)
<div>
<a href="{{URL::to('productdetail?id=<?php echo $r->id; ?>')}}">
<img src="..\public\uploads\{!! $r->fileName !!}" height="200" width="250"/>
</a>
</div>
#endforeach
and the link it generates is:
http://localhost:8000/{{URL::to('productdetail?id=1'}}
But I want to generate a link like this:
http://localhost:8000/productDetail?id=1
Any help?
Tux almost had it, but you need to put the variable outside the string. This should work:
<a href="{{ URL::to('productdetail?id='.$r->id) }}">
But why are you using a GET variable for this? The beauty of Laravel is that you can have the ID as part of the URL. If I was writing this I would have this in my routes file:
Route::get('/product/{stub}/{id}', ['as' => 'prodDetailPage', 'uses' => 'ProductDetail#index');
Then you could just use <a href="{{ route('prodDetailPage', [$r->stub, $r->id]) }}">
For stub you can create a function to turn the title into a stub, it would need to convert it to lower case, remove special chars and convert spaces to hyphens.
You can directly use:
#foreach($result as $r)
<div>
<a href="{{URL::to('productdetail?id=$r->id')}}">
<img src="..\public\uploads\{!! $r->fileName !!}" height="200" width="250"/>
</a>
</div>
#endforeach
Well, I have got this, actually I was putting the PHP tags at a wrong place, so the final link would be like this:
<div>
<a href="{!! URL::to('productdetail?id=') !!}<?php echo $r->id; ?>">
<img src="..\public\uploads\{!! $r->fileName !!}" height="200" width="250"/>
</a>
</div>