<?php

namespace dynamicimages\classes;

use dynamicimages\interfaces\dynamicimages_asset_i;
use dynamicimages\traits\dynamicimages_asset_t;

class dynamicimages_asset implements dynamicimages_asset_i
{
    use dynamicimages_asset_t;
    /***
     * @example $asset = new dynamicimages_asset('/path/to/image.jpg');
     * @var string $path The path to the dynamic image asset
     */
    public string $path;
    /**
     * The pixel width of the image
     * @var int|null $width
     */
    private ?int $width = null;
    /**
     * The pixel height of the image
     * @var int|null $height
     */
    private ?int $height = null;
    /**
     * @param string $path
     * @throws \Exception
     */
    public function __construct(string $path)
    {
        $this->path = WD . '/' . $path;
        $this->initialize();
    }
    /**
     * @inheritDoc
     */
    public function initialize(): void
    {
        if (!file_exists($this->path)) {
            throw new \Exception("Dynamic image asset not found: {$this->path}");
        }
        list($this->width, $this->height) = getimagesize($this->path);
    }

    public function getPath(): string
    {
        return $this->path;
    }

    public function getWidth(): ?int
    {
        return $this->width;
    }
    public function getHeight(): ?int
    {
        return $this->height;
    }
}