423
423
*/
424
424
class Image extends ImageAutodoc implements \ArrayAccess
425
425
{
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
+ ];
426
464
427
465
/**
428
466
* The resource for the underlying VipsImage.
@@ -675,6 +713,37 @@ public static function newFromFile(
675
713
return self ::wrapResult ($ result );
676
714
}
677
715
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
+
678
747
/**
679
748
* Create a new Image from a compressed image held as a string.
680
749
*
@@ -696,6 +765,38 @@ public static function newFromBuffer(
696
765
return self ::wrapResult ($ result );
697
766
}
698
767
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
+
699
800
/**
700
801
* Create a new Image from a php array.
701
802
*
0 commit comments