Files
ai-stylegallery/app/Http/Controllers/PrintController.php

54 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class PrintController extends Controller
{
public function printImage(Request $request)
{
$request->validate([
'image_path' => 'required|string',
'quantity' => 'required|integer|min:1',
]);
$imagePath = public_path(str_replace(url('/'), '', $request->input('image_path')));
$quantity = $request->input('quantity');
if (!file_exists($imagePath)) {
Log::error("PrintController: Image file not found at {$imagePath}");
return response()->json(['error' => 'Image file not found.'], 404);
}
// IMPORTANT: Replace this command with one that works in your environment.
// Examples:
// Linux/macOS: $command = ['lpr', '-#', $quantity, $imagePath];
// Windows (assuming a shared printer named 'MyNetworkPrinter'):
// $command = ['print', '/d:\\MyNetworkPrinter', $imagePath];
// You might need to install additional software or configure your system
// to enable command-line printing.
// For a more robust solution, consider a dedicated print server application
// or a commercial print API.
$command = ['echo', "Simulating print of {$quantity} copies of {$imagePath}"]; // Placeholder
try {
$process = new Process($command);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
Log::info("PrintController: Successfully sent print command for {$imagePath} (x{$quantity})");
return response()->json(['message' => 'Print command sent successfully.']);
} catch (ProcessFailedException $exception) {
Log::error("PrintController: Print command failed. Error: " . $exception->getMessage());
return response()->json(['error' => 'Failed to send print command.', 'details' => $exception->getMessage()], 500);
}
}
}