[SOLVED] problems with window sizing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

[SOLVED] problems with window sizing

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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');">
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post 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...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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'] '" />';
}
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it'd be best to use preg_replace_callback for it.
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
jrucifer
Forum Commoner
Posts: 32
Joined: Fri May 13, 2005 2:36 pm
Location: phoenix

Post 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.
Post Reply