user3062701
user3062701

Reputation: 31

calling php function inside another

I would like to call a function inside another function in order to crop all the image files in a folder. Can you please help me? I am new to PHP, and I cannot figure out how to do it properly.

<?php

$dir = "PATH TO DIRECTORY";
$exclude = array("jpg", "jpeg", "png", "gif");
if (is_dir($dir)) {
    $files = scandir($dir);
    foreach($files as $file){
        if(!in_array($file,$exclude)){
            // DO SOMETHING
        }
    }
}

?>

Image cropping function to be called inside the function above found below

    function PIPHP_ImageCrop($image, $x, $y, $w, $h)
{
      $tw = imagesx($image);
      $th = imagesy($image);
      if ($x > $tw || $y > $th || $w > $tw || $h > $th)
                  return FALSE;
      $temp = imagecreatetruecolor($w, $h);
      imagecopyresampled($temp, $image, 0, 0, $x, $y, 
                  $w, $h, $w, $h);
       return $temp;
}

Upvotes: 1

Views: 228

Answers (2)

kumar
kumar

Reputation: 47

$dir = "PATH TO DIRECTORY";
$exclude = array("jpg", "jpeg", "png", "gif");
if (is_dir($dir)) {
$files = scandir($dir);

   foreach($files as $file){
     $ext = explode(".", $file);
     if(!in_array($ext[1],$exclude)){
        PIPHP_ImageCrop($image, $x, $y, $w, $h);
     }
   }
}

Upvotes: 1

Tim B
Tim B

Reputation: 41210

To call it you just call it, exactly the same as you call any other function:

    if(!in_array($file,$exclude)){
        PIPHP_ImageCrop($image, $x, $y, $w, $h);
    }

If the function is not present in the same file then you need to include the file that defines the function.

Upvotes: 1

Related Questions