Skip to content

Commit

Permalink
chore: extract dimmensions if needed and add them to url
Browse files Browse the repository at this point in the history
  • Loading branch information
preda-bogdan authored and selul committed Jan 21, 2019
1 parent f5ab3a9 commit b66f135
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
22 changes: 22 additions & 0 deletions inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,28 @@ protected function strip_image_size_from_url( $url ) {
return $url;
}

/**
* Try to determine height and width from strings WP appends to resized image filenames.
*
* @param string $src The image URL.
*
* @return array An array consisting of width and height.
*/
protected function parse_dimensions_from_filename( $src ) {
$width_height_string = array();
$extensions = array_keys( Optml_Config::$extensions );
if ( preg_match( '#-(\d+)x(\d+)\.(?:' . implode( '|', $extensions ) . '){1}$#i', $src, $width_height_string ) ) {
$width = (int) $width_height_string[1];
$height = (int) $width_height_string[2];

if ( $width && $height ) {
return array( $width, $height );
}
}

return array( false, false );
}

/**
* Returns the array of image sizes since `get_intermediate_image_sizes` and image metadata doesn't include the
* custom image sizes in a reliable way.
Expand Down
26 changes: 2 additions & 24 deletions inc/tag_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,6 @@ private function parse_dimensions_from_tag( $tag, $image_sizes, $args = array()
return array( $args['width'], $args['height'], $args['resize'] );
}

/**
* Try to determine height and width from strings WP appends to resized image filenames.
*
* @param string $src The image URL.
*
* @return array An array consisting of width and height.
*/
public static function parse_dimensions_from_filename( $src ) {
$width_height_string = array();
$extensions = array_keys( Optml_Config::$extensions );
if ( preg_match( '#-(\d+)x(\d+)\.(?:' . implode( '|', $extensions ) . '){1}$#i', $src, $width_height_string ) ) {
$width = (int) $width_height_string[1];
$height = (int) $width_height_string[2];

if ( $width && $height ) {
return array( $width, $height );
}
}

return array( false, false );
}

/**
* Called by hook to replace image tags in content.
*
Expand All @@ -110,7 +88,7 @@ public function process_image_tags( $content, $images = array() ) {

list( $width, $height, $resize ) = self::parse_dimensions_from_tag( $images['img_tag'][ $index ], $image_sizes, array( 'width' => $width, 'height' => $height, 'resize' => $resize ) );
if ( false === $width && false === $height ) {
list( $width, $height ) = self::parse_dimensions_from_filename( $tmp );
list( $width, $height ) = $this->parse_dimensions_from_filename( $tmp );
}
$optml_args = $this->to_optml_dimensions_bound( $width, $height, $this->max_width, $this->max_height );
$tmp = $this->strip_image_size_from_url( $tmp );
Expand Down Expand Up @@ -167,7 +145,7 @@ public function filter_srcset_attr( $sources = array(), $size_array = array(), $

foreach ( $sources as $i => $source ) {
$url = $source['url'];
list( $width, $height ) = self::parse_dimensions_from_filename( $url );
list( $width, $height ) = $this->parse_dimensions_from_filename( $url );

if ( empty( $width ) ) {
$width = $image_meta['width'];
Expand Down
12 changes: 11 additions & 1 deletion inc/url_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ public function build_image_url(
$url = str_replace( array_keys( $this->site_mappings ), array_values( $this->site_mappings ), $url );
}

$url = $this->strip_image_size_from_url( $url );
$new_url = $this->strip_image_size_from_url( $url );

if ( $new_url !== $url ) {
list( $width, $height ) = $this->parse_dimensions_from_filename( $url );
$optml_args = $this->to_optml_dimensions_bound( $width, $height, $this->max_width, $this->max_height );
if ( $optml_args['width'] !== false && $optml_args['height'] !== false ) {
$args['width'] = $optml_args['width'];
$args['height'] = $optml_args['height'];
}
$url = $new_url;
}

return ( new Optml_Image( $url, $args ) )->get_url( $this->is_allowed_site );
}
Expand Down

0 comments on commit b66f135

Please sign in to comment.