What are the potential pitfalls of using exec() in PHP to run external commands like ImageMagick?

Using exec() in PHP to run external commands like ImageMagick can pose security risks if not properly sanitized. It opens the door to command injection attacks where malicious users can execute arbitrary commands on the server. To mitigate this risk, always validate and sanitize user input before passing it to exec().

$filename = escapeshellarg($_POST['filename']);
$command = "convert $filename -resize 200x200 thumbnail.jpg";
exec($command);