ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

img alt - conditional alternative?

August 23, 2011 9:14am

Subscribe [2]
  • #1 / Aug 23, 2011 9:14am

    silverdart

    46 posts

    In Safari, the ‘alt’ parameter in an ‘img’ link never seems to display the alt text - just shows the ugly box with question mark icon. In IE, the alt value is displayed inside a box and next to a little red icon, while in Firefox the text is shown correctly when an image is missing.

    So is there a way of using EE conditionals to display an alternative image, valid for all browsers, when the called one is missing?

    In the site which is giving rise to this question, I am calling this image dynamically within the img tag through a custom field which contains a value equivalent to the filename of the underlying image, eg ‘{customfield}.jpg’ resulting in ‘12345.jpg’. The images are not directly linked to the entry fields (just too many of them to implement that), so they sit in a folder on the server, and are referenced by the custom field value which exists already in the database. This setup works fine, but occasionally there is no image to match the custom field value, so the alt value is called and displayed in various unsatisfactory ways, depending on the browser.

    I have tried playing around with various conditionals, but can’t seem to find a solution. Using a conditional based on the custom field, such as {if customfield}{/if} does not work because the field value is always there, it’s the underlying image which is missing.

    This is probably a newbie-type issue - but any thoughts would be gratefully received. Many thanks.

  • #2 / Aug 23, 2011 9:53am

    Fusion Design

    128 posts

    Hi there,

    Replace the paths and custom field name. Enable php on output and see if this works for you.

    <?php  
    $rootpath = "/home/site/public_html/images/uploads/";        
    $localpath = "{site_url}images/uploads/";                        
    $filename = "{custom_field}.jpg";
    $rootfile = $rootpath . $filename;
    if (file_exists($rootfile)) {
        echo $localpath . $filename;
    } 
    else {
        echo $localpath . "missing.jpg";
    } 
    ?>
  • #3 / Aug 23, 2011 11:16am

    silverdart

    46 posts

    Hi fusion

    Many thanks, but it’s not working for me.

    I wrapped your code (modified for my site) in the same channel entries tag pair as are currently wrapping the img link plus I enabled php for output - and the page shows nothing at all in that space, just blank.

    {code}

    {exp:channel:entries channel="mychannel" status="open" limit="1" disable="categories|pagination"} 

    <?php  
    $rootpath = ”/home/myusername/www/mysite.com/images/myfolder/”;        
    $localpath = ”{site_url}images/myfolder/”;                        
    $filename = ”{custom_field}.jpg”;
    $rootfile = $rootpath . $filename;
    if (file_exists($rootfile)) {
        echo $localpath . $filename;

    else {
        echo $localpath . "missing.jpg";

    ?> 

    {/exp:channel:entries}

    {/code}

    I will carry on tinkering, but any further thoughts would be gratefully received.

  • #4 / Aug 23, 2011 11:19am

    Fusion Design

    128 posts

    You can create a test template with just this code, and change custom_field into segment_2 or 3.
    Then vist that url, and put an existing filename as your last segment.
    Does it return a path for that image, the missing image, or no path at all?

  • #5 / Aug 23, 2011 11:27am

    Fusion Design

    128 posts

    In this demo, I have the following code

    <?php  
    $rootpath = "/home/fusionde/public_html/ajaxcms/images/uploads/";        
    $localpath = "{site_url}images/uploads/";                        
    $filename = "{segment_2}.png";
    $rootfile = $rootpath . $filename;
    if (file_exists($rootfile)) {
        echo $localpath . $filename;
    } 
    else {
        echo $localpath . "missing.png";
    } 
    ?>

    Going to the url http://fusiondemo.co.uk/ajaxcms/filedemo/slide1 has an image path of http://fusiondemo.co.uk/ajaxcms/images/uploads/slide1.png.
    The same applies to slide2 and slide3, which are also images.
    If http://fusiondemo.co.uk/ajaxcms/filedemo/slide4 is entered, there is no image by that name, so missing.png is returned.

    Are you able to successfully setup a demo like this?

  • #6 / Aug 23, 2011 11:57am

    silverdart

    46 posts

    No path at all.

    It’s in a test template, just the code as above with the EE channel entries tags removed and segment_3 instead of custom_field. The source shows that the filename is being output correctly - and it was being output correctly in the previous attempt (including EE tags) - but no image or missing image in either case.

  • #7 / Aug 23, 2011 12:11pm

    Fusion Design

    128 posts

    So your output is something along the lines of: http://mysite.com/images/uploads/.jpg
    Does going to http://mysite.com/images/uploads/the_segment_3_value.jpg give the the actual image?

  • #8 / Aug 23, 2011 12:54pm

    silverdart

    46 posts

    No, my output is nothing, no text, no image, just blank on the page.  Page source shows the full php code, above, with the correct image names in place - and the paths look correct to me.

    The image shows up fine in the direct url to the images directory.

  • #9 / Aug 23, 2011 12:56pm

    Fusion Design

    128 posts

    The page source shows the php code? Are you sure php is turned on in your template?

  • #10 / Aug 23, 2011 1:02pm

    silverdart

    46 posts

    Allow PHP? is set to ‘Yes’. PHP Parsing stage is set to ‘Output’. That should be OK?

  • #11 / Aug 23, 2011 1:08pm

    Fusion Design

    128 posts

    Yeah. Does a simple bit of code output in your template:

    <?php echo "test"; ?>
  • #12 / Aug 23, 2011 1:10pm

    silverdart

    46 posts

    Yes.

  • #13 / Aug 23, 2011 1:26pm

    Fusion Design

    128 posts

    Try removing site_url and use the following.

    <?php  
    $localpath = "/images/uploads/";                        
    $filename = "{segment_2}.png";
    $rootfile = $_SERVER["DOCUMENT_ROOT"] . $localpath . $filename;
    if (file_exists($rootfile)) {
        echo $localpath . $filename;
    } 
    else {
        echo $localpath . "missing.png";
    } 
    ?>

    If that doesn’t work try replacing {segment_2} in the code with an actual filename.

  • #14 / Aug 23, 2011 1:36pm

    Fusion Design

    128 posts

    You could also try is_file instead of file_exists.

    <?php  
    $localpath = "/ajaxcms/images/uploads/";                        
    $filename = "{segment_2}.png";
    $rootfile = $_SERVER["DOCUMENT_ROOT"] . $localpath . $filename;
    if (is_file($rootfile)) {
        echo $localpath . $filename;
    } 
    else {
        echo $localpath . "missing.png";
    } 
    ?>
  • #15 / Aug 23, 2011 1:49pm

    silverdart

    46 posts

    That’s outputting ‘/images/uploads/missing.jpg’ as text, not the actual image, either the segment_3 one or the ‘missing’ one. Likewise if I change {segment_3} to the actual image file name, it outputs the name as text, not the image itself.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases