How from Intervention Image get resource for imagepng?

Posted 1 week ago by PetroGromovo

Hello, In laravel 5.8 app I want to banner generation programmatically, like base image, image at left, text at center, watermark at right. I do it as :

        // create new Intervention Image
        $img = \Image::make( public_path('foo.jpg') );

// paste another image
        $img->insert( public_path('bar.png') );

// create a new Image instance for inserting
        $watermark = \Image::make( public_path('watermark.png') );
        $img->insert($watermark, 'center');

        // insert watermark at bottom-right corner with 10px offset
        $img->insert( public_path('watermark.png'), 'bottom-right', 10, 10);



        // create Image from file
        $img = \Image::make( public_path('foo.jpg') );

// write text
        $img->text('The quick brown fox jumps over the lazy dog.');

// write text at position
        $img->text('The quick brown fox jumps over the lazy dog.', 120, 100);

// use callback to define details
        $img->text('foo', 0, 0, function($font) {
            $font->file( public_path('fonts/themify/themify.ttf') );
            $font->size(24);
            $font->color('#fdf6e3');
            $font->align('center');
            $font->valign('top');
            $font->angle(45);
        });

//        var_dump($img);

        // draw transparent text
        $img->text('foo', 0, 0, function($font) {
            $font->color(array(255, 255, 255, 0.5));
        });

        header('Content-Type: image/png');
        imagepng($img, null, 100);
//        imagedestroy($img); //Free up memory

And I got error :

imagepng() expects parameter 1 to be resource, object given

dumping $img I got :

object(Intervention\Image\Image)#910 (9) { ["driver":protected]=> object(Intervention\Image\Gd\Driver)#907 (2) { ["decoder"]=> object(Intervention\Image\Gd\Decoder)#908 (1) { ["data":"Intervention\Image\AbstractDecoder":private]=> NULL } ["encoder"]=> object(Intervention\Image\Gd\Encoder)#905 (4) { ["result"]=> NULL ["image"]=> NULL ["format"]=> NULL ["quality"]=> NULL } } ["core":protected]=> resource(45) of type (gd) ["backups":protected]=> array(0) { } ["encoded"]=> string(0) "" ["mime"]=> string(10) "image/jpeg" ["dirname"]=> string(40) "/mnt/_work_sdb8/wwwroot/lar/votes/public" ["basename"]=> string(7) "foo.jpg" ["extension"]=> string(3) "jpg" ["filename"]=> string(3) "foo" }

I see that $img has protected resource property. How can I resource from $img for imagepng? Do I move in valid way ?

Thanks!

Please sign in or create an account to participate in this conversation.