Topic: Using same image with two types of extensions

Is it possible to use an image for example: 1.gif but if it doesn't exist then something like 1.png or 1.jpg will be displayed in its place?

Bingiman

Re: Using same image with two types of extensions

With PHP, yes, that's how PunBB does avatars. With HTML? No

Re: Using same image with two types of extensions

How would I do this? Here is the line I wanted to use:

<img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/new.png" alt="New posts" />  <small><?php echo $lang_index['New posts']; ?></small>   

Re: Using same image with two types of extensions

Before that, have a PHP line for if(file_exists(/path/to/file.png))

5 (edited by bingiman 2008-01-17 02:32)

Re: Using same image with two types of extensions

I just don't get it. This is what I have at the moment.

if(file_exists(img/'.$pun_user['style'].'/new.gif))
<img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/new.png" alt="New posts" />  <small><?php echo $lang_index['New posts']; ?></small>   

I have something similar that I use on another CMS but I don't know how to do it in pun

$filename = THEME."forum/whosonline.gif";
if (file_exists($filename)) { echo "<img src='$filename' align='left'  alt=''>"; }
else { echo "<img src='".THEME."images/blank.gif' width='40' alt=''>"; }

Re: Using same image with two types of extensions

That's because your code isn't valid PHP. The first line is PHP. The second line is HTML. If you want that to remain, you need to surround the HTML with {}s and close/reopen the PHP tags properly so that it's treated as HTML. If you want to treat it all as PHP, you would echo out the HTML instead.

Re: Using same image with two types of extensions

I did. I tried it as PHP and it doesn't' seem to work for me so I must be doing something wrong.

Re: Using same image with two types of extensions

Could you paste your code?

Re: Using same image with two types of extensions

This is what I have:

<div id="stats" class="block">
    <h2><span><?php echo $lang_index['board_legend'] ?></span></h2>
    <div class="box">
        <div style="padding-top: 10px; text-align: center;" class="inbox">
<?php
if(file_exists(img/'.$pun_user['style'].'/new.gif)) {

?>
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/new.png" alt="New posts" />  <small><?php echo $lang_index['New posts']; ?></small>   
<?php
}

                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/normal.png" alt="No new posts" /><small>  <?php echo $lang_index['No new posts']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/sticky.png" alt="Sticky thread" /><small>  <?php echo $lang_index['Sticky thread']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/closed.png" alt="Closed thread" /><small>  <?php echo $lang_index['Closed thread']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/moved.png" alt="Moved thread" /><small>  <?php echo $lang_index['Moved thread']; ?></small><br /><br />
</div></div>
</div></div>

Re: Using same image with two types of extensions

if(file_exists('img/'.$pun_user['style'].'/new.gif')) {

It's a string, so it needs 's around it

Re: Using same image with two types of extensions

Well, at least now I get no errors but I don't understand why it doesn't work. In my case the default image is new.png but if new.png doesn't' exist it should display new.gif but it doesn't show new.gif when I remove new.png

<?php
if(file_exists('img/'.$pun_user['style'].'/new.gif')) {

?>
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/new.png" alt="New posts" />  <small><?php echo $lang_index['New posts']; ?></small>   
<?php
}
?>
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/normal.png" alt="No new posts" /><small>  <?php echo $lang_index['No new posts']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/sticky.png" alt="Sticky thread" /><small>  <?php echo $lang_index['Sticky thread']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/closed.png" alt="Closed thread" /><small>  <?php echo $lang_index['Closed thread']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/moved.png" alt="Moved thread" /><small>  <?php echo $lang_index['Moved thread']; ?></small><br /><br />
</div></div>
</div></div>

Re: Using same image with two types of extensions

I got it now..

<?php
if(file_exists('img/'.$pun_user['style'].'/new.gif')) {

?>
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/new.gif" alt="New posts" />  <small><?php echo $lang_index['New posts']; ?></small>   
<?php
}else {
?>
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/new.png" alt="New posts" />  <small><?php echo $lang_index['New posts']; ?></small>   
<?php
}
?>

                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/normal.png" alt="No new posts" /><small>  <?php echo $lang_index['No new posts']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/sticky.png" alt="Sticky thread" /><small>  <?php echo $lang_index['Sticky thread']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/closed.png" alt="Closed thread" /><small>  <?php echo $lang_index['Closed thread']; ?></small>   
                                <img style="vertical-align: middle;" src="img/<?php echo $pun_user['style'] ?>/moved.png" alt="Moved thread" /><small>  <?php echo $lang_index['Moved thread']; ?></small><br /><br />
</div></div>
</div></div>

13 (edited by bingiman 2008-01-17 03:14)

Re: Using same image with two types of extensions

Not that I have a clue how to do it but wouldn't this be better as an array for all the images. So in other words if all those png files (normal, sticky etc...) doesn't exist then the ones with the gif extension would be displayed.