1

I am using intervention for php for image manipulations. Is it possible to apply font size and color to text through a variable like this? I have calculated $fontsize and $color above this but it says undefined variable

$img->text($string, $item['x'], $top, function($font) {
                        $font->file('assets/fonts/Roboto-Medium.ttf');
                        $font->size($fontsize);
                        $font->color($color);
                        $font->align('left');
                        $font->valign('top');
                    });

1 Answer 1

5

You need to use below syntax to pass variable:
Here you need to use a use() method.

$img->text($string, $item['x'], $top, function() use($font){
    $font->file('assets/fonts/Roboto-Medium.ttf');
    $font->size($fontsize);
    $font->color($color);
    $font->align('left');
    $font->valign('top');
});

EDIT

Here $font must be a Class object as this is used in the callback function. If you just want array then go for the following way:

$font = []; // initialize array
$img->text($string, $item['x'], $top, function() use($font){
        $font['file'] = 'assets/fonts/Roboto-Medium.ttf';
        $font['size'] = $fontsize;
        $font['color'] = $color;
        $font['align'] = 'left';
        $font['valign'] = 'top';
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer! I should define $font above this right? Can you also please explain how should I define $font ?
Got it :D $img->text($string, $item['x'], $top, function($font)use($fontsize,$color) { $font->file('assets/fonts/Roboto-Medium.ttf'); $font->size($fontsize); $font->color($color); $font->align('left'); $font->valign('top'); }); used like this ^ Thanks a lot!
It gave me error that font file must be included. The font file was already included. So the issue is I guess it doesn't expect an associative array in place of class object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.