Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bs_lib
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
bs_lib
Commits
cf12fa39
Commit
cf12fa39
authored
1 year ago
by
Ivica Puljic
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3389133
by pivica: Add service for extraction dimensions from SVG image
parent
0c88bbe1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bs_lib.services.yml
+3
-0
3 additions, 0 deletions
bs_lib.services.yml
src/SvgTools.php
+86
-0
86 additions, 0 deletions
src/SvgTools.php
with
89 additions
and
0 deletions
bs_lib.services.yml
0 → 100644
+
3
−
0
View file @
cf12fa39
services
:
bs_lib.svg_tools
:
class
:
Drupal\bs_lib\SvgTools
This diff is collapsed.
Click to expand it.
src/SvgTools.php
0 → 100644
+
86
−
0
View file @
cf12fa39
<?php
namespace
Drupal\bs_lib
;
/**
* Defines a service with some handy SVG tools.
*/
class
SvgTools
{
/**
* Extracts width and height dimensions from an SVG content string.
*
* This function attempts to retrieve the width and height attributes from the
* SVG content. If these attributes are not found, it falls back to extracting
* the dimensions from the viewBox attribute. The extracted dimensions are
* returned as an associative array with 'width' and 'height' keys.
*
* @param string $svg_content
* The SVG content to extract dimensions from.
*
* @return array|false
* An associative array containing 'width' and 'height' dimensions, or FALSE
* if width or height are not found.
*/
public
function
getDimensions
(
string
$svg_content
)
{
$width
=
NULL
;
$height
=
NULL
;
// First try to get dimensions from width and height attribute.
preg_match
(
'/<svg[^>]*width=["\']?([^"\']+)["\']?[^>]*>/i'
,
$svg_content
,
$widthMatches
);
if
(
!
empty
(
$widthMatches
[
1
]))
{
$width
=
$widthMatches
[
1
];
}
preg_match
(
'/<svg[^>]*height=["\']?([^"\']+)["\']?[^>]*>/i'
,
$svg_content
,
$heightMatches
);
if
(
!
empty
(
$heightMatches
[
1
]))
{
$height
=
$heightMatches
[
1
];
}
// If width or height attributes were not found, try to extract from
// viewBox attribute.
if
(
!
$width
||
!
$height
)
{
preg_match
(
'/<svg[^>]*viewBox=["\']?([^"\']+)["\']?[^>]*>/i'
,
$svg_content
,
$viewBoxMatches
);
if
(
!
empty
(
$viewBoxMatches
[
1
]))
{
$viewBoxValues
=
explode
(
' '
,
$viewBoxMatches
[
1
]);
if
(
count
(
$viewBoxValues
)
>=
4
)
{
$width
=
$viewBoxValues
[
2
];
$height
=
$viewBoxValues
[
3
];
}
}
}
if
(
!
empty
(
$width
)
&&
!
empty
(
$height
))
{
return
[
'width'
=>
$this
->
removeUnit
(
$width
),
'height'
=>
$this
->
removeUnit
(
$height
),
];
}
return
FALSE
;
}
/**
* Removes units from a given value and returns the numeric representation.
*
* This method takes a string value as input, removes any non-numeric and
* non-dot characters (excluding the dot to allow floating-point numbers),
* and converts the resulting string to a float.
*
* @param string $value
* The input value, which may include units or be a numeric string.
* @return float
* The numeric representation of the input value without units.
*
*/
protected
function
removeUnit
(
$value
)
{
// Remove non-numeric and non-dot characters from the value.
$numericValue
=
preg_replace
(
'/[^0-9.]/'
,
''
,
$value
);
// Convert the numeric value to a float.
return
floatval
(
$numericValue
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment