asset() function in symfony3 does not convert url - php

I want to load pictures dynamically from database.
Here's the table containing the image informations:
Controller code
public function listeproductAction()
{
$em = $this->getDoctrine()->getManager();
$img = $em->getRepository('AppBundle:image')->findAll();
return $this->render('lucky/images.html.twig', array(
'img' => $img ,
));
}
twig code
{% for im in img %}
<img src="{{ asset('/uploads/brochures/' ~ im.getBrochure()) }} ">
{% endfor %}
result
<div class="main-content">
<img src="/uploads/brochures/0fa58c757752ced32d04b57bb7fa0d65.png ">
</div>
The problem is that asset function doesn't find the image.

Related

How to get my gallery to slider in twig file?

I have rendered all my pictures from gallery, but need to get them to slider in my twig file.. is that possible without using any plugin, please???
My code, php:
<?php
$context = Timber::get_context();
$context["post"] = Timber::get_post();
Timber::render( 'pages/single/single-one-collection.twig', $context, CACHE_TIME);
My twig:
{% for picture in post.get_field('oc_gallery') %}
<div class="section" id="section{{ loop.index }}">
<img src="{{ TimberImage(picture).src | resize(400, 266)}}" alt="" />
</div>
{% endfor %}

Octobercms - Getting frontend user avatar

So i'm trying to gererate a frontend user list, but i'm having trouble reaching the avatar url.
i'm able to get all user names but when i try to do the same with the profile picture the fallback is shown.
The {{ user.avatar.url }} is working on the user page when someone is signed in.
I've tried to look for the query used on the backend to get the user avatar on the preview, but i was not able to find it.
I don't know if this is relevant but i'm using https://octobercms.com/plugin/netsti-uploader for frontend users to upload their avatars. It's working since if i upload it on the frontend the backend user preview shows the right avatar
This is what i am using to get all users:
CODE:
use October\Rain\Auth\Models\User;
function onInit() {
$this['activatedUsers'] = User::whereIsActivated(true)->get();
}
MARKUP
<div>
{% for user in activatedUsers %}
<div class="card list">
{% if user.avatar %}
<img class="userimg" src="{{ user.avatar.url }}">
{% else %}
<img class="userimg" src="assets/images/user.png">
{% endif %}
<p class="name"><span class="rank-title">NAME</span><br>{{ user.name }} {{ user.surname }}</p>
{% if user.last_login %}
<p><span class="rank-title">LAST UPDATE</span><br>{{ user.last_login }}</p>
{%endif%}
</div>
{% endfor %}
All help is appreciated, thanks
try to use it like that.
use RainLab\User\Models\User;
function onInit() {
$this['activatedUsers'] = User::whereIsActivated(true)->get();
}
Markup
{% for user in activatedUsers %}
<div class="card list">
{{ user.avatar.path }}
</div>
{% endfor %}
Have a look at October\Rain\Database\Attach\File class to see available methods :
getThumb($w,$h,$options) - Generates and returns a thumbnail path
getPath() - Returns the public address to access the file
getLocalPath() - Returns a local path to this file. If the file is stored remotely,it will be downloaded to a temporary directory.
getDiskPath() - Returns the path to the file, relative to the storage disk
e.g :
{{user.avatar.getThumb(200,200, { mode : 'crop' } )}}
install "Frontend File Uploader for Model" plugin
insert {% component 'imageUploader' %} in your markup
insert
function onInit()
{
$user = Auth::getUser();
if($user){
$component = $this->addComponent(
'NetSTI\Uploader\Components\ImageUploader',
'imageUploader',
['modelClass'=>'RainLab\User\Models\User','modelKeyColumn'=>'avatar', 'deferredBinding' => false]
);
$component->bindModel('avatar', $user);
}
}
on your code section
You can query the backend user model with with 'avatar'.
use Backend\Models\User;
...
$user = User::where('id', $author_id)->with('avatar')->first();
...
<h1>{{ user.avatar.path }}</h1>

symfony 3 - url image from database

I wanted to display the url of an image from bdd in my file twig I receive an error of the image like the:
this is my bdd :
and my view from twig :
{% extends '#App/layout.html.twig' %}
{% block title %} Illustration {% endblock %}
{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
<h2> {{ imageFormation.image.nom }}</h2>
<hr>
<img src=" {{ imageFormation.image.url |raw }} "/><br><br>
</div>
{% endblock %}
Can you help me solve my problem please?
i dont know why i cant get the real image.
edit :
/**
* #Route("/formation/{formation}" , name="image")
* #param $image
* #return \Symfony\Component\HttpFoundation\Response
*/
public function imageFormationAction($formation){
$doctrine = $this->container->get('doctrine');
$em = $doctrine->getManager();
$imageRepository = $em->getRepository('AppBundle:Formation');
$imageFormation = $imageRepository->findOneByid($formation);
return $this->render('#App/formation/formationImage.html.twig', array(
'imageFormation' => $imageFormation
));
}
Your image url must be relative to web directory, like this fichier\Mention.jpg, not the image's path in your file system C:\xampp\htdocs\Symfony\MonProjetCv\web\fichier\Mention.jpg.
Save a relative path to your image file in the database (url equal fichier\Mention.jpg), and use asset function in your twig file:
{% extends '#App/layout.html.twig' %}
{% block title %} Illustration {% endblock %}
{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
<h2> {{ imageFormation.image.nom }}</h2>
<hr>
<img src=" {{ asset(imageFormation.image.url); }} "/><br><br>
</div>
{% endblock %}
Can you please try this way?
<img src='{{ asset("IMAGE_DIRECTORY_PATH/"~ image.url)}}' />

How to preview images inside mybundle symfony 3

I am trying to preview images which stored inside mybundle/image folder. but i can not get proper image path for view images which name is dynamic.
here is the code.
// inController
$image = $this->get('file_locator');
$path = $image->locate('#AcmeMyBundle/uploads/images/my_img.jpg');
return $this->render('images/myaction.html.twig', array(
'path'=>$path,
));
//in myaction.html.twig
{% image '#SeStorageBundle/uploads/images/'{{ image.name }} %}
<img src="{{ asset_url }}" width="100%" height="100%" alt="my alt of image" class="pull-left">
{% endimage %}
If the link to your image is correct, then try using asset to render the image like this:
<img src ="{{asset(path)}}"/>

how to display blob image stored in mysql database in symfony 2.7.3

I am having trouble displaying blob image on my web site, am using symfony 2.7.3...
i tried to encode the blob data in base 64, am able to display the base 64 value but when i embed it in nothing happens bellow is my code.
pls what am in doing wrong ??
controller
$badges = $this->getDoctrine()
->getRepository('AppBundle:News')->findAll();
$images = array();
foreach ($badges as $key => $badge) {
$images[$key] = base64_encode(stream_get_contents($badge->getImage()));
}
return $this->render('default/index.html.twig', array(
'badges' => $badges,
'images' => $images,
));
}
view
{% if badges %}
{% block badge %}
{% for key,badge in badges %}
<div style = "margin:4px;height:100px" class="thumbnail">
<div class="pull-left">
<a href=""><img alt="embeded image"src="data:image/png;base64,{{ images[key] }}" />
</div>
<div style="width:auto" class = "pull-right" >
{{ badge.title }}
</div>
</a>
</div>
{% endfor %}
{% endblock %}
{% endif %}

Categories