Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It's not possible to generate ImageMeta for images from other sources than images from public directory #51

Open
dejagersh opened this issue Oct 31, 2023 · 2 comments

Comments

@dejagersh
Copy link
Contributor

dejagersh commented Oct 31, 2023

I am trying to generate ImageMeta for a Post model, which has an image stored in S3. When this image is uploaded, I calculate their dimensions, write that information to my model, and then write the image to S3. ImageMeta only has a constructor for constructing from a public path, which I don't have.

I am currently working around this like this:

$imageMeta = new ImageMeta('images/logo-small.png');

$imageMeta->width = $post->image_width;
$imageMeta->height = $post->image_height;

Which isn't optimal... Is there a different way I should be doing this? Or is a better approach not possible right now? 😬

I have some ideas for improving ImageMeta, but it's a breaking change. Something like this would be much more flexible I think:

readonly class ImageMeta
{
    public function __construct(public int $width, public int $height)
    {
    }

    public static function forPublicImage(string $path): self
    {
        $publicPath = public_path($path);

        if (!is_file($publicPath)) {
            throw new Exception("Path {$publicPath} is not a file.");
        }

        [$width, $height] = getimagesize($publicPath);

        return new self($width, $height);
    }

    public static function forImageFromDisk(Filesystem $filesystem, string $path) {
        stream_copy_to_stream(
            $filesystem->readStream($path),
            $tmpFile = tmpfile()
        );

        [$width, $height] = getimagesize(stream_get_meta_data($tmpFile)['uri']);

        return new self($width, $height);
    }
}
@bardolf69
Copy link

Agreed this would be great. I'm in the same boat as you using remote image storage not a public_path.

@ralphjsmit
Copy link
Owner

Hello guys, thank you, I'm definitely open to a change to improve this (preferably in some way without a BC).
What you can always do in the meantime is just create a new class and extend the original ImageMeta class.

That you can make your own e.g. RemoteImageMeta:

class RemoteImageMeta extends ImageMeta
{
    public function __construct(string $path)
    {
        // Own logic to determine width and height, then set the properties defined in the parent `ImageMeta`.
        $this->width = $width;
        $this->height = $height;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants