0

I wrote some code to upload an image to a server. Now I wanted to add a function "imageresize" wich resizes the image to a predefinded height and width. When I run it I get a fatal error

what I've tried: I found a simple piece of code on the internet witch works and tried the same stucture in my script


<?php

function voegGebruikerToe ($naam, $Ww, $WwConfirm, $sqlInsert, $nameImage, $naamDiv) {


    if(!empty($naam) and !empty($Ww) and !empty($WwConfirm)){
        if ($Ww == $WwConfirm) {

            $passHash = password_hash($Ww, PASSWORD_DEFAULT); // Creates a password hash

            $sql1 = "INSERT INTO users (username, password) VALUES ('$naam', '$passHash')";        
            if ($sqlInsert->query($sql1) === TRUE) {            //insert invevoerde data to database
                echo "ok";              
            } else {

            }


        } else {
            echo "your passwords doesn't match";
        }
    } else{
        echo "Please enter all the fields.";
    }

    $file = $_FILES[$naamDiv]['tmp_name']; 
    $sourceProperties = getimagesize($file);
    $fileNewName = $nameImage;
    $folderPath = "/var/www/";
    $ext = pathinfo($_FILES[$naamDiv]['name'], PATHINFO_EXTENSION);
    $imageType = $sourceProperties[2];
    $imageFileType = strtolower(pathinfo($_FILES[$naamDiv]['name'],PATHINFO_EXTENSION));


            // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {

        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed. <br>";

    } else {

        switch ($imageType) {


            case IMAGETYPE_PNG:
                $imageResourceId = imagecreatefrompng($file); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagepng($targetLayer,$folderPath. $fileNewName. ".". $ext);*/

                break;


            case IMAGETYPE_JPEG:
                $imageResourceId = imagecreatefromjpeg($file); 
                $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
                imagejpeg($targetLayer,$folderPath. $fileNewName. ".". $ext);*/
                echo "works";
                break;


            default:
                echo "Invalid Image type.";
                exit;
                break;
        }


        //move_uploaded_file($file, $folderPath. $fileNewName. ".". $ext);
        echo "Image Resize Successfully.";
    }


    function imageResize($imageResourceId,$width,$height) {

        $targetWidth =200;
        $targetHeight =200;


        $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
        imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);


        return $targetLayer;
    }   
}

voegGebruikerToe($admNaam, $admWw, $admWwConfirm, $conn, "administrator", "ImageUser1");

?>


the example code I found:

<?php

function x ($y) {


    function y ($z) {
        return ($z*2);
    }

    return($y+3);
}

$y = 4;
$y = x($y)*y($y);
echo $y;

?>

Fatal error: Uncaught Error: Call to undefined function imageResize() in /var/www/html/v0.2_webpagina_barchart (setupPage)/php/setUpPHP.php:75 Stack trace: #0 /var/www/html/v0.29_webpagina/php/setUpPHP.php(146): voegGebruikerToe('t', 't', 't', Object(mysqli), 'administrator', 'ImageUser1') #1 /var/www/html/v0.2_webpagina /setUp.php(4): include('/var/www/html/v...') #2 {main} thrown in /var/www/html/v0.2_webpagina/php/setUpPHP.php on line 75

  • Although you *can* nest functions like that, there is no good reason to do so. See the dupe – Nick Aug 28 '19 at 04:50
  • Nested functions are not "hoisted" so PHP doesn't recognise `imageResize` where you call it. But if you declared it at the beginning of the function, it would find it ok. However the second time you call the outer function your code will crash with a "Cannot redeclare imageResize" error – Nick Aug 28 '19 at 04:53
  • thanks for your reaction! I will defenitly try that. Can you explain in short where to define it better, and why I cant call this funtion when twice if I place this function in the beginning? –  Aug 28 '19 at 06:30
  • It would be best to define the function at the same level as you define your other functions. That will resolve all the problems you are seeing. – Nick Aug 28 '19 at 07:08

0 Answers0