+ /**
+ * Encode an image file as a base64 data URI.
+ * If the image file has a suitable MIME type and size, encode it as a
+ * base64 data URI. Return false if the image type is unfamiliar or exceeds
+ * the size limit.
+ *
+ * @param string $file Image file to encode.
+ * @param string|null $type File's MIME type or null. If null, CSSMin will
+ * try to autodetect the type.
+ * @param int|bool $sizeLimit If the size of the target file is greater than
+ * this value, decline to encode the image file and return false
+ * instead. If $sizeLimit is false, no limit is enforced.
+ * @return string|bool: Image contents encoded as a data URI or false.
+ */
+ public static function encodeImageAsDataURI( $file, $type = null, $sizeLimit = self::EMBED_SIZE_LIMIT ) {
+ if ( $sizeLimit !== false && filesize( $file ) >= $sizeLimit ) {
+ return false;
+ }
+ if ( $type === null ) {
+ $type = self::getMimeType( $file );
+ }
+ if ( !$type ) {
+ return false;
+ }
+ $data = base64_encode( file_get_contents( $file ) );
+ return 'data:' . $type . ';base64,' . $data;
+ }
+