Page 1 of 1

[SOLVED] problems with window sizing

Posted: Thu Aug 04, 2005 3:08 am
by jrucifer
i have my website set up so that select users can update in a blog-type style and upload images or whatever else... everything is working out fine, except i can't figure out how i would go about sizing each image pop-up window to the correct image.

this is how i currently have it set up...

query:

Code: Select all

$query = " SELECT * FROM images ";
$result = mysql_query($query); 

while ($row = mysql_fetch_array($result)) 
{
$popheight = $row["iheight"]+'16';
$popwidth  = $row["iwidth"]+'16';
$wintitle  = $row["name"];
regex:

Code: Select all

$body = preg_replace("|\[image=[\"\']?(.+?)[\"\']?\](.+?)\[/image\]|i","<a href=\"javascript:;\" onClick=\"image(\'/idisplay.php?name=$1\',\'$wintitle\',\'scrollbars=no,toolbar=yes,status=yes,resizable=yes,left=100,top=100,width=$popwidth,height=$popheight\')\">$2</a>",$body);
i'm aware that this just pulls the height and width from the most recent image put in the database, but i just can't seem to find a solution using what little knowledge i have in php.

I also tried putting a javascript in the idisplay.php to resize the window to the iheight and iwidth dimensions, but that didn't give me the correct window size.

basically my question is: how can i select the correct image dimensions for the appropriate images while maintaining the proper regex code?

i confuse myself just trying to put this in the right words, but if you have a solution for me please help.

here's the website i'm working on...
http://www.skateaz.com

Posted: Thu Aug 04, 2005 4:49 am
by s.dot
use getimagesize() to populate your links to the image.

Code: Select all

$size = getimagesize('source.jpg')

$width = $size[0] + 16;
$height = $size[1] + 16;
To generate a popup window with these dimensions

Code: Select all

<a href="#" onClick="javascript:window.open('linktopictureorpagewithpicture.php','Name','menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,height=<? echo $height; ?>,width=<? echo $width; ?>,resizable=no');">

Posted: Fri Aug 05, 2005 3:14 pm
by jrucifer
thanks, i understand that part... but thats only the begining. now i need to be able to dynamically select the right image out of the database when a user types in the name of the image. for example:

a user uploads an image and names it 'funny', it's 300px by 500px.
then, they go to update their section and they type:
"this is [image='funny']hilarious[/image]"

if i just use

Code: Select all

$query = " SELECT * FROM images ";
$result = mysql_query($query); 

while ($row = mysql_fetch_array($result)) 
{

$source = $row["source"];
$wintitle  = $row["name"];

$size = getimagesize("$source"); 

$width = $size[0] + 16; 
$height = $size[1] + 16; 
}
to get the image size, than that just grabs the size for whichever image is next in the database. I need to be able to get the image sizes for the names the user types in, and then have those image sizes put with the correct links that are generated... especially when users are linking to multiple images at a time. thanks

Posted: Fri Aug 05, 2005 3:41 pm
by feyd
before you start processing the message/text/whatever, perform the query on the image table, run through all the imagesm storing them in an associative array where the key is their name. While processing the query results perform your image size checks and store the dimensions and whatever other data you require as the value (should be an array) of that image's element..

Posted: Sat Aug 06, 2005 2:59 pm
by jrucifer
i'm not sure i understand exactly what you're saying. this is what i have...

Code: Select all

$source = $row["source"];
$name  = $row["name"];

$size = getimagesize("$source"); 

$width = $size[0] + 16; 
$height = $size[1] + 16; 
$iarray = array($name, 1 => $width, $height);

$iwidth  = array($iarray[0], 1 => $iarray[1]);
$iheight = array($iarray[0], 1 => $iarray[2]);
still just pulls the last entry from the database as the width and height...

Posted: Sat Aug 06, 2005 4:50 pm
by s.dot
You need a WHERE clause in your query.

Judging by your first post, your query is

Code: Select all

$query = " SELECT * FROM images ";
That would grab all the rows, but unless you put it in a loop, when you get data from that, it will only return the first row.

You need to specify which image you want to grab from the database. Perhaps something like:

Code: Select all

$query = "SELECT * FROM images WHERE id = '$imageid'";
$imageid could be set in your link to the picture, from a form, etc.

Posted: Sat Aug 06, 2005 4:56 pm
by feyd
really rough idea:

Code: Select all

$query = mysql_query('all images') or die(mysql_error());

$images = array();
while($row = mysql_fetch_assoc($query))
{
  if(($size = getimagesize($row['path']) !== false)
  {
    $images[$row['imageName']] = array();
    $image =& $images[$row['imageName']];
    $image['path'] = $row['path'];
    $image['width'] = $size[0];
    $image['height'] = $size[1];
    $image['type'] = $size['mime'];
  }
}
for output:

Code: Select all

if(isset($images[$imageName]))
{
  echo '<img src="' . $images[$imageName]['path'] . '" width="' . $images[$imageName]['width'] .'" height="' . $images[$imageName]['height'] '" />';
}

Posted: Sun Aug 07, 2005 7:16 pm
by jrucifer
thank you so much for helping me, but this is still driving me crazy...

one final question...

if i have it set up like:

Code: Select all

$images[$name]['width']
how do i set the $name through regex?

Code: Select all

$width = $images[$name]['width'] + 16; 
$height = $images[$name]['height'] + 16; 

if(isset($images[$name])) 
		   {
		   
		   
		    $body = preg_replace("|\[image=[\"\']?(.+?)[\"\']?\](.+?)\[/image\]|i","<a href=\"javascript:;\" onClick=\"image(\'/idisplay.php?name=$1\',\'$wintitle\',\'scrollbars=no,toolbar=yes,status=yes,resizable=yes,left=100,top=100,width=$width,height=$height\')\">$2</a>",$body);
		
		}
so when [image=cave]text[/image] is processed, it sets

Code: Select all

$images[$name]['height'] + 16
to

Code: Select all

$images[cave]['height'] + 16
if you can help me out i'd appreciate it. thanks.

Posted: Sun Aug 07, 2005 7:33 pm
by feyd
it'd be best to use preg_replace_callback for it.

Posted: Sun Aug 07, 2005 8:16 pm
by jrucifer
i've read the php manual for preg_replace and preg_replace_callback and i'm still confused how preg_replace_callback could be implemented here...

Posted: Sun Aug 07, 2005 8:27 pm
by feyd
create a function that will do the replacement. Place the name of this function in a string and pass it as the second argument to preg_replace_callback()

This function will be called for each match preg_replace_callback() finds with an array of the matches within that single match. You can pull the image list into that function using the global keyword.

Posted: Sun Aug 07, 2005 11:10 pm
by jrucifer
sorry, you'll have to understand i'm very new to php and programing languages all together. it's very hard for me to understand without some sort of example to go along with. if you could just bear with me i'd appreciate it, i'm trying my hardest to learn. thanks.

Posted: Sun Aug 07, 2005 11:48 pm
by feyd
basic idea:

Code: Select all

function someFunction($matches)
{
  global $images;
  print_r($matches);
  $imageName = $matches[1];
  if(isset($images[$imageName]))
  {
    return '<img src="' . $images[$imageName]['path'] . '" width="' . $images[$imageName]['width'] . '" height="' . $images[$imageName]['height'] . '" />';
  }
  else
  {
    return $matches[0]; // image not found, don't process it.
  }
}

$body = $preg_replace_callback("|\[image=[\"\']?(.+?)[\"\']?\](.+?)\[/image\]|i",'someFunction',$body);

Posted: Thu Aug 11, 2005 2:52 pm
by jrucifer
thank you so much. after toying with it for a few more hours, i finally got it to work how i wanted. thanks again for helping me solve my problem.