Different file formats in URL::asset(...)

Published 3 weeks ago by Ivans84

Hello everybody.

I have a web page with some pictures. Most of them are in .jpg format, but I also have some in .png format. I need to make a universal link which could let me display both format pictures. At the moment my code looks like this:

<div>
    <a href="{{ URL::asset('images/pages/albums/'.$albumName.'/'.$picture->pictureName.'.jpg') }}">
        <img src="{{ URL::asset('images/pages/albums/'.$albumName.'/small/'.$picture->pictureName.'.jpg' }}">
    </a>
</div>

If you can help me please, how I need to change the line started with "<a href..." and "<img...", so two formats could be displayed?

Cronix
Cronix
3 weeks ago (647,500 XP)

Simple. The file extension (.jpg/.png/.whatever) should be a part of the actual filename in $picture->pictureName (yourimage.jpg/yourimage.png, etc). I've never seen anybody store files without the extension. Why would you do that? If you save files normally with the extension, then it doesn't matter. You'd just

<a href="{{ URL::asset('images/pages/albums/'.$albumName.'/'.$picture->pictureName }}">
        <img src="{{ URL::asset('images/pages/albums/'.$albumName.'/small/'.$picture->pictureName }}">
</a>
Ivans84

Thank you for a quick reply. The problem is that $picture->pictureName is coming from the database. In the database these picture names are stored without any extension as I use these picture names for some another purposes as well. Do you know if there are any possible constructions like ".*" or "'.jpg' || '.png'"?

Cronix
Cronix
3 weeks ago (647,500 XP)

Do you know if there are any possible constructions like ".*" or "'.jpg' || '.png'"?

No. Problem is php doesn't know whether the file on the filesystem with .png/.jpg exists. So you'd have to wrote code to first check wither filename.jpg exists, if not, use filename.png. Quite wasteful and intensive, when there is a much simpler solution.

Store them with the file extension (and store the base filename in another column without extension). Or have another column to store the file extension.

pictures
-pictureName
-pictureExtension
<img src="{{ URL::asset('images/pages/albums/'.$albumName.'/small/'.$picture->pictureName . '.' . $picture->pictureExtension}}">

then it will all be dynamic. Personally I'd store it with the extension, and use a separate column to store the base filename to use however else you're using it.

Ivans84

Thank you very much. That is what I needed to know. Also thank you for your advice, I will do that.

Please sign in or create an account to participate in this conversation.