function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Is there a way to use contentType="image/png" in apex:page?

We're trying to use Visualforce/Apex to drive a dynamic image from images stored ina  static resource.  The following doesn't work



<apex:page controller="RescueChannelImageCon" contentType="image/png">
	{!URLFOR($Resource.Rescue_Resources, "images/chatavailable.png")}	


<apex:page controller="RescueChannelImageCon" contentType="image/png">
	<apex:image url="{!URLFOR($Resource.Rescue_Resources, "images/chatavailable.png")}" alt="{!$Label.ChatNow}" />
	<apex:image url="{!URLFOR($Resource.Rescue_Resources, "images/chatunavailable.png")}" alt="{!$Label.ChatUnavailable}" rendered="{!NOT(status)}" />



The idea is to allow another website to embed/hotlink this dynamically rendered image (Apex and VF decide what image to use, other site grabs it via same URL) without using clunky iframes.  


Will this work for you?


<apex:page controller="RescueChannelImageCon">
     <apex:variable var="imageVar" value="{!DynamicImageName}"/>
     <apex:image url="{!URLFOR($Resource.Rescue_Resources, imageVar)}" />

public with sharing class RescueChannelImageCon 
   public String DynamicImageName {get; set;}

   public RescueChannelImageCon ()
	// Logic to determine proper image from the resource here
        DynamicImageName = 'chatavailable.png';



unfortunately no.  your code is basically what i have, just putting logic more in controller (doing the same thing, different approach).  i need to render the actual page as an image, not render HTML image tags.  


i may try your approach for smallish millisecond performance boost though, as it'd not be using multiple getters in the controller.  i'll decide on that once i profile it with the new debugger.


i think the only approach that visualforce could support to do this is passing blob content from controller to vf, which it definitely doesn't support, and even more so with using static resources, which aren't accessible in apex.


to give you an idea, i was trying to take this


<iframe allowTransparency="true" frameBorder="0" height="45" width="105" scrolling="no" src="[SITE URL]/RescueChannelImage?channel=Support" />



and turn it into this


<script type="text/javascript">
function launchchat(){"[SITE URL]/RescueInstantChatForm?channel=Support","RescueChat","status=0,toolbar=0,scrollbars=0,resizable=0,menubar=0,width=363,height=466");
<img src=[SITE URL]/RescueChannelStatusIcon?channel=Support" 
onclick="launchchat()" />


so that the customer embedding the code would be able to directly embed a dynamically chosen (by Apex) image on their own website.  the iframe approach works, but the image approach is more performant, and a much better end user experience.