Image Size Problem In My GuestBook

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
angus
Forum Newbie
Posts: 10
Joined: Sat Sep 17, 2005 5:43 pm

Image Size Problem In My GuestBook

Post by angus »

OK in my huest book I have a slight problem.

http://www.saxonian.co.uk/index.php?option=guest

Not the image obviously, but the size of it. I did have a code to find all of the image paths I think using preg_match and then can resize it that way. But since cassandra fell, and then I had an old backup, I've seemed to have lost that, and My last backup was either to early (before code was implemented or too late (i.e. backup after the down time).

This is my code for filling the guestbook

Code: Select all

//
// Get GuestBook
//

function get_guest_blog()
{

	global $guesttable;

	$sql = "SELECT * FROM " . $guesttable . " ORDER BY guest_date DESC, guest_time DESC";

	$result = mysql_query($sql);

	if ($result === false)
	{
		header("Location: index.php?error=2");
	}

	$nrows = mysql_num_rows($result);

		print "<div align=\"center\"><a href=\"./index.php?option=guest&action=add\">Add An Entry</a></div><br /><br />";

	if ($nrows == 0)
	{
		print "No Guest Entries Found<br />";
	} else {
		while ($row = mysql_fetch_array($result)) {
			$text = $row['guest_text'];
			$text = preg_replace("/\[img\](.*)\[\/img\]/i",  "<img border=\"0\" src=\"$1\" />", $text);
			$text = preg_replace("/\[center\](.*)\[\/center\]/i", "<center>$1</center>", $text);
			$text = preg_replace("/\[url\](.*)\[\/url\]/i", "<a href=\"$1\">$1</a>", $text);
			$text = preg_replace("/\[url=(.*)\](.*)\[\/url\]/i", "<a target=\"_blank\" href=\"$1\">$2</a>", $text);

			print "<div class=\"divBox\">\n";
			print "<b>" . $row['guest_name'] . "</b><br />" . $row['guest_date'] . " - " . $row['guest_time'] . "<br /><br />\n";
			print "<b>" . $row['guest_title'] . "</b><br /><br />\n";
			print nl2br($text) . "\n";
			print "</div>\n";
		}

			print "<br /><div align=\"center\"><a href=\"./index.php?option=guest&action=add\">Add An Entry</a></div>";
	}
}
It does what its meant to, I just need to add something to resize the images.

So basically I need the code to collect all of the image paths from the BBCodes, use getimagesize and then use an if, saying

if width > #pixels then

echo "<img size=" 573 px " src="image path">

or

echo "<img src="image path">

Thats about it.

Any help is apprieciated


John
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

All of your images should be in the same directory, correct? Or you should know where they are at on your server. So filling the getimagesize() shouldn't be hard.

Code: Select all

$size = getimagesize("/home/user/public_html/pictures/$somepic");
$width = $getimagesize[0];
$height = $getimagesize[1];

if($width > 100)
{
   echo "<img src=\"$somepic\" width=\"100\">";
} ELSE
{
   echo "<img src=\"$somepic\">";
}
Of course, you should never use HTML to resize images.. you should create a thumbnail instead.
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.
angus
Forum Newbie
Posts: 10
Joined: Sat Sep 17, 2005 5:43 pm

Post by angus »

Actually these are linked to, like image tags in forums.

[ img ] path [ / img]

And since I'm sorting them out in a preg_replace, to sort them out when collecting them from the database. SO the database will have the tags in place, and when they come out, it will show them from a remote location (path in this case), and determine the width, it needs to be done in the while bit, but I need to find the, sizes first ... this might be going in circles :roll:

Is there anyway of using a function in a preg_replace? like

Code: Select all

preg_replace("/\[img\](.*)\[\/img\]/i",  "<img border=\"0\" width=\"" . image_size($1) . "\" src=\"$1\" />", $text);
I have tried this but it give me this (its just an example) I get this error.
Parse error: parse error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/jsaxon/public_html/includes/function_body.php on line 194
SO on the other hand how would I create a thumbnail? And whats wrong with html resizing?


Thanks
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Your error likely means that you are missing an ending } bracket somewhere. I am not good enough with regex's to provide you with one that would match an [ img ] [ /img ] scenario. :(

There are plenty of thumbnail scripts available on google, hotscripts, planet-source-code, etc. The disadvantage of HTML resizing is that the full picture still loads, regardless if it is 1 px tall and 1px wide. If the original picture is say 400kb in size, then the smaller version (no matter how small) will still be 400kb in size, and will take just as long as the original picture to load. A good quality thumbnail 100 x 100 pixels wide, would be around 4kb.
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 »

the error is due to $1 which is an illegal variable name outside of regular expression result patterns. The code you have will run the function before running the regular expression. Use preg_replace_callback().
angus
Forum Newbie
Posts: 10
Joined: Sat Sep 17, 2005 5:43 pm

Post by angus »

That worked brilliantly thanks mate, as for the thumbnail, I'll sort that out another time, as I'd like to get the admincp sorted first :).

Final Code

Code: Select all

//
// Get GuestBook
//

function get_guest_blog()
{

	global $guesttable;

	$sql = "SELECT * FROM " . $guesttable . " ORDER BY guest_date DESC, guest_time DESC";

	$result = mysql_query($sql);

	if ($result === false)
	{
		header("Location: index.php?error=2");
	}

	$nrows = mysql_num_rows($result);

		print "<div align=\"center\"><a href=\"./index.php?option=guest&action=add\">Add An Entry</a></div><br /><br />";

	if ($nrows == 0)
	{
		print "No Guest Entries Found<br />";
	} else {
		while ($row = mysql_fetch_array($result)) {
			$text = $row['guest_text'];
			$text = preg_replace_callback("/\[img\](.*)\[\/img\]/i", "image_resize", $text);
			$text = preg_replace("/\[center\](.*)\[\/center\]/i", "<center>$1</center>", $text);
			$text = preg_replace("/\[url\](.*)\[\/url\]/i", "<a href=\"$1\">$1</a>", $text);
			$text = preg_replace("/\[url=(.*)\](.*)\[\/url\]/i", "<a target=\"_blank\" href=\"$1\">$2</a>", $text);

			print "<div class=\"divBox\">\n";
			print "<b>" . $row['guest_name'] . "</b><br />" . $row['guest_date'] . " - " . $row['guest_time'] . "<br /><br />\n";
			print "<b>" . $row['guest_title'] . "</b><br /><br />\n";
			print nl2br($text) . "\n";
			print "</div>\n";
		}

			print "<br /><div align=\"center\"><a href=\"./index.php?option=guest&action=add\">Add An Entry</a></div>";
	}
}

//
// Image Resize Function
//

function image_resize($matches)
{

list($width, $height, $type, $attr) = getimagesize($matches[1]);
if ($width > 560)
{
	$image = '<img src="' . $matches[1] . '" width="552" alt="Saxonian" />';
	return ($image);
} else {
	$image = '<img src="' . $matches[1] . '" width="' . $width . '" alt="Saxonian" />';
	return ($image);
}

}
Post Reply