Function not called?

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
dukebd711
Forum Newbie
Posts: 3
Joined: Tue Oct 31, 2006 7:45 pm

Function not called?

Post by dukebd711 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is a image resizing page.  It allows users to upload images and then when images are displayed they are resized into a thumbnail.  The function imageResize returns width and height.  
"Action2" is called and should then call imageResize further into the function to get the new height and width yet it does not.  I downloaded a debugger and have check my syntax and am just not able to figure out why it rolls over the function call ([color=red]no longer highlighted in red below[/color]).
Any help would be very much appreciated.
Thanks.
Charlie

Code: Select all

<?
function imageResize($width, $height, $target)
{ 
	echo "IN imageResize";
	
	if ($width > $height) 
	{ 
		$percentage = ($target / $width); 
	}
	
	else
	{
		$percentage = ($target / $height); 
	}
	
	echo "In Deep";
	$width = round($width * $percentage); 
	$height = round($height * $percentage); 

	return "width=\"$width\" height=\"$height\""; 

}

if($_POST["action2"] == "View Images")
{
	$handle = @opendir("Images");

	if(!empty($handle))
	{
		while(false !== ($file = readdir($handle)))
		{
			if(is_file("Images/" . $file))
			{
				echo "In View Images";
				$mysock = getimagesize("Images/" . $file);
				$myMax = 60;
				echo $mysock[0];
				echo $mysock[1];
				echo '<a href="Images/' . $file . '"><img src="Images/' . $file . '" <?php imageResize($mysock[0], $mysock[1], $myMax); ?>></a>';
			}
		}
	}
	
	closedir($handle);
}


if($_POST["action"] == "Upload Image")
{
	unset($imagename);

	if(!isset($_FILES) && isset($HTTP_POST_FILES))
	{
		$_FILES = $HTTP_POST_FILES;
	}

	if(!isset($_FILES['image_file']))
	{
		$error["image_file"] = "An image was not found.";
	}
	
	$imagename = basename($_FILES['image_file']['name']);
	//echo $imagename;

	if(empty($imagename))
	{
		$error["imagename"] = "The name of the image was not found.";
	}

	if(empty($error))
	{
		$newimage = "Images/" . $imagename;
		//echo $newimage;
		$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
		chmod("Images/$imagename", 0777);
		
		if(empty($result))
		{
			$error["result"] = "There was an error moving the uploaded file.";
		}
	}

}

include("upload_form.php");

if(is_array($error))
{
	while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}


?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your function call is inside a PHP level string, therefore doesn't happen.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

echo '<a href="Images/' . $file . '"><img src="Images/' . $file . '"'; imageResize($mysock[0], $mysock[1], $myMax); echo '></a>';
No need for php tags here (your already in php mode.
dukebd711
Forum Newbie
Posts: 3
Joined: Tue Oct 31, 2006 7:45 pm

Post by dukebd711 »

Sorry about not using the php tags, will do in the future :D

Thanks for the help. Now when I run this code it goes into the imageResize function but the image does not change size. The function seems to work fine and it returns the new width and height but does not affect the picture size. Am I going about this the wrong way? Should the return change the size of the picture?
Thanks again for any help

8)

Code: Select all

<?
function imageResize($width, $height, $target)
{ 
		
	if ($width > $height) 
	{ 
		$percentage = ($target / $width); 
	}
	
	else
	{
		$percentage = ($target / $height); 
	}
	
	$width = round($width * $percentage); 
	$height = round($height * $percentage); 

	return "width=\"$width\" height=\"$height\""; 

}

if($_POST["action2"] == "View Images")
{
	$handle = @opendir("Images");

	if(!empty($handle))
	{
		while(false !== ($file = readdir($handle)))
		{
			if(is_file("Images/" . $file))
			{
				echo "in View Images";
				$mysock = getimagesize("Images/" . $file);
				echo '<a href="Images/' . $file . '"><img src="Images/' . $file . '"'; imageResize($mysock[0], $mysock[1], $myMax); echo '></a>';
       		}
		}
	}
	
	closedir($handle);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

imageResize() returns a string. Your code would expect it to echo.
dukebd711
Forum Newbie
Posts: 3
Joined: Tue Oct 31, 2006 7:45 pm

Post by dukebd711 »

feyd wrote:imageResize() returns a string. Your code would expect it to echo.
Thanks feyd.
I messed around for way too long trying to echo it into the <img src> tag and was not successful so I took a different approach. I am passing the name of the file to my imageResize function and echoing the img tag from this method also. The problem now is that all of my pictures are the size of a period...? I changed my target size to 15000 and the size of the image did not change. I echoed the width and height that the images should be and they were equal to my target yet the pictures were still tiny.
Any help would be awesome, thanks.

Code: Select all

<?
function imageResize($width, $height, $target, $image)
{ 
	if ($width > $height) 
	{ 
		$percentage = ($target / $width); 
	}
	
	else
	{
		$percentage = ($target / $height); 
	}
	
	$width = round($width * $percentage); 
	$height = round($height * $percentage); 
    
    echo '<img src="Images/' . $image . '" width="$width" height="$height">';

}
Post Reply