Page 1 of 1

Function not returning a value in php 5

Posted: Wed Dec 13, 2006 1:47 pm
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?

Posted: Wed Dec 13, 2006 1:54 pm
by John Cartwright
what does a var_dump() yield both inside and outside the function?

Posted: Wed Dec 13, 2006 2:02 pm
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.

Posted: Wed Dec 13, 2006 4:47 pm
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

Posted: Wed Dec 13, 2006 7:47 pm
by RobertGonzalez
So I take it you got it working?

Posted: Thu Dec 14, 2006 10:03 am
by Charles256
nope.that last null means it isn't working :-D

Posted: Thu Dec 14, 2006 10:07 am
by feyd
Add this at the beginning of your function please

Code: Select all

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