Skip to content

Commit da6b151

Browse files
committed
add findLoad(), findLoadBuffer()
they call vips_foreign_find_load(), if available, or fall back to the vips-loader property
1 parent ed93d8c commit da6b151

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ All notable changes to `:vips` will be documented in this file.
66
### Added
77
- fix minor formatting issues reported by phpcs [John Cupitt]
88
- regenerated autodocs from libvips 8.4 to reduce confusion [John Cupitt]
9-
- add Image::hasAlpha tester
9+
- add Image::hasAlpha() [Kleis Auke Wolthuizen]
10+
- add Image::findLoad(), Image::findLoadBuffer() [John Cupitt]
1011

1112
### Deprecated
1213
- Nothing

src/Image.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,44 @@
423423
*/
424424
class Image extends ImageAutodoc implements \ArrayAccess
425425
{
426+
/**
427+
* Map load nicknames to canonical names. Regenerate this table with
428+
* something like:
429+
*
430+
* $ vips -l foreign | grep -i load | awk '{ print $2, $1; }'
431+
*
432+
* Plus a bit of editing.
433+
*
434+
* @internal
435+
*/
436+
private $nicknameToCanonical = [
437+
"csvload" => "VipsForeignLoadCsv",
438+
"matrixload" => "VipsForeignLoadMatrix",
439+
"rawload" => "VipsForeignLoadRaw",
440+
"vipsload" => "VipsForeignLoadVips",
441+
"analyzeload" => "VipsForeignLoadAnalyze",
442+
"ppmload" => "VipsForeignLoadPpm",
443+
"radload" => "VipsForeignLoadRad",
444+
"pdfload" => "VipsForeignLoadPdfFile",
445+
"pdfload_buffer" => "VipsForeignLoadPdfBuffer",
446+
"svgload" => "VipsForeignLoadSvgFile",
447+
"svgload_buffer" => "VipsForeignLoadSvgBuffer",
448+
"gifload" => "VipsForeignLoadGifFile",
449+
"gifload_buffer" => "VipsForeignLoadGifBuffer",
450+
"pngload" => "VipsForeignLoadPng",
451+
"pngload_buffer" => "VipsForeignLoadPngBuffer",
452+
"matload" => "VipsForeignLoadMat",
453+
"jpegload" => "VipsForeignLoadJpegFile",
454+
"jpegload_buffer" => "VipsForeignLoadJpegBuffer",
455+
"webpload" => "VipsForeignLoadWebpFile",
456+
"webpload_buffer" => "VipsForeignLoadWebpBuffer",
457+
"tiffload" => "VipsForeignLoadTiffFile",
458+
"tiffload_buffer" => "VipsForeignLoadTiffBuffer",
459+
"magickload" => "VipsForeignLoadMagickFile",
460+
"magickload_buffer" => "VipsForeignLoadMagickBuffer",
461+
"fitsload" => "VipsForeignLoadFits",
462+
"openexrload" => "VipsForeignLoadOpenexr"
463+
];
426464

427465
/**
428466
* The resource for the underlying VipsImage.
@@ -675,6 +713,37 @@ public static function newFromFile(
675713
return self::wrapResult($result);
676714
}
677715

716+
/**
717+
* Find the name of the load oepration vips will use to load a file, for
718+
* example "VipsForeignLoadJpegFile". You can use this to work out what
719+
* options to pass to newFromFile().
720+
*
721+
* @param string $filename The file to test.
722+
*
723+
* @return string|null The name of the load operation, or null.
724+
*/
725+
public static function findLoad(string $filename): string
726+
{
727+
$result = null;
728+
729+
try {
730+
# added in php-vips-ext 1.0.5
731+
$result = vips_foreign_find_load($filename);
732+
} catch (Exception $e) {
733+
# fallback: use the vips-loader property ... this can be much slower
734+
try {
735+
$image = newFromFile($filename);
736+
# Unfortunately, vips-loader is the operation nickname, rather
737+
# than the canonical name returned by vips_foreign_find_load().
738+
$loader = $image->get("vips-loader");
739+
$result = $nicknameToCanonical[$loader];
740+
} catch (Exception $e) {
741+
}
742+
}
743+
744+
return $result;
745+
}
746+
678747
/**
679748
* Create a new Image from a compressed image held as a string.
680749
*
@@ -696,6 +765,38 @@ public static function newFromBuffer(
696765
return self::wrapResult($result);
697766
}
698767

768+
/**
769+
* Find the name of the load oepration vips will use to load a buffer, for
770+
* example "VipsForeignLoadJpegBuffer". You can use this to work out what
771+
* options to pass to newFromBuffer().
772+
*
773+
* @param string $filename The formatted image to test.
774+
*
775+
* @return string|null The name of the load operation, or null.
776+
*/
777+
public static function findLoadBuffer(string $buffer): string
778+
{
779+
$result = null;
780+
781+
try {
782+
# added in php-vips-ext 1.0.5
783+
$result = vips_foreign_find_load_buffer($buffer);
784+
} catch (Exception $e) {
785+
# fallback: use the vips-loader property ... this can be much slower
786+
try {
787+
$image = newFromBuffer($buffer);
788+
# Unfortunately, vips-loader is the operation nickname, rather
789+
# than the canonical name returned by
790+
# vips_foreign_find_load_buffer().
791+
$loader = $image->get("vips-loader");
792+
$result = $nicknameToCanonical[$loader];
793+
} catch (Exception $e) {
794+
}
795+
}
796+
797+
return $result;
798+
}
799+
699800
/**
700801
* Create a new Image from a php array.
701802
*

tests/new.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function testVipsNewFromFile()
3131
$this->assertEquals($image->bands, 3);
3232
}
3333

34+
public function testVipsFindLoad()
35+
{
36+
$filename = dirname(__FILE__) . "/images/img_0076.jpg";
37+
$loader = Vips\Image::findLoad($filename);
38+
39+
$this->assertEquals($loader, "VipsForeignLoadJpegFile");
40+
}
41+
3442
public function testVipsNewFromBuffer()
3543
{
3644
$filename = dirname(__FILE__) . "/images/img_0076.jpg";
@@ -42,6 +50,15 @@ public function testVipsNewFromBuffer()
4250
$this->assertEquals($image->bands, 3);
4351
}
4452

53+
public function testVipsFindLoadBuffer()
54+
{
55+
$filename = dirname(__FILE__) . "/images/img_0076.jpg";
56+
$buffer = file_get_contents($filename);
57+
$loader = Vips\Image::findLoadBuffer($buffer);
58+
59+
$this->assertEquals($loader, "VipsForeignLoadJpegBuffer");
60+
}
61+
4562
}
4663

4764
/*

0 commit comments

Comments
 (0)