+ Start a Discussion

URLFOR - Check for image in zip file before displaying

I have a simple visual force page and controller for displaying an image within a custom object page layout.  The only issue is that I want to be able to display a generic image if the dynamic image is not available.  For example, I'm displaying inventory images.  If some inventory items have no image, then I'll display a "No Image Available" image.


I could add a checkbox to the custom inventory object and set it to true when there is an image or else leave it false when there won't be an image in my zip file.  But it would be easier if I could simply identify within the VF page or the controller whether the image was brought back so that I could determine whether to display the generic image or not..

public with sharing class InventoryImageController {
      public SPECIALTY_ITEM__c specialtyItem;
      public String Name       {get;set;}       
      public String ImageName  {get;set;}         

public InventoryImageController(ApexPages.StandardController controller)           
  { if (ApexPages.currentPage().getParameters().get('id')!= null) {       specialtyItem = [SELECT Id, Name
                    FROM SPECIALTY_ITEM__c                               WHERE Id = :ApexPages.currentPage().getParameters().get('id')];           }                         
 ImageName = specialtyItem.name + '_s.jpg';      

 I was trying to play around with rendered and an output panel but I'm not sure that's the route to take.  Is there an easy solution to this besides adding custom field to the custom object to set whether there will be an image in the zip file or not?


<apex:page standardController="SPECIALTY_ITEM__c" extensions="InventoryImageController">
<apex:variable var="imageVar" value="{!ImageName}"/>

<apex:outputPanel id="itemImagePanel" rendered="true">
<apex:image url="{!URLFOR($Resource.Inventory_Images, imageVar)}" id="itemImage" width="100" height="100" style="position:relative;left:235px"/>
<apex:image url="{!URLFOR($Resource.Inventory_Images, "NoImage_s.jpg")}" rendered="false" id="noImage" width="100" height="100" style="position:relative;left:235px"/>