php-vips icon indicating copy to clipboard operation
php-vips copied to clipboard

Some PNG images donesn't work with embed.

Open SudhirVi opened this issue 4 years ago • 5 comments


require __DIR__ . '/vendor/autoload.php';

use Jcupitt\Vips;
use Jcupitt\Vips\Extend;

$target_width = 200;
$target_height = 200;

$bufferData = file_get_contents('./4942896.png');
$image = Vips\Image::thumbnail_buffer($bufferData, $target_width, [
    'height' => $target_height
]);
$x = ($target_width - $image->width) / 2;
$y = ($target_height - $image->height) / 2;

$image = $image->embed($x, $y, $target_width, $target_height, [
    'extend' => Extend::BACKGROUND,
    'background' => [255, 255, 255, 0.0]
]);
$buffer = $image->writeToBuffer('.png', []);
header("Content-Type: image/png");
echo $buffer;

linear: vector must have 1 or 3 elements. 4942896

SudhirVi avatar Apr 05 '21 03:04 SudhirVi

Hi @Sud4,

Your PNG doesn't have an alpha channel, so you only need a three-element array for the background.

Try:

    'background' => [255, 255, 255]

jcupitt avatar Apr 05 '21 12:04 jcupitt

You can also just use a single number, ie.

    'background' => 255

Now it'll work for any PNG image.

jcupitt avatar Apr 05 '21 12:04 jcupitt

it's works, Can i do 'background' => 0 to make extended background transparent.

SudhirVi avatar Apr 05 '21 13:04 SudhirVi

Sure, should work. Though your PNG doesn't have an alpha channel, of course, you'd need to add one.

jcupitt avatar Apr 05 '21 13:04 jcupitt

it's works just perfect, Thank You!.

SudhirVi avatar Apr 05 '21 13:04 SudhirVi