Forum Moderators: open

Message Too Old, No Replies

resize image in iframe

         

jackvull

11:59 am on Jun 16, 2005 (gmt 0)

10+ Year Member



Hi
I have a PHP page that is essentially going to be a gallery of pictures. Ideally what I want is for a user to click on the numbered link and that loads an image into an iframe on the same page. This works but the images are too big to fit onto the current layout. Is there a way I can use JavaScript to load the image into the iframe and resize it automatically? I've looked on loads of pages and can't seem to find an answer.
Below is the current PHP script, which does everything apart from resize the images:
Thanks!

<?php include("session.php");?>

<HTML>
<HEAD>
<TITLE>...</TITLE>

<LINK REL=STYLESHEET HREF="style.css" TYPE="text/css">
<script language = "javascript" type="text/javascript" src="jsfunctions.js">
</script>

</HEAD>
<BODY>

<?php include("menu.html");?>

<div STYLE='position:absolute;top:170; left:220; '>

<?
// initialize counter
$count = 0;

// set directory name
$dir = "./flyers";

echo "<table width = '400'><tr>";

// open directory and parse file list
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
// iterate over file list
// print filenames
while (($filename = readdir($dh))!== false) {
if (($filename!= ".") && ($filename!= "..")) {
$pos = strstr($filename, 'Thumbs');
if ($pos == false) {
$count ++;
//echo $dir . "/" . $filename . "\n";
echo "<a class= 'gallery' href = ".$dir."/".$filename." TARGET='myiframe'>".$count."</a>&#160";

}
}
}
// close directory
closedir($dh);
}
}
echo "</tr>";
echo "<tr><td><p><br /></p></td></tr>";
echo "<tr><IFRAME id = 'myiframe' name = 'myiframe' frameborder = '0' WIDTH=400 HEIGHT=250 border='0' >If you can see this, your browser doesn't understand IFRAME.</A>";
echo "</IFRAME></tr>";
echo "</table>";

?>

</div>

<?

//Now close the recordset and the connection object
mysql_close();

?>

</BODY>
</HTML>

Sathallrin

1:50 pm on Jun 17, 2005 (gmt 0)

10+ Year Member



You can use this javascript:

<script language="JavaScript" type="text/javascript">
function showimageinframe(imagelink,framename,width,height) {
/* function showimageinframe by Sathallrin */
var source = '<img width="'+width+'" height="'+height+'" src="'+imagelink+'" />';
var myframe = document.getElementById(framename).contentWindow.document;
myframe.open();
myframe.write(source);
myframe.close();
}
</script>

and then change your link to this:

echo "<a class= 'gallery' href = 'javascript:showimageinframe(".$dir."/".$filename.",'myiframe',350,250)'>".$count."</a>&#160";

You can change the height/width in the link as needed.

jackvull

10:01 am on Jun 20, 2005 (gmt 0)

10+ Year Member



Thanks. I replaced this PHP line with:
echo "<a class= 'gallery' href = 'javascript:showimageinframe(".$dir."/".$filename.",myiframe,200,200);'>".$count."</a>&#160";
as the 'myiframe' was causing an error.
Is this right?

So, for example, this then gives me a link of:
javascript:showimageinframe(./gallery/a.jpg,myiframe,200,200);

However, this still throws an error of:
Line: 1
Char: 18
Error: Syntax
Code: 0

This error is in IE. Firefox doesn't show an error but nothing happens either.

Any ideas?

Sathallrin

12:40 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



You need to quote the text inputs... So try this:

echo "<a class='gallery' href=".'"javascript:showimageinframe('."'".$dir."/".$filename."','myiframe',200,200);".'">'.$count."</a>&#160";

jackvull

1:34 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



That did it. Thanks very much!