Is there any easy way to set image asset in symfony 2 - php

I have the html template where i image is referenced as images/logo.gif
Now in Symfony templates i have to use
src="{{ asset(['images/', 'logo.gif']|join) }}"
Is it possible to use something like
src="{{ asset(['images'])}}/logo.gif
so that i need not replace all the image tags in html file. Then i can find and replace easily. Othwise i have to manually change all image occurances

Why don't you just use this?
src="{{ asset('images/logo.gif') }}"
EDIT:
I didn't try it but concatenation should work as well:
{% set imageDir = 'images/' %}
src="{{ asset(imageDir ~ 'logo.gif') }}"
However, it introduces unnecessary complexity. I don't see anything wrong in a hard coded version. You don't change it often.

Related

How to make an asset to show dynamically in Laravel

I'm trying to fix an issue with the avatar image in the header. If I use the following URL it works fine :
src="{{asset('user/uploads/avatars/1562309192.jpg') }}"
While if I try to make it dynamic and use the below, it doesn't work :
src="{{asset('user/uploads/avatars/{ Auth::user()->avatar}') }}"
Also just to mention, the below is working fine for some pages but others not. Thats why I decided to use the asset() helper.
src="user/uploads/avatars/{{ Auth::user()->avatar }}"
I hope this helpful you
src="{{asset('user/uploads/avatars/'.Auth::user()->avatar) }}"
Try it with excluding Auth::user()->avatar from the string.
<img src="{{ asset('user/uploads/avatars/'". Auth::user()->avatar .") }}">

image working in index.blade.php but not show.blade.php

I am using Laravel to build a Events management system. Users can upload images to be displayed with their event. I have the img tag working on my index.blade.php and assumed I could use the same code on my show.blade.php page, it seems not.
I have the image stored in public/images and I am aware I should use storage for this but I am still learning basics so found this easier for now.
<img style="width:100%" src="images/{{$event->image}}">
As #Frank Ayyaz said you can use a backslash to solve this another Laravel-way is using public_path method which returns the fully qualified path to the public directory.
so you can do something like:
<img style="width:100%" src="{{ public_path('images/'.$event->image) }}">
Please use asset() method. The asset() method is used to include CSS/JavaScript/images files, you can use it as:
{{ URL::asset('images/'.$event->image) }}
<img style="width:100%" src="{{ URL::asset('images/'.$event->image) }}">
You can also use this function for CSS and js.
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
<script src="{{ asset('js/custom.js') }}"></script>
For detail please see this article:
https://medium.com/#zwacky/laravels-url-to-vs-url-asset-fd427ed6f7ef
I suggest Not to use URL::asset but only asset method because sometime URL may cause Problem while showing the image...and use dd() function and check whether u are getting the path i.e Public_path , storage_path() or the custom like the image are stored in public directory..
You may use asset() method of Laravel and use any Prefix whatever you like..
Example:
For static
<img src="{!! asset('assets/images/cams/cam-01.png') !!}" class="img-fluid"
alt="Card image cap">
For Dynamic
<img src="{!! $settings->aws_url.$performer->coverimage !!}" class="img-fluid" alt="Card image cap">
when u use js or css or images u should use {{asset('path')}} to get the resource, it has noting to do with the blade u use, but the resources path.

Img not displaying correctly in Symfony

My twig file goes like this :
Let us try to see an image :
<img src="{{ absolute_url(asset('app/Resources/images/bulb.png')) }}" alt="Symfony!" width="42" height="42"/>
Trying it another way :
<img src="app/Resources/images/bulb.png" alt="Symfony!" width="42" height="42"/>
But when I go to that page in Symfony, I see something like this :
What did I do wrong ?
currently, twig is going to be trying to find your asset in:
/web/app/Resources/images/bulb.png
use instead:
{{ asset('#AppBundle/Resources/public/images/bulb.png', absolute=true) }}
note the use of an additional public folder. If you must store assets in app, then this is sensible.
However, #Ewan Delanoy is correct, you really should be storing all your assets directly in the web folder.
Then you can just call
{{ asset('images/bulb.png', absolute=true) }}

Laravel 4.1 blade linking to user profile picture

I am trying to link to a user profile image in Laravels blade template but I am only getting errors here.
This is my image tag containing the link:
<img class="img-circle dashboardprofileimage" src="{{ URL::asset('img/profile_pictures/users/{{ Auth::user()->profile_picture }}') }}"/>
I would be very happy if anyone could help me out here. I guess its a simple thing but I have tried quite a lot of times now.
Thanks.
Your error is using nested {{ }}, you just need it once. Check out the correct code below:
<img
class="img-circle dashboardprofileimage"
src="{{
URL::asset('img/profile_pictures/users/' . Auth::user()->profile_picture)
}}" />
Note: it's splited into several lines for better legibility.
you just use {{ }} one time to print URL::asset() and Auth::user()
<img class="img-circle dashboardprofileimage" src="{{ URL::asset('img/profile_pictures/users/'.Auth::user()->profile_picture) }}"/>

Passing Assetic image path from database

I have been stumped for a couple days and I am seeking some direction.
I am attempting to call an image path stored in database to twig file in order to display said image. The twig example below, I am expecting the same image to be displayed twice. When inspecting the rendered html, the variable passes the path from the database, but the first image is not displayed.
From controller:
'logo' => $vendor->getLogovendors()
From database column logoVendors:
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
From twig:
<div class="container">
{{logo | raw}}
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
</div>
I am new to Symfony and its asset management. Any help or prodding in the right direction would be appreciated.
You should normally store only the path to the image in your database!
If logo was the variable you pass to the template holding the image path bundles/loginlogin/img/fs_logo_large.png you could simply include it using twig's asset function like this:
<img src="{{ asset(logo) }}"/>
what you're trying to do ( evaluating a twig function inside a string ) can be solved aswell...but i don't recommend it.
If you want to store the complete code including {{ asset() }} in your database you need to make twig evaluate the code inside the string.
This means twig shall execute the code inside the string instead of just printing it.
This can be achieved using the evaluate filter from this answer.
The final result would then be:
{{ logo |evaluate |raw }}

Categories