What potential issues can arise when using PHP to manipulate and display images on a website?

One potential issue that can arise when using PHP to manipulate and display images on a website is insecure file uploads, which can lead to security vulnerabilities such as allowing malicious files to be uploaded and executed on the server. To solve this issue, it is important to validate file types, restrict file sizes, and store uploaded files in a secure directory outside of the web root.

// Example code to validate file type and restrict file size before uploading
$allowedTypes = ['image/jpeg', 'image/png'];
$maxSize = 5242880; // 5MB

if ($_FILES['image']['error'] === UPLOAD_ERR_OK) {
    if (in_array($_FILES['image']['type'], $allowedTypes) && $_FILES['image']['size'] <= $maxSize) {
        $uploadPath = '/path/to/upload/directory/' . $_FILES['image']['name'];
        move_uploaded_file($_FILES['image']['tmp_name'], $uploadPath);
        echo 'Image uploaded successfully!';
    } else {
        echo 'Invalid file type or file size too large.';
    }
}