0

I am using the PHP libvips library, I have a function that applies background colour on images using vips flatten method. The issue is when I am trying to apply background colour to .jpg image it gives me the below error

Fatal error: Uncaught Jcupitt\Vips\Exception: linear: vector must have 1 or 2 elements

My Code for applying background colour on image

$img = Vips\Image::newFromFile($inputFileName, ['access' => 'sequential']);
$img = $img->flatten(['background' => [255, 0, 0]]);
$img->writeToFile($outputFileName);

The image which I am trying to apply background color

enter image description here

Note: When I am using Imagick library it also doesn't apply the bg colour on the image but it doesn't give me an error. How can I prevent this in LibVips??

It works fine when image type is png and jpeg

Vinay Kapoor
  • 47
  • 1
  • 10
  • `flatten` flattens out the alpha channel (the last one). It's useful for PNG images if you want to replace the transparent pixels with some colour. JPG images do not have an alpha, so they can't be flattened. – jcupitt Dec 07 '21 at 10:56
  • @jcupitt Is there any way to prevent the error, or can I check something like if image has any alpha channel or not before applying background color – Vinay Kapoor Jan 19 '22 at 06:37
  • Sure, see https://libvips.github.io/php-vips/docs/classes/Jcupitt-Vips-Image.html#method_hasAlpha – jcupitt Jan 19 '22 at 11:20
  • The new URL is https://libvips.github.io/php-vips/classes/Jcupitt-Vips-Image.html#method_hasAlpha – Arkadiy Bolotov Oct 27 '22 at 07:29

0 Answers0