Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F2362
svgSize.diff
Public
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Authored By
•
bzimport
Nov 21 2014, 8:51 PM
2014-11-21 20:51:37 (UTC+0)
Size
3 KB
Referenced Files
None
Subscribers
None
svgSize.diff
View Options
Index: Image.php
===================================================================
RCS file: /cvsroot/wikipedia/phase3/includes/Image.php,v
retrieving revision 1.132
diff -u -r1.132 Image.php
--- Image.php 22 Oct 2005 20:52:31 -0000 1.132
+++ Image.php 28 Oct 2005 00:02:08 -0000
@@ -1696,13 +1696,18 @@
}
/**
- * Return a rounded pixel equivalent for a labeled CSS/SVG length.
+ * Return a pixel equivalent for a labeled CSS/SVG length.
+ * This may be a float value.
* http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
*
* @param string $length
* @return int Length in pixels
*/
function wfScaleSVGUnit( $length ) {
+ //catch 0, NULL, false, ""
+ if ( $length === 0 ) return 0;
+ if ( is_null( $length ) || $length === false || $length=== "" ) return false;
+
static $unitLength = array(
'px' => 1.0,
'pt' => 1.25,
@@ -1716,10 +1721,12 @@
if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
$length = floatval( $matches[1] );
$unit = $matches[2];
- return round( $length * $unitLength[$unit] );
+ return $length * $unitLength[$unit];
} else {
+ if ( !is_numeric( $length ) ) return false;
+
// Assume pixels
- return round( floatval( $length ) );
+ return floatval( $length );
}
}
@@ -1733,8 +1740,8 @@
* @return array
*/
function wfGetSVGsize( $filename ) {
- $width = 256;
- $height = 256;
+ $width = false;
+ $height = false;
// Read a chunk of the file
$f = fopen( $filename, "rt" );
@@ -1748,14 +1755,43 @@
}
$tag = $matches[1];
if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
- $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
+ $width = trim( substr( $matches[1], 1, -1 ) );
}
if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
- $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
+ $height = trim( substr( $matches[1], 1, -1 ) );
+ }
+
+ $width = wfScaleSVGUnit( $width );
+ $height = wfScaleSVGUnit( $height );
+
+ //if width and height is not given, use something big.
+ //try to preserve spect ratio based on viewBox.
+ if ( !$width || !$height ) {
+ $w = $h = false;
+
+ if( preg_match( '/\bviewBox\s*=\s*"\s*[^\s"]+\s*[^\s"]+\s*([^\s"]+)\s*([^\s"]+)\s*"/s', $tag, $matches )
+ || preg_match( '/\bviewBox\s*=\s*\'\s*[^\s\']+\s*[^\s\']+\s*([^\s\']+)\s*([^\s\']+)\s*\'/s', $tag, $matches ) ) {
+
+ $w = wfScaleSVGUnit( $matches[1] );
+ $h = wfScaleSVGUnit( $matches[2] );
+ }
+
+ if ( $w === 0 || $w === false || $h === 0 || $h === false ) $w = $h = 1;
+
+ $f = $w / $h;
+
+ //NOTE: Per default, use a width that's bigger than most
+ // people's setting for previes on image description pages.
+ // The image is not going to be rendered that big anyway.
+ if ( !$width ) $width = $height ? ( $height * $f ) : 2048;
+ if ( !$height ) $height = $width / $f;
}
- return array( $width, $height, 'SVG',
- "width=\"$width\" height=\"$height\"" );
+ $width= round( $width );
+ $height= round( $height );
+
+ return array( $width, $height,
+ 'SVG', "width=\"$width\" height=\"$height\"" );
}
/**
File Metadata
Details
Attached
Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1938
Default Alt Text
svgSize.diff (3 KB)
Attached To
Mode
T5691: Aspect ratio broken for SVG images without size attributes (PATCH included)
Attached
Detach File
Event Timeline
Log In to Comment