acctman Posted April 22, 2009 Share Posted April 22, 2009 why am i getting this error on my function? Fatal error: Cannot redeclare resize() (previously declared in /copyimg.php:144) in /copyimg.php on line 144 function resize($src,$dest) { $source_pic = ($src); $destination_pic = ($dest); $max_width = 500; $max_height = 600; $msrc = imagecreatefromjpeg($source_pic); list($width,$height) = getimagesize($source_pic); $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if(($width <= $max_width) && ($height <= $max_height)){ $tn_width = $width; $tn_height = $height; } elseif (($x_ratio * $height) < $max_height){ $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $tmp = imagecreatetruecolor($tn_width,$tn_height); imagecopyresampled($tmp,$msrc,0,0,0,0,$tn_width, $tn_height,$width,$height); imagejpeg($tmp,$destination_pic,90); imagedestroy($msrc); imagedestroy($tmp); } list($width,$height) = getimagesize($src); if(file_exists($src)) { if ($width > 500) { resize($src,$dest); echo "Copied Image: $imagename\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/ Share on other sites More sharing options...
JonnoTheDev Posted April 22, 2009 Share Posted April 22, 2009 Because you have 2 functions with the same name or you are including the same file more than once on your script. Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/#findComment-816425 Share on other sites More sharing options...
acctman Posted April 22, 2009 Author Share Posted April 22, 2009 Because you have 2 functions with the same name or you are including the same file more than once on your script. is there anyway around this, i'm using the if statement below. if(file_exists($src)) { if ($width > 500) { resize($src,$dest); echo "Copied Image: $imagename\n"; } else { if(copy($src, $dest)) { echo "Copied Image: $imagename\n"; } else { echo "Image Copy Failed: $imagename\n"; } } } else if(!file_exists($src)) { if ($width > 500) { resize($oldsrc,$dest); echo "Old Copied Image: $imagename\n"; } else { if(copy($oldsrc, $dest)) { echo "Old Copied Image: $imagename\n"; } else { echo "Old Image Copy Failed: $imagename\n"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/#findComment-816451 Share on other sites More sharing options...
JonnoTheDev Posted April 22, 2009 Share Posted April 22, 2009 The script you have posted has nothing to do with the error. This script calls resize() but does not declare it! In the file with the error you must be including i.e. include('functions.php'); more than once or you have the following declared more than once: function resize() { } Do a scan of your web files for 'function resize' and see how many times you find it. You can only have 1 function with a particular name. Also if this is not the case then change any include() to use include_once() or require_once() for the errornous script. Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/#findComment-816457 Share on other sites More sharing options...
acctman Posted April 22, 2009 Author Share Posted April 22, 2009 The script you have posted has nothing to do with the error. This script calls resize() but does not declare it! In the file with the error you must be including i.e. include('functions.php'); more than once or you have the following declared more than once: function resize() { } Do a scan of your web files for 'function resize' and see how many times you find it. You can only have 1 function with a particular name. Also if this is not the case then change any include() to use include_once() or require_once() for the errornous script. so i can't create a function at the top of the script and then later execute it at the bottom? the function has to be in a separate file and then included? Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/#findComment-816491 Share on other sites More sharing options...
JonnoTheDev Posted April 22, 2009 Share Posted April 22, 2009 so i can't create a function at the top of the script and then later execute it at the bottom? the function has to be in a separate file and then included? You can declare a function anywhere you just can't have 2 functions with the same name! This is your error. i.e. // function-file1.php function xyz() { } // function-file2.php function xyz() { } // index.php // this will throw an error as i am including 2 files with the same function include('function-file1.php'); include('function-file2.php'); Or you are including the same file more than once // index.php // this will throw an error as i am including the same file twice include('function-file1.php'); include('function-file1.php'); Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/#findComment-816494 Share on other sites More sharing options...
Daniel0 Posted April 22, 2009 Share Posted April 22, 2009 It's because he is including it multiple times. The error message says that (the filenames and line numbers are identical). Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/#findComment-816496 Share on other sites More sharing options...
premiso Posted April 22, 2009 Share Posted April 22, 2009 You just cannot have a re-declaration of the function. If that page is included twice, and needs to be included twice on the same page, yes. It would be better to create a functions file and include that once at the top of the page so that function is accessible. Alternatively you can define the function declaration inside of an function_exists if. Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/#findComment-816498 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2009 Share Posted April 22, 2009 Or its more likely that the posted code is part of a loop that is resizing multiple files and the function definition is inside of the loop, instead of being before the loop. Post the whole file to get the quickest solution instead of a game of 20 guesses to find out what you are actually doing in the code. Quote Link to comment https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/#findComment-816635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.