Skip to content

Commit

Permalink
fix: improve generic placeholder computed width/height when the sourc…
Browse files Browse the repository at this point in the history
…e size is unknown
  • Loading branch information
selul committed Nov 1, 2019
1 parent d4e63b6 commit 0c3cc3f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
6 changes: 4 additions & 2 deletions inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,10 @@ public function set_properties() {

$content_parts = parse_url( content_url() );

$this->upload_resource['content_path'] = $content_parts['path'];
$this->upload_resource['content_host'] = $content_parts['scheme'] . '://' . $content_parts['host'];
$this->upload_resource['content_path'] = $content_parts['path'];
$this->upload_resource['content_folder'] = ltrim( $content_parts['path'], '/' );
$this->upload_resource['content_folder_length'] = strlen( $this->upload_resource['content_folder'] );
$this->upload_resource['content_host'] = $content_parts['scheme'] . '://' . $content_parts['host'];

$service_data = $this->settings->get( 'service_data' );

Expand Down
38 changes: 27 additions & 11 deletions inc/lazyload_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class Optml_Lazyload_Replacer extends Optml_App_Replacer {
use Optml_Normalizer;
use Optml_Validator;

const SVG_PLACEHOLDER = 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%20#width#%20#height#%22%20width%3D%22#width#%22%20height%3D%22#height#%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3C%2Fsvg%3E';
/**
* Cached object instance.
*
Expand Down Expand Up @@ -185,9 +186,10 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
$low_url = apply_filters( 'optml_content_url', $is_slashed ? stripslashes( $original_url ) : $original_url, $optml_args );
$low_url = $is_slashed ? addcslashes( $low_url, '/' ) : $low_url;
} else {
$low_url = self::get_svg_for(
$low_url = $this->get_svg_for(
isset( $optml_args['width'] ) ? $optml_args['width'] : '100%',
isset( $optml_args['height'] ) ? $optml_args['height'] : '100%'
isset( $optml_args['height'] ) ? $optml_args['height'] : '100%',
( $should_ignore_rescale ? null : $original_url )
);
}

Expand Down Expand Up @@ -299,27 +301,41 @@ public function should_add_data_tag( $tag ) {
/**
* Get SVG markup with specific width/height.
*
* @param int $width Markup Width.
* @param int $height Markup Height.
* @param int $width Markup Width.
* @param int $height Markup Height.
* @param string|null $url Original URL.
*
* @return string SVG code.
*/
public static function get_svg_for( $width, $height ) {
public function get_svg_for( $width, $height, $url = null ) {

if ( $url !== null && ! is_numeric( $width ) ) {
$url = strtok( $url, '?' );
$key = crc32( $url );
$sizes = wp_cache_get( $key, 'optml_sources' );
if ( $sizes === false ) {
$filepath = substr( $url, strpos( $url, $this->upload_resource['content_folder'] ) + $this->upload_resource['content_folder_length'] );
$filepath = WP_CONTENT_DIR . $filepath;
if ( is_file( $filepath ) ) {
$sizes = getimagesize( $filepath );
wp_cache_add( $key, [ $sizes[0], $sizes[1] ], 'optml_sources', DAY_IN_SECONDS );
}
}
list( $width, $height ) = $sizes;
}

$width = ! is_numeric( $width ) ? '100%' : $width;
$height = ! is_numeric( $height ) ? '100%' : $height;

static $SVG = '<svg xmlns="http://www.w3.org/2000/svg" width="#width#" height="#height#" style=""> <rect id="backgroundrect" width="100%" height="100%" x="0" y="0" fill="#FFFFFF" stroke="none"/> <g style="" class="currentLayer"> <rect fill="#ffffff" stroke="#ffffff" stroke-width="2" stroke-linejoin="round" stroke-dashoffset="" fill-rule="nonzero" style="color: rgb(0, 0, 0);" class="selected" stroke-opacity="1" fill-opacity="1"/></g></svg>';

return 'data:image/svg+xml,' . rawurlencode(
return
str_replace(
[ '#width#', '#height#' ],
[
$width,
$height,
],
$SVG
)
);
self::SVG_PLACEHOLDER
);
}

/**
Expand Down

0 comments on commit 0c3cc3f

Please sign in to comment.