Search code examples
phpvariablesgdphp-gd

Can't use variable outside function even after setting it global


I have made a script to generate thumbnails using PHP's GD library. The thing is that the original images are in their respective directories that are assigned to 'gallery/'.$section;and the thumbs will go in the directory 'gallery/thumbs/'.$section

Now, every time the script is executed i want it to check if the thumbnail already exist and if not only then create it but whenever i call the function and reload the page it creates new thumbnail every time.

I know it is about the scope of the variable inside and outside the function but what it is I can't figure out. I've tried setting the variable as global but still it is not working out.

Here is the pre-declaration. The idir is for images directory and the tdiris for thumbs directory.

<?php
$idir = "gallery/".$section.'/';
$tdir="gallery/thumbs/".$section.'/';
if(!file_exists($tdir)){
mkdir($tdir);
}

Here is the function i am using.

function createThumbs($idir, $tdir, $tw, $th){
    $dir=opendir($idir);
    while(($fname = readdir($dir)) != false){
        if($fname!='.' && $fname != '..'){
        $img = imagecreatefromjpeg($idir.$fname);

        $width = imagesx($img);
        $height = imagesy($img);

        if($width>$height){
            $new_width=$tw;
            $new_height=$height*($tw/$width);
        }
        if ($width < $height) 
        {
            $new_width=$width*($th/$height);
            $new_height=$th;
        }
        if ($width == $height) 
        {
            $new_width=$tw;
            $new_height=$th;
        }


        $tmp_img = imagecreatetruecolor($new_width, $new_height);
        imagecopyresampled($tmp_img, $img, 0,0,0,0, $new_width, $new_height, $width, $height);

        imagejpeg($tmp_img, $tdir.$fname, 100);

        imagedestroy($tmp_img); 
        imagedestroy($img); 
    }
}
closedir($dir); 
}

And after this i am calling the function like that.

if (!file_exists($tdir.$fname)) {
    createThumbs($idir,$tdir,250, 200);
}

Also when calling the function it gives an error - undefined variable .$fname.


Solution

  • You intialize the value for $fname inside the function, then the first time you try to get $fname value it is never been initialized, thus the error you get.

    It could help you move the if statement inside the while loop:

    function createThumbs($idir, $tdir, $tw, $th){
    
        $dir=opendir($idir);
        while(($fname = readdir($dir)) != false){
            if($fname!='.' && $fname != '..' && !file_exists($tdir.$fname)){
            $img = imagecreatefromjpeg($idir.$fname);
    
            $width = imagesx($img);
            $height = imagesy($img);
    
            if($width>$height){
                $new_width=$tw;
                $new_height=$height*($tw/$width);
            }
            if ($width < $height) 
            {
                $new_width=$width*($th/$height);
                $new_height=$th;
            }
            if ($width == $height) 
            {
                $new_width=$tw;
                $new_height=$th;
            }
    
    
            $tmp_img = imagecreatetruecolor($new_width, $new_height);
            imagecopyresampled($tmp_img, $img, 0,0,0,0, $new_width, $new_height, $width, $height);
    
            imagejpeg($tmp_img, $tdir.$fname, 100);
    
            imagedestroy($tmp_img); 
            imagedestroy($img); 
            }
        }
        closedir($dir); 
    }
    

    And then always call thumbnail creation:

    //if (!file_exists($tdir.$fname)) {
        createThumbs($idir,$tdir,250, 200);
    //}