Function not returning a value in php 5

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
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Function not returning a value in php 5

Post by Charles256 »

First page:

Code: Select all

$test=createThumbnail($_FILES['fleImage']['tmp_name'],$imgPath,THUMBNAIL_WIDTH,75,TRUE);
    print_r($test);
	exit();
the function

Code: Select all

function createThumbnail($srcFile, $destFile, $width, $quality = 75,$test=FALSE)
{
	$thumbnail = '';
	
	if (file_exists($srcFile)  && isset($destFile))
	{
		$size        = getimagesize($srcFile);
		$w           = number_format($width, 0, ',', '');
		$h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
		
		$thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
		
		$name=explode("/",$thumbnail);
		$size=count($name);			
		
	}
	$answer=basename($thumbnail);
	
	if (basename($thumbnail)=="")
	{
		$answer= $name[$size-1];
	}
	// return the thumbnail file name on sucess or blank on fail
	return $answer;
}
now I know thumbnail contains somethign because i put in an echo in the function and exit so I could see it. however, the function is not returning it. any theories on why?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what does a var_dump() yield both inside and outside the function?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

It appears that if your $srcFile does not exist, or $destFile is not set, then you will be calling $name[$size-1] even though $name and $size are undefined. You might want to just change your first if to return false if it fails. Even though thats not technically an error, its good programming practice to watch out for that. You should turn on notices on your dev server, it should catch alot of those things.

Secondly, to get to your question, what is the value of answer within your function? Echo that at each step and see what it is, not just $thumbnail.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

new code.

Code: Select all

function createThumbnail($srcFile, $destFile, $width, $quality = 75,$test=FALSE)
{
	$thumbnail = '';
	
	if (file_exists($srcFile)  && isset($destFile))
	{
		$size        = getimagesize($srcFile);
		$w           = number_format($width, 0, ',', '');
		$h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
		
		$thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
		
		$name=explode("/",$thumbnail);
		$size=count($name);			
		
	}
	$answer=basename($thumbnail);
	
	if (basename($thumbnail)=="")
	{
		$answer= $name[$size-1];
	}
	var_dump($answer);
	var_dump($thumbnail);
	// return the thumbnail file name on sucess or blank on fail
	return $answer;
}

Code: Select all

    $imgPath = THUMBNAIL_IMG_DIR . $image;
    $test=createThumbnail($_FILES['fleImage']['tmp_name'],$imgPath,THUMBNAIL_WIDTH,75,TRUE);
    var_dump($test);
	exit();
results
string(36) "ba22e2c3df41ebe57f29900042da46f4.jpg" string(64) "../images/gallery/thumbnail/ba22e2c3df41ebe57f29900042da46f4.jpg" NULL string(0) "" NULL
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So I take it you got it working?
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

nope.that last null means it isn't working :-D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Add this at the beginning of your function please

Code: Select all

$_ = debug_backtrace();
var_dump(array_shift($_));
Post Reply