Image Process is stopping in between

Hi, I have written a code to crop the images. Its working perfectly well. Then I tried to loop the process to read images inside more folders. But when I tried to loop the same, the process is breaking after completing few folders. The stopping is not in a specified folder. I will paste the code.

<?php
/*Reading POST data from FORM.PHP*/
$constNo=$_POST["const"];
$boothfrom=$_POST["from"];
$boothto=$_POST["to"];
/*End Reading POST data from FORM.PHP*/

mkdir($constNo);
//$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
for ($bFrom=$boothfrom;$bFrom<=$boothto;$bFrom++)
{
	$currentDir=getcwd();
	chdir($constNo);
	mkdir($bFrom);
	chdir($currentDir);
	$k=1;
	$sourceDir = '/S11A'.$constNo.'P'.$bFrom;
	$directory = getcwd().'/'.$sourceDir.'/';
	$filecount = 0;
 
	$files2 = glob( $directory ."*.*" );
 
	if( $files2 ) 
	{
		$endFile = count($files2);
	}	
	for ($m=3;$m<=$endFile-1;$m++)
	{
		$fileName='S11A'.$constNo.'P'.$bFrom;
		if ($m>=10)
			$inputfile=$currentDir.'/'.$fileName.'/'.$fileName.'-page-000'.$m.'.jpg';
		else
			$inputfile=$currentDir.'/'.$fileName.'/'.$fileName.'-page-0000'.$m.'.jpg';
		//fwrite($myfile, $inputfile."\n");

		// Create an image from given image
		if ($im = imagecreatefromjpeg($inputfile))
		{
			$x=30;
			$y=61;
			for ($i=1;$i<=10;$i++)
			{
				for ($j=1;$j<=3;$j++)
				{
					// Set the crop image size 
					$width=385;
					$height=153;
					if ($j==3)
					{
						$width=384;
					}
					if ($i==10)
					{
						$height=152;
					}

					$im2 = imagecrop($im, ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
					$x=$x+385+11.5;
					$file=$currentDir.'/'.$constNo.'/'.$bFrom.'/'.$k.".png";
					//fwrite($myfile, $file.'  ');
					if ($im2 !== FALSE) 
					{
						header("Content-type: image/png");
						//	imageresolution($im2, 150, 150);
						imagepng($im2, $file);
						imagedestroy($im2);
					}
					
					/*START IMAGE MERGE */
					$image = imagecreatefrompng($file);
					$frame = imagecreatefrompng($currentDir.'/booth.png');
					imagecopymerge($image, $frame, 300, 40, 0, 0, 69, 72, 100);
					imagepng($image, $file);
					imagedestroy($image);
					imagedestroy($frame);
					/* END Image Merge */

					/* TEXT Insertion */
					header("Content-type: image/png");
					$image = imagecreatefrompng($file);
					$color = imagecolorallocate($image, 0, 0, 0);
					$string = "1";
					$fontSize = 5;
					if ($bFrom>=100)
					{
						$x_cor=310;
					}
					else if ($bFrom>=10)
					{
						$x_cor=319;
					}
					else
					{
						$x_cor = 328;
					}
					$y_cor = 105;
					imagettftext($image, 12, 0, 12, 20, $color, './arial.ttf','Sl.No.');
					imagettftext($image, 19, 0, $x_cor, $y_cor, $color, './arialbd.ttf',$bFrom);
					imagepng($image, $file);
					imagedestroy($image);
					/* END Text Insertion */

					/*Delete Image with Blank */
					$fileSize = filesize($file);
					//fwrite($myfile," - File Size : ".$fileSize." \n");
					if ($fileSize <= 16000)
						unlink($file);
					else
						$k++;
				}			
				if ($i>=4)
					$y=$y+153+12.2;
				else	
					$y=$y+153+12.5;
				$x=30;
			}
			imagedestroy($im);
		}
		else
			break;
	}
clearstatcache();
}
//fclose($myfile);
?>

Just a suggestion, but using Intervention Image library (or any image library) wold make cropping so much easier.

<?php
use Intervention\Image\ImageManagerStatic as Image;

// ...

// Load the image
$image = Image::make($image_path);

// Resize the image
$image->resize(600, 400, function ($constraint) {
    $constraint->aspectRatio();
    $constraint->upsize();
});

// Crop the image (Assuming you want to crop a 300x300 square from the center)
$image->crop(300, 300, null, null); // You can replace null, null with x and y coordinates to specify a different cropping point

// Sharpening the image
$image->sharpen(25);

// Save the new image
$image->save($image_path, 100);

// Database Table Column:
$data['image_path'] = $image_path;

Sponsor our Newsletter | Privacy Policy | Terms of Service