0

I am using libvips PHP library, I am applying some effects on my image using the library and it's work very charm, but somehow for one image it's not working properly and giving me below error

Note - I m getting this error only the image which I uploaded, in other images, it works for me

Fatal error: Uncaught Jcupitt\Vips\Exception: recomb: bands in must equal matrix width

I think the issue is with the image band the image which has 3 bands it's working but for band 1 it's giving me errors.

Here is the code which I am using for apply b & w effect on my image

$img = Vips\Image::newFromFile($inputFileName, ['access' => 'sequential']);
//Matrix for black and white image
$technicolor = Vips\Image::newFromArray([
    [0.2225, 0.7169, 0.0606],
    [0.2225, 0.7169, 0.0606],
    [0.2227, 0.7169, 0.0606],
]);
if ($img->hasAlpha()) {
    $imageWithoutAlpha = $img->extract_band(0, ['n' => $img->bands - 1]);
    $alpha = $img->extract_band($img->bands - 1, ['n' => 1]);
    $img = $imageWithoutAlpha->recomb($technicolor)->bandjoin($alpha);
}
$img = $img->recomb($technicolor);
$img->writeToFile($outputFileName);

Here is the image info

error-img.jpg: 2000x1334 uchar, 1 band, b-w, jpegload
width: 2000
height: 1334
bands: 1
format: uchar
coding: none
interpretation: b-w
xoffset: 0
yoffset: 0
xres: 11.811
yres: 11.811
filename: error-img.jpg
vips-loader: jpegload
jpeg-multiscan: 0
jpeg-chroma-subsample: 4:4:4
iptc-data: 12836 bytes of binary data
exif-data: 13500 bytes of binary data
resolution-unit: in
exif-ifd0-Make: Canon (Canon, ASCII, 6 components, 6 bytes)
exif-ifd0-Model: Canon EOS 7D (Canon EOS 7D, ASCII, 13 components, 13 bytes)
exif-ifd0-XResolution: 300/1 (300, Rational, 1 components, 8 bytes)
exif-ifd0-YResolution: 300/1 (300, Rational, 1 components, 8 bytes)
exif-ifd0-ResolutionUnit: 2 (Inch, Short, 1 components, 2 bytes)
exif-ifd0-Software: Adobe Photoshop Camera Raw 8.1 (Windows) (Adobe Photoshop Camera Raw 8.1 (Windows), ASCII, 41 components, 41 bytes)
exif-ifd0-DateTime: 2013:09:05 23:59:59 (2013:09:05 23:59:59, ASCII, 20 components, 20 bytes)
exif-ifd0-Artist: Nik. Gribas - gnikos@hotmail.com (Nik. Gribas - gnikos@hotmail.com, ASCII, 33 components, 33 bytes)
exif-ifd0-Copyright: Nik. Gribas (Nik. Gribas (Photographer) - [None] (Editor), ASCII, 12 components, 12 bytes)
exif-ifd1-Compression: 6 (JPEG compression, Short, 1 components, 2 bytes)
exif-ifd1-XResolution: 72/1 (72, Rational, 1 components, 8 bytes)
exif-ifd1-YResolution: 72/1 (72, Rational, 1 components, 8 bytes)
exif-ifd1-ResolutionUnit: 2 (Inch, Short, 1 components, 2 bytes)
exif-ifd2-ExposureTime: 1/5 (1/5 sec., Rational, 1 components, 8 bytes)
exif-ifd2-FNumber: 4/1 (f/4.0, Rational, 1 components, 8 bytes)
exif-ifd2-ExposureProgram: 1 (Manual, Short, 1 components, 2 bytes)
exif-ifd2-ISOSpeedRatings: 1600 (1600, Short, 1 components, 2 bytes)
exif-ifd2-ExifVersion: Exif Version 2.3 (Exif Version 2.3, Undefined, 4 components, 4 bytes)
exif-ifd2-DateTimeOriginal: 2013:08:30 23:49:47 (2013:08:30 23:49:47, ASCII, 20 components, 20 bytes)
exif-ifd2-DateTimeDigitized: 2013:08:30 23:49:47 (2013:08:30 23:49:47, ASCII, 20 components, 20 bytes)
exif-ifd2-ShutterSpeedValue: 2321928/1000000 (2.32 EV (1/4 sec.), SRational, 1 components, 8 bytes)
exif-ifd2-ApertureValue: 4/1 (4.00 EV (f/4.0), Rational, 1 components, 8 bytes)
exif-ifd2-ExposureBiasValue: 0/1 (0.00 EV, SRational, 1 components, 8 bytes)
exif-ifd2-MaxApertureValue: 3/1 (3.00 EV (f/2.8), Rational, 1 components, 8 bytes)
exif-ifd2-MeteringMode: 5 (Pattern, Short, 1 components, 2 bytes)
exif-ifd2-Flash: 16 (Flash did not fire, compulsory flash mode, Short, 1 components, 2 bytes)
exif-ifd2-FocalLength: 17/1 (17.0 mm, Rational, 1 components, 8 bytes)
exif-ifd2-SubSecTimeOriginal: 64 (64, ASCII, 3 components, 3 bytes)
exif-ifd2-SubSecTimeDigitized: 64 (64, ASCII, 3 components, 3 bytes)
exif-ifd2-FocalPlaneXResolution: 5184000/907 (5715.546, Rational, 1 components, 8 bytes)
exif-ifd2-FocalPlaneYResolution: 3456000/595 (5808.403, Rational, 1 components, 8 bytes)
exif-ifd2-FocalPlaneResolutionUnit: 2 (Inch, Short, 1 components, 2 bytes)
exif-ifd2-CustomRendered: 0 (Normal process, Short, 1 components, 2 bytes)
exif-ifd2-ExposureMode: 1 (Manual exposure, Short, 1 components, 2 bytes)
exif-ifd2-WhiteBalance: 0 (Auto white balance, Short, 1 components, 2 bytes)
exif-ifd2-SceneCaptureType: 0 (Standard, Short, 1 components, 2 bytes)
exif-ifd2-FlashPixVersion: FlashPix Version 1.0 (FlashPix Version 1.0, Undefined, 4 components, 4 bytes)
exif-ifd2-ColorSpace: 65535 (Internal error (unknown value 65535), Short, 1 components, 2 bytes)
jpeg-thumbnail-data: 12598 bytes of binary data
xmp-data: 10134 bytes of binary data
icc-profile-data: 408 bytes of binary data

The Image in which I am getting error

enter image description here

Vinay Kapoor
  • 47
  • 1
  • 10

1 Answers1

0

Your image is a one-band JPEG, so it's already black and white.

I would convert to black and white with colourspace. It will work for any image, plus it takes account of the image gamma, so it should produce a better result than a simple linear recombination. It also knows about alpha channels, and it's smart enough to pass an image that's already black and white though unchanged.

Docs are here:

https://libvips.github.io/php-vips/docs/classes/Jcupitt-Vips-ImageAutodoc.html#method_colourspace

The possible colourspaces are documented here:

https://libvips.github.io/php-vips/docs/classes/Jcupitt-Vips-Interpretation.html

So you need:

$img = Vips\Image::newFromFile($inputFileName, ['access' => 'sequential']);
$img = $img->colourspace("b-w");
$img->writeToFile($outputFileName);

The php docs are not very good, unfortunately, due to the limitations of php-doc. The python, ruby or C documentation is usually more useful, and the API is the same. Try for example:

https://libvips.github.io/pyvips/vimage.html#pyvips.Image.colourspace

jcupitt
  • 10,213
  • 2
  • 23
  • 39
  • I found this git issue link also, does that issue and this one are both same? (This one is for sharp) https://github.com/lovell/sharp/issues/2584 – Vinay Kapoor Nov 19 '21 at 09:12
  • One more thing I have one common function which accepts the different filter names like sepia, monochrome etc.., and based on filter name I am applying the matrix on image, above solution is only works for black and white – Vinay Kapoor Nov 19 '21 at 09:29
  • You could use `colourspace("srgb")` to force everything (CMYK too, for example) to three bands ready for your matrix. You'd still need to take care around alpha. – jcupitt Nov 19 '21 at 09:46