Are there any best practices or guidelines for managing image linking and embedding in PHP to maintain control over website content?

When managing image linking and embedding in PHP, it's important to sanitize user input to prevent security vulnerabilities such as cross-site scripting attacks. One way to do this is by validating the image URLs before displaying them on the website. Additionally, storing images in a secure directory and restricting access to them can help maintain control over website content.

// Validate and sanitize image URL before displaying
$imageUrl = filter_var($_POST['image_url'], FILTER_VALIDATE_URL);

// Store images in a secure directory
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['image']['name']);

if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadFile)) {
    echo "Image uploaded successfully.";
} else {
    echo "Failed to upload image.";
}

// Restrict access to images
header("Content-Type: image/jpeg");
readfile($uploadFile);