Aw heck, I’m feeling ambitious today!
Here’s some code to try (see it in action: JavaScript/PHP Image Test)…
<html>
<head>
<title>JavaScript Image Test</title>
<script language="JavaScript">
function popWin(picname) {
var picture = document.picname;
var SRC = document.picture.src;
var height = document.picture.height + 50;
var width = document.picture.width + 50;
window.open(SRC,'popup','width=' + width + ',height=' + height + ',menubar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=no,top=50,left=50');
}
</script>
</head>
<body>
<?php
$array = GetImageSize("images/mycoding.gif");
?>
<p><a href="" onClick="popWin('picture'); return false;"><img src="http://www.mycoding.com/images/mycoding.gif" <?php echo($array[3]); ?> name="picture" border="0" alt="ALT Text Here"></a></p>
</body>
</html>
What’s happened here is that PHP’s GetImageSize() function has grabbed the data I mentioned earlier - and as before, the 4th element (numbered 3, though…) contains the HTML attributes to save us all some time. I’ve echoed that info in the middle of the IMG tag…
Since the PHP is processed on the server-side, the code is spit out as normal HTML, allowing JavaScript to grab the height/width attributes as before and add their 50 pixels onto it.
This could should be fairly easy to modify for use with a database, for example, which holds references to all these images. I think the function should have no problem with multiple images, as long as their name attribute is unique - if you were, say grabbing this info from an SQL database, you could simply use some kind of PictureID number as the name to make sure it is unique each time.
Now, I know what you’re thinking: it’s too plain. As it is now, it just open the image in a new window without any HTML formatting. However, I don’t think it would be too tricky to get around this.
Perhaps this line could be changed:
var SRC = document.picture.src;
…to something like this…
var SRC = “http://www.yoursite.com/showimage.php?src=” + document.picture.src;
Then, you just setup the showimage.php file to take the “src” variable from the Query String and use it to display the image in full - you can format around it as necessary!
TAAAAAAAAAAA-DAAAAAAAAA!