This PHP script processes an SVG file, colors specific elements (representing teeth) by their IDs, and either outputs the modified SVG or saves it as a new file.
- PHP 7.4+
- Composer (for installing the necessary dependencies)
-
Clone this repository or download the script.
-
Install the required dependencies using Composer:
composer install
$svgFile: The path to the SVG file you want to process. (Required)$outputMode: The output mode, which determines whether the result is printed to the browser or saved to a file. (Required)'O'(output): Outputs the modified SVG directly to the browser.'F'(file): Saves the modified SVG to the storage/filled/ directory.
...$args: One or more IDs of the teeth elements you want to color. (Optional)
- O (Output): Sends the modified SVG directly to the browser with the
Content-Type: image/svg+xmlheader. - F (File): Saves the modified SVG to the
storage/filled/directory with a unique filename and returns the file path.
You can use index.php file in this repository for some quick examples.
To try the O mode you can start a PHP server with php -S localhost:8000 and navigate to http://localhost:8000
require 'path/to/toothColor.php';
try {
// Output mode: O (direct to browser)
toothColor('path/to/teeth.svg', 'O', 1, 3, 5);
// Output mode: F (save to file)
$savedFilePath = toothColor('path/to/teeth.svg', 'F', 1, 3, 5);
echo "File saved to: " . $savedFilePath;
} catch (FileNotFoundException $e) {
echo "Error: SVG file not found!";
} catch (FileNotValidException $e) {
echo "Error: The provided file is not a valid SVG!";
} catch (OutputModeNotValidException $e) {
echo "Error: Output mode is invalid!";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}The following exceptions may be thrown by the script:
FileNotFoundException: Thrown when the provided SVG file cannot be found.FileNotValidException: Thrown when the provided file is not a valid SVG.OutputModeNotValidException: Thrown when an invalid output mode is provided (valid modes are 'O' and 'F').Exception: Thrown if there is an error when attempting to save the SVG inFmode.