Melodia's avatar

How to access public path image

I tried a couple solutions that I've seen here and stackoverflow but nothing works. I am rendering html content directly from a controller for pdf generation, and in that html I need to access an image from my images folder under public path.

How can I get this right?

This is what I have right now that does not work and no error is shown.

public function convert_customer_data_to_html(){
 $customer_data = $this->get_customer_data();
 $output = '
        <td><img height="50" src="asset(/imageslogo-1.png)" alt="Image"/></td>
  '
0 likes
10 replies
pardeepkumar's avatar

I suggest you to try something like that

Just put your Images in Public Directory (public/images_folder).

Let's suppose I stored images in public/images/Cloudways_logo.jpg.

View

<img src="{{url('/images/myimage.jpg')}}" alt="Image"/>

Melodia's avatar

This will give a syntax error

syntax error, unexpected 'png or jpg'

And I assume I cannot use blade inside a php file since this is being done in the controller.

tykus's avatar

Are you using any specific package for generating the PDF?

tykus's avatar

Ok, I think you should be able to use the public_path() helper in that case:

<img height="50" src="public_path('imageslogo-1.png')" alt="Image"/>

This will expand to the real path to the image file on your server's filesyste,

bestmomo's avatar

I dont understand how you manage your code. Why dont you use Blade for this ?

webtrickshome's avatar

public_path() will provide you with the path directory upto the public folder. You can then concatenate your filename as shown below. {{ public_path().'/imageslogo-1.png' }} Hope, this helps you.

Melodia's avatar

@tykus I tried this:

No error, but the image still does not show.

@bestmomo, Am using DomPDF to display database data, for that reason the content has to be rendered inside the controller. So I cannot use blade.

bestmomo's avatar
bestmomo
Best Answer
Level 52

Expecting images are in images directory :

$output = '
        <td><img height="50" src="' . url("images/imageslogo-1.png") . '" alt="Image"/></td>
  '
1 like

Please or to participate in this conversation.