Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine the Width and Height of an External Image

Status
Not open for further replies.

Krus1972

Programmer
Mar 18, 2004
145
US
Does anyone know if there is a way, in classic ASP, to determine the height and width of an external image, that is not located on the server, and store these values in seperate classic ASP variables?

For example, I would like to determine the height and width of the following image without having to save it locally and then store each value into separate ASP variables:


Thanks for your help
 
Krus1972 said:
Does anyone know if there is a way, in classic ASP, to determine the height and width of an external image, that is not located on the server, and store these values in seperate classic ASP variables?

There isn't.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Hello - this might be a bit of a late comment but I thought you might be interested in a possible workaround.

1. Load image into DIV with javascript
2. Measure image using javascript
3. Send dimensions to ASP page to save values to database

Code:
<html>
<head>
<script>
function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

function getImage(imageURL) {
    document.getElementById('imageDiv').innerHTML = "<img id='myImage' src='" + imageURL + "' />";
}

function measureImage() {
    var myImage = document.getElementById('myImage');
    document.getElementById('imageWidth').value = myImage.width;
    document.getElementById('imageHeight').value = myImage.height;

    sendMeasurementsToServer();
}

function sendMeasurementsToServer() {
    xmlhttpSendMeasurementsToServer = GetXmlHttpObject();
    if (xmlhttpSendMeasurementsToServer == null) {
        alert("Your browser does not support XMLHTTP!");
        return;
    }

    var imageURL = document.getElementById('imageURL').value;
    var imageWidth = document.getElementById('imageWidth').value;
    var imageHeight = document.getElementById('imageHeight').value;

    url = "savemeasurements.asp?ImageURL=" + imageURL + "&ImageHeight=" + imageHeight + "&ImageWidth=" + imageWidth;
    url = url + "&sid=" + Math.random();
    xmlhttpSendMeasurementsToServer.onreadystatechange = function() {
        if (xmlhttpSendMeasurementsToServer.readyState == 4) {
            var aspResponse = xmlhttpSendMeasurementsToServer.responseText;
            alert(aspResponse);
        }
    }
    xmlhttpSendMeasurementsToServer.open("GET", url, true);
    xmlhttpSendMeasurementsToServer.send(null);
}
</script>
<head>
<body>
Enter image URL
<input type="text" id="imageURL" style="width:500px;padding:10px;">
<input type="button" value="Measure" onclick="getImage(document.getElementById('imageURL').value); measureImage();">
<div id="imageDiv"></div>

Client-side measurement
<br>
Width:<input type="text" id="imageWidth">
Height:<input type="text" id="imageHeight">


</body>
</html>

And make a file called savemeasurements.asp

Code:
<%
ImageURL=Request.QueryString("ImageURL")
ImageHeight=Request.QueryString("ImageHeight")
ImageWidth=Request.QueryString("ImageWidth")

If ImageURL<>"" And ImageHeight<>0 And ImageWidth<>0 Then
  'Connect to database here and save data
  Response.Write("Server side code now executing - Saving dimensions of " & ImageURL & " (" & ImageWidth & " x " & ImageHeight & ") to database")
End If
%>

I have tested it and it basically works although it might need a little refinement as if the image hasn't finished loading then it doesn't have time to get the dimensions of the image. There might be some kind of event to watch for to show that the image has finished loading....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top