How can classes be used to encapsulate image storage and retrieval logic in PHP for easier modification in the future?

When dealing with image storage and retrieval logic in PHP, it is beneficial to encapsulate this functionality within a class. By doing so, you can easily modify the code in the future without affecting other parts of your application. This also promotes code reusability and maintainability.

class ImageStorage {
    private $imageDirectory;

    public function __construct($imageDirectory) {
        $this->imageDirectory = $imageDirectory;
    }

    public function storeImage($image, $imageName) {
        $imagePath = $this->imageDirectory . '/' . $imageName;
        move_uploaded_file($image, $imagePath);
    }

    public function retrieveImage($imageName) {
        $imagePath = $this->imageDirectory . '/' . $imageName;
        return file_get_contents($imagePath);
    }
}

// Example of how to use the ImageStorage class
$imageStorage = new ImageStorage('images');
$imageStorage->storeImage($_FILES['image']['tmp_name'], $_FILES['image']['name']);
$imageData = $imageStorage->retrieveImage('example.jpg');