What are some potential security risks associated with using shell_exec in PHP scripts for image manipulation?

Using shell_exec in PHP scripts for image manipulation can pose security risks such as command injection attacks if user input is not properly sanitized. To mitigate this risk, it is recommended to use PHP's built-in functions for image manipulation like GD or Imagick.

// Example of using GD library for image manipulation
$image = imagecreatefromjpeg('example.jpg');
$width = imagesx($image);
$height = imagesy($image);

// Resize the image
$new_width = $width * 0.5;
$new_height = $height * 0.5;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Save the resized image
imagejpeg($new_image, 'resized_example.jpg');

// Free up memory
imagedestroy($image);
imagedestroy($new_image);