SearchSearch   MemberlistMembers   RegisterRegister   ProfileProfile   Log inLog in 

Capturing Remote Images Using ASPJpeg

 
Post new topic   Reply to topic    Open Hosting Internet Solutions Forum Index -> Articles and Tutorials -> Server Side Web Development
View previous topic :: View next topic  
Author Message
Nick
Forum Moderator


Joined: 18 Jun 2002
Posts: 3635

PostPosted: Sun Mar 19, 2006 1:27 am    Post subject: Capturing Remote Images Using ASPJpeg Reply with quote

Capturing Remote Images Using ASPJpeg

ASPJpeg is a fantastic way of manipulating images on the server. But what if the image is on another server completely?

Introduction
This problem reared its ugly head when developing the OpenHosting website. On the new Status page we wanted to have a graph of the current network throughput. The only problem was that the software generating this graph sits on a separate web server to the OpenHosting website. We wanted to be able to grab this image and use ASPJpeg to clean it, resize it, and most importantly, cache it.

How it's done...
Thankfully there's a simple solution to grabbing an image from a remote server. Using the XMLHTTP object (commonly used to request remote documents such as XML files) we request the PNG image file, and use ASPJpeg's OpenBinary method to read the incoming byte stream. The image is then saved as a JPEG on the web server. A simple ASP script works out whether the cached image needs updating. If so, the above routine is executed. If not, the page simply returns the cached JPEG image. Here's the (modified) code.

Code:
<%
If cacheFileIsExpired("statusgraph.jpg", 4,"hours") Then
   Response.Write("<img src=""getRemoteImage.asp"" alt=""Updated image"" />")
Else
  Response.Write("<img src=""cachedImage.jpg"" alt=""Image loaded from cache"" />")
End If
%>


And the getRemoteImage page that works the XMLHTTP magic:
Code:
<%
Dim objXMLHTTP : Set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
Dim objJpeg : Set objJpeg = Server.CreateObject("Persits.Jpeg")
objXMLHTTP.Open "GET", "http://www.domain2.com/theImage.jpg"
objXMLHTTP.Send
objJpeg.OpenBinary(objXMLHTTP.ResponseBody)
objJpeg.Save "d:\webs\domain1.com\wwwroot\cachedImage.jpg"
objJpeg.SendBinary
Set objXMLHTTP = Nothing
Response.End
%>


Note that the "cacheFileIsExpired" function in the first code example is the same that is used in my Data caching using text files article.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Open Hosting Internet Solutions Forum Index -> Articles and Tutorials -> Server Side Web Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.11 © 2001, 2002 phpBB Group
FAQClick here for help using the phpBB forum