php-imagine / Imagine

PHP Object Oriented image manipulation library
https://imagine.readthedocs.io
Other
4.42k stars 530 forks source link

Issue reading GDImage object using \Imagine\Gd\Imagine read() method #834

Closed jcogs-design closed 1 year ago

jcogs-design commented 1 year ago

Issue description

When trying to read an existing GDImage object into an Imagine\Gd\Imagine object I get an exception Variable does not contain a stream resource

Unclear from manual whether there is a way / how to insert a GDImage object into an Imagine object.

What version of Imagine are you using?

1.3.2

What's the PHP version you are using?

8.0.25

What's the imaging library you are using [gd/imagick/gmagick/any]?

GD2

What's the imaging library configuration

GD Version: "2.3.3" FreeType Support: true FreeType Linkage: "with freetype" GIF Read Support: true GIF Create Support: true JPEG Support: true PNG Support: true WBMP Support: true XPM Support: true XBM Support: true WebP Support: true BMP Support: true TGA Read Support: true JIS-mapped Japanese Font Support: false

Minimal PHP code to reproduce the error:


$gd_resource = imagecreatefromstring(file_get_contents($path)); // in php 8 creates a GDImage object
$imagine_object = new Imagine\Gd\Imagine(); // Creates new Imagine GD object
$imagine_object->read($gd_resource); // Throws an error 'Variable does not contain a stream resource'
ausi commented 1 year ago

Imagine\Gd\Imagine::read() can only handle resources that point to files, not GD resource objects.
So your code should probably look like this:

$file_contents = file_get_contents($path);
$imagine_object = new Imagine\Gd\Imagine();
$imagine_object->load($file_contents);

Or if you need to handle existing GD resources you could use the class factory to create an image from that:

$gd_resource = imagecreatefromstring(file_get_contents($path)); // in php 8 creates a GDImage object
(new Imagine\Factory\ClassFactory())->createImage(
    Imagine\Factory\ClassFactoryInterface::HANDLE_GD, 
    $gd_resource, 
    new Imagine\Image\Palette\RGB(), 
    new Imagine\Image\Metadata\MetadataBag(),
);
jcogs-design commented 1 year ago

Hi - that's super helpful thanks. Am just getting head around Imagine and finding the documentation quite terse: great as a reference for specific methods, but not so useful for getting a handle on approach used by library. Such illustrative information as I have found so far dates from 2011, and not useful for current version of library. Do you know if there are good sources of information on use of the library beyond the docs? Thanks!

ausi commented 1 year ago

Do you know if there are good sources of information on use of the library beyond the docs?

I don’t know. I usually read the source code directly to find out how it is meant to be used. ☺️