1

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;
}
  • Refer - http://stackoverflow.com/questions/8104998/how-can-call-function-of-one-php-file-from-another-php-file-and-pass-parameter-t – Hussain Dec 30 '13 at 13:48
  • Did you try using the function name and a pair of parentheses? Why did it fail? – John Dvorak Dec 30 '13 at 13:48

2 Answers2

1

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.

Tim B
  • 40,716
  • 16
  • 83
  • 128
1
$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);
     }
   }
}
kumar
  • 47
  • 2