manager = new ImageManager(new Driver()); } /** * Crop an image based on the given coordinates. * * @param string $sourcePath Absolute path to the source image. * @param int $yMin Starting Y coordinate. * @param int $yMax Ending Y coordinate. * @param string $outputPath Absolute path to save the cropped image. * @return bool True on success, false on failure. */ public function cropImage(string $sourcePath, int $yMin, int $yMax, string $outputPath): bool { try { if (!file_exists($sourcePath)) { throw new \Exception("Source image not found: {$sourcePath}"); } $image = $this->manager->read($sourcePath); $width = $image->width(); $height = $image->height(); // Validate coordinates $yMin = max(0, $yMin); $yMax = min($height, $yMax); $cropHeight = $yMax - $yMin; if ($cropHeight <= 0) { throw new \Exception("Invalid crop height: {$cropHeight} (yMin: {$yMin}, yMax: {$yMax})"); } // Crop the full width of the image for the given Y range $image->crop($width, $cropHeight, 0, $yMin); $image->save($outputPath); return true; } catch (\Exception $e) { \Log::error("Image cropping failed: " . $e->getMessage()); return false; } } }