Jump to content

Recommended Posts

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";
}

Link to comment
https://forums.phpfreaks.com/topic/155196-cannot-redeclare-resize-error/
Share on other sites

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";
}
}
}

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.

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?

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');

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.