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;
}
2
  • Refer - stackoverflow.com/questions/8104998/… Commented Dec 30, 2013 at 13:48
  • Did you try using the function name and a pair of parentheses? Why did it fail? Commented Dec 30, 2013 at 13:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.