Page 1 of 1

How do you split up a filename in PHP, and insert code?

Posted: Mon Mar 06, 2017 10:27 am
by simonmlewis
We are looking at using srcset in our website, writing it ourselves.

Is it best to store each version name in it’s own database field? image, image475, image768 and so on?
Or a better way I was thinking (which is similar perhaps to Wordpress) is to dynamically write on page what numbers we want.

So :

Code: Select all

<img srcset="/images/pages/$row->image”.475.”.jpg 475w,
             /images/pages/$row->image”.768.”jpg 768w,
             /images/pages/$row->image”.475.”.jpg 1920w"
     sizes="(max-width: 475px) 475px,
            (max-width: 768px) 768px,
            (max-width: 1920px) 1920px"
     src="/images/pages/25644881item-phone-name.jpg" alt='photo name here'>
$row->image”.475.”
My question is, how do I break up the filename from the variable (in case we did a .jpeg or a .png), and concatenate in that file extension?

So we take $row->image, split up the name and extension into two variables. So it reads a little more like this:

Code: Select all

<img srcset="/images/pages/$imagename"."475"."$imageextension"." 475w,....

Re: How do you split up a filename in PHP, and insert code?

Posted: Mon Mar 06, 2017 12:00 pm
by Celauran
There seems to be quite a lot going on in that question, so let me start with this:
simonmlewis wrote:My question is, how do I break up the filename from the variable (in case we did a .jpeg or a .png), and concatenate in that file extension?

So we take $row->image, split up the name and extension into two variables. So it reads a little more like this:

Code: Select all

<img srcset="/images/pages/$imagename"."475"."$imageextension"." 475w,....
pathinfo
Specifically, see the first example.

Re: How do you split up a filename in PHP, and insert code?

Posted: Mon Mar 06, 2017 12:22 pm
by simonmlewis
Yes path info is perfect. Works well. Thanks.