From 4141954980fe6f40a41947704b289fd29b0f32bf Mon Sep 17 00:00:00 2001 From: Kirill Samojlenko Date: Tue, 8 May 2018 16:52:48 +0300 Subject: [PATCH 1/9] add debug mode for JRI plugin if defined constant JRI_DUMMY_IMAGE --- models/RwdImage.php | 209 ++++++++++++++++++++++++++++---------------- 1 file changed, 136 insertions(+), 73 deletions(-) diff --git a/models/RwdImage.php b/models/RwdImage.php index 08925b9..5e7fec6 100644 --- a/models/RwdImage.php +++ b/models/RwdImage.php @@ -98,10 +98,33 @@ public function verify_svg_mime_type( $attachment ) { } } + /** + * Generate generate fake src for empty image sizes + * + * @param object $options empty image size options. + * @param bool $retina for retina image + * + * @return string + */ + public function dummy_src( $options, $retina = false ) { + $sizename = $options->key; + + $color = substr( md5( $sizename, false ), 0, 6 ); + $textcolor = '000000'; + if ( $options->size->h == 9999 ) { + $size = ( $retina ) ? $options->size->w * 2 : $options->size->w; + $textcolor = 'ff0000'; + } else { + $size = ( $retina ) ? $options->size->w * 2 : $options->size->w . 'x' . ( $retina ) ? $options->size->h * 2 : $options->size->h; + } + + return "http://via.placeholder.com/{$size}/$color/$textcolor"; + } + /** * Generate tag for the current attachment with specified size * - * @param string|array $size Required image size. + * @param string|array $size Required image size. * @param array $attributes Additional html attributes to be used for main tag. * * @return string @@ -148,19 +171,30 @@ public function picture( $size, $attributes = array() ) { foreach ( $this->rwd_set->options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->picture ) ) { - continue; - } - - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { + $src = $this->dummy_src( $option ); + } else { + continue; + } + } else { + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $src = array( $baseurl . $sources[ $subkey ]['file'] ); - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_descriptor; + $src = array( $baseurl . $sources[ $subkey ]['file'] ); + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { + $src = array( $this->dummy_src( $option ) ); + } + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { + $src[] = $this->dummy_src( $option, true ) . ' ' . $retina_descriptor; + } else { + $src[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_descriptor; + } + } } } } @@ -172,7 +206,7 @@ public function picture( $size, $attributes = array() ) { ); $template = $option->picture ? $option->picture : $default_template; - $html .= strtr( $template, $tokens ) . $this->eol; + $html .= strtr( $template, $tokens ) . $this->eol; } $html .= ''; } // End if(). @@ -185,7 +219,7 @@ public function picture( $size, $attributes = array() ) { /** * Generate tag for the current attachment with specified size * - * @param string|array $size Required image size. + * @param string|array $size Required image size. * @param array $attributes Additional html attributes to be used for main tag. * * @return string @@ -218,24 +252,38 @@ public function img( $size, $attributes = array() ) { // generation of responsive sizes. foreach ( $this->rwd_set->options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->srcset ) ) { - continue; - } - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { + $src = $this->dummy_src( $option ); + } else { + continue; + } + } else { + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - $tokens = array( - '{src}' => esc_attr( $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ) . $sources[ $subkey ]['file'] ), - '{w}' => $meta_data['sizes'][ $option->key ]['width'], - ); - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { - $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; - $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; + $tokens = array( + '{src}' => esc_attr( $baseurl . $sources[ $subkey ]['file'] ), + '{w}' => $meta_data['sizes'][ $option->key ]['width'], + ); + + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { + $tokens['{src}'] = $this->dummy_src( $option ); + } + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { + $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { + $srcset[] = $this->dummy_src( $option, true ) . ' ' . $retina_width . 'w'; + } else { + $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; + } + } } } + } $src = $tokens['{src}']; $srcset[] = strtr( "{src} $option->srcset", $tokens ); @@ -269,7 +317,7 @@ public function img( $size, $attributes = array() ) { * Generate background media queries * * @param string $selector CSS selector. - * @param string|array $size Required image size. + * @param string|array $size Required image size. * * @return string Generated html comments warnings. */ @@ -288,42 +336,57 @@ public function background( $selector, $size ) { } // generation of responsive sizes. foreach ( $rwd_options as $subkey => $option ) { - if ( ! isset( $sources[ $subkey ] ) || is_null( $option->bg ) ) { - continue; - } - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - - $src = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ) . $sources[ $subkey ]['file']; - $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); + if ( ! isset( $sources[ $subkey ] ) || is_null( $option->srcset ) ) { + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { + $src = $this->dummy_src( $option ); + } else { + continue; + } + } else { + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - if ( ! isset( $rwd_background_styles[ $media ] ) ) { - $rwd_background_styles[ $media ] = array(); - } - $rwd_background_styles[ $media ][ $selector ] = "$selector{background-image:url('$src');}"; + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { + $src = $this->dummy_src( $option ); + } else { + $src = $baseurl . $sources[ $subkey ]['file']; + } + $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - // Check media pixel and media resolution dpi. - $media_pixel_ration = ( $multiplier < 2.5 ? 1.5 : 2.5 ); - $media_resolution = ( $multiplier < 2.5 ? '144dpi' : '192dpi' ); - - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - - if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; - $media_retina = strtr( $option->bg_retina, array( - '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", - '{min_res}' => "(min-resolution : {$media_resolution})", - ) ); - if ( ! isset( $rwd_background_styles[ $media_retina ] ) ) { - $rwd_background_styles[ $media_retina ] = array(); + if ( ! isset( $rwd_background_styles[ $media ] ) ) { + $rwd_background_styles[ $media ] = array(); + } + $rwd_background_styles[ $media ][ $selector ] = "$selector{background-image:url('$src');}"; + + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + // Check media pixel and media resolution dpi. + $media_pixel_ration = ( $multiplier < 2.5 ? 1.5 : 2.5 ); + $media_resolution = ( $multiplier < 2.5 ? '144dpi' : '192dpi' ); + + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + + if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { + + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { + $src_retina = $this->dummy_src( $option, true ); + } else { + $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; + } + $media_retina = strtr( $option->bg_retina, array( + '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", + '{min_res}' => "(min-resolution : {$media_resolution})", + ) ); + if ( ! isset( $rwd_background_styles[ $media_retina ] ) ) { + $rwd_background_styles[ $media_retina ] = array(); + } + $rwd_background_styles[ $media_retina ][ $selector ] = "$selector{background-image:url('$src_retina');}"; } - $rwd_background_styles[ $media_retina ][ $selector ] = "$selector{background-image:url('$src_retina');}"; } - } - } // End if(). + } // End if(). + + } } // End foreach(). } // End if(). @@ -333,7 +396,7 @@ public function background( $selector, $size ) { /** * Generate img tag for svg image * - * @param string|array $size Required image size. + * @param string|array $size Required image size. * @param array $attributes Image attributes. * * @return string Generated html comments warnings. @@ -495,10 +558,10 @@ public function get_set_sources() { * * @param int $attach_id Attachment ID. * @param array $meta_data Attachment meta data. - * @param string $key Image size key. - * @param int $width Image width. - * @param int $height Image height. - * @param int $crop Crop image. + * @param string $key Image size key. + * @param int $width Image width. + * @param int $height Image height. + * @param int $crop Crop image. * * @return array */ @@ -509,8 +572,8 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr $image_baseurl = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . image_get_intermediate_size( $attach_id, $key )['path']; if ( ! file_exists( $image_baseurl ) || ! isset( $meta_data['sizes'][ $key ]['rwd_width'] ) - || $meta_data['sizes'][ $key ]['rwd_width'] !== $width || $meta_data['sizes'][ $key ]['rwd_height'] !== $height - || ( 0 !== strcmp( $crop_str, $meta_data['sizes'][ $key ]['crop'] ) ) + || $meta_data['sizes'][ $key ]['rwd_width'] !== $width || $meta_data['sizes'][ $key ]['rwd_height'] !== $height + || ( 0 !== strcmp( $crop_str, $meta_data['sizes'][ $key ]['crop'] ) ) ) { // Get WP Image Editor Instance. $image_path = get_attached_file( $attach_id ); @@ -528,8 +591,8 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr // We taked resized image only if we sure that resize was successful and resized dimensions are correct. // - if original image is bigger than resized copy - resize was successful. if ( ( $meta_data['width'] > $resize_sizes['width'] && $meta_data['height'] > $resize_sizes['height'] ) - // - if crop enabled and resized image match the requested size - resize was successful. - || ( ! empty( $crop ) && $resize_sizes['width'] === $width && $resize_sizes['height'] === $height ) + // - if crop enabled and resized image match the requested size - resize was successful. + || ( ! empty( $crop ) && $resize_sizes['width'] === $width && $resize_sizes['height'] === $height ) ) { // WP Image Editor save image. $image_editor->save(); @@ -630,7 +693,7 @@ protected function get_attachment_metadata( $attachment_id ) { * Set updated values to cache * * @param int $attachment_id Attachment post to update it's metadata cache. - * @param array $meta_data New meta data values. + * @param array $meta_data New meta data values. */ protected function set_attachment_metadata( $attachment_id, $meta_data ) { static::$meta_datas[ $attachment_id ] = $meta_data; From d853af2314e2dbd427c31fed77bbfbf4dc44778c Mon Sep 17 00:00:00 2001 From: Kirill Samojlenko Date: Tue, 8 May 2018 16:52:48 +0300 Subject: [PATCH 2/9] add debug mode for JRI plugin if defined constant JRI_DUMMY_IMAGE --- models/RwdImage.php | 209 ++++++++++++++++++++++++++++---------------- 1 file changed, 136 insertions(+), 73 deletions(-) diff --git a/models/RwdImage.php b/models/RwdImage.php index 08925b9..5e7fec6 100644 --- a/models/RwdImage.php +++ b/models/RwdImage.php @@ -98,10 +98,33 @@ public function verify_svg_mime_type( $attachment ) { } } + /** + * Generate generate fake src for empty image sizes + * + * @param object $options empty image size options. + * @param bool $retina for retina image + * + * @return string + */ + public function dummy_src( $options, $retina = false ) { + $sizename = $options->key; + + $color = substr( md5( $sizename, false ), 0, 6 ); + $textcolor = '000000'; + if ( $options->size->h == 9999 ) { + $size = ( $retina ) ? $options->size->w * 2 : $options->size->w; + $textcolor = 'ff0000'; + } else { + $size = ( $retina ) ? $options->size->w * 2 : $options->size->w . 'x' . ( $retina ) ? $options->size->h * 2 : $options->size->h; + } + + return "http://via.placeholder.com/{$size}/$color/$textcolor"; + } + /** * Generate tag for the current attachment with specified size * - * @param string|array $size Required image size. + * @param string|array $size Required image size. * @param array $attributes Additional html attributes to be used for main tag. * * @return string @@ -148,19 +171,30 @@ public function picture( $size, $attributes = array() ) { foreach ( $this->rwd_set->options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->picture ) ) { - continue; - } - - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { + $src = $this->dummy_src( $option ); + } else { + continue; + } + } else { + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $src = array( $baseurl . $sources[ $subkey ]['file'] ); - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_descriptor; + $src = array( $baseurl . $sources[ $subkey ]['file'] ); + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { + $src = array( $this->dummy_src( $option ) ); + } + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { + $src[] = $this->dummy_src( $option, true ) . ' ' . $retina_descriptor; + } else { + $src[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_descriptor; + } + } } } } @@ -172,7 +206,7 @@ public function picture( $size, $attributes = array() ) { ); $template = $option->picture ? $option->picture : $default_template; - $html .= strtr( $template, $tokens ) . $this->eol; + $html .= strtr( $template, $tokens ) . $this->eol; } $html .= ''; } // End if(). @@ -185,7 +219,7 @@ public function picture( $size, $attributes = array() ) { /** * Generate tag for the current attachment with specified size * - * @param string|array $size Required image size. + * @param string|array $size Required image size. * @param array $attributes Additional html attributes to be used for main tag. * * @return string @@ -218,24 +252,38 @@ public function img( $size, $attributes = array() ) { // generation of responsive sizes. foreach ( $this->rwd_set->options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->srcset ) ) { - continue; - } - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { + $src = $this->dummy_src( $option ); + } else { + continue; + } + } else { + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - $tokens = array( - '{src}' => esc_attr( $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ) . $sources[ $subkey ]['file'] ), - '{w}' => $meta_data['sizes'][ $option->key ]['width'], - ); - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { - $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; - $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; + $tokens = array( + '{src}' => esc_attr( $baseurl . $sources[ $subkey ]['file'] ), + '{w}' => $meta_data['sizes'][ $option->key ]['width'], + ); + + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { + $tokens['{src}'] = $this->dummy_src( $option ); + } + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { + $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { + $srcset[] = $this->dummy_src( $option, true ) . ' ' . $retina_width . 'w'; + } else { + $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; + } + } } } + } $src = $tokens['{src}']; $srcset[] = strtr( "{src} $option->srcset", $tokens ); @@ -269,7 +317,7 @@ public function img( $size, $attributes = array() ) { * Generate background media queries * * @param string $selector CSS selector. - * @param string|array $size Required image size. + * @param string|array $size Required image size. * * @return string Generated html comments warnings. */ @@ -288,42 +336,57 @@ public function background( $selector, $size ) { } // generation of responsive sizes. foreach ( $rwd_options as $subkey => $option ) { - if ( ! isset( $sources[ $subkey ] ) || is_null( $option->bg ) ) { - continue; - } - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - - $src = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ) . $sources[ $subkey ]['file']; - $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); + if ( ! isset( $sources[ $subkey ] ) || is_null( $option->srcset ) ) { + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { + $src = $this->dummy_src( $option ); + } else { + continue; + } + } else { + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - if ( ! isset( $rwd_background_styles[ $media ] ) ) { - $rwd_background_styles[ $media ] = array(); - } - $rwd_background_styles[ $media ][ $selector ] = "$selector{background-image:url('$src');}"; + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { + $src = $this->dummy_src( $option ); + } else { + $src = $baseurl . $sources[ $subkey ]['file']; + } + $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - // Check media pixel and media resolution dpi. - $media_pixel_ration = ( $multiplier < 2.5 ? 1.5 : 2.5 ); - $media_resolution = ( $multiplier < 2.5 ? '144dpi' : '192dpi' ); - - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - - if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; - $media_retina = strtr( $option->bg_retina, array( - '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", - '{min_res}' => "(min-resolution : {$media_resolution})", - ) ); - if ( ! isset( $rwd_background_styles[ $media_retina ] ) ) { - $rwd_background_styles[ $media_retina ] = array(); + if ( ! isset( $rwd_background_styles[ $media ] ) ) { + $rwd_background_styles[ $media ] = array(); + } + $rwd_background_styles[ $media ][ $selector ] = "$selector{background-image:url('$src');}"; + + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + // Check media pixel and media resolution dpi. + $media_pixel_ration = ( $multiplier < 2.5 ? 1.5 : 2.5 ); + $media_resolution = ( $multiplier < 2.5 ? '144dpi' : '192dpi' ); + + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + + if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { + + if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { + $src_retina = $this->dummy_src( $option, true ); + } else { + $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; + } + $media_retina = strtr( $option->bg_retina, array( + '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", + '{min_res}' => "(min-resolution : {$media_resolution})", + ) ); + if ( ! isset( $rwd_background_styles[ $media_retina ] ) ) { + $rwd_background_styles[ $media_retina ] = array(); + } + $rwd_background_styles[ $media_retina ][ $selector ] = "$selector{background-image:url('$src_retina');}"; } - $rwd_background_styles[ $media_retina ][ $selector ] = "$selector{background-image:url('$src_retina');}"; } - } - } // End if(). + } // End if(). + + } } // End foreach(). } // End if(). @@ -333,7 +396,7 @@ public function background( $selector, $size ) { /** * Generate img tag for svg image * - * @param string|array $size Required image size. + * @param string|array $size Required image size. * @param array $attributes Image attributes. * * @return string Generated html comments warnings. @@ -495,10 +558,10 @@ public function get_set_sources() { * * @param int $attach_id Attachment ID. * @param array $meta_data Attachment meta data. - * @param string $key Image size key. - * @param int $width Image width. - * @param int $height Image height. - * @param int $crop Crop image. + * @param string $key Image size key. + * @param int $width Image width. + * @param int $height Image height. + * @param int $crop Crop image. * * @return array */ @@ -509,8 +572,8 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr $image_baseurl = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . image_get_intermediate_size( $attach_id, $key )['path']; if ( ! file_exists( $image_baseurl ) || ! isset( $meta_data['sizes'][ $key ]['rwd_width'] ) - || $meta_data['sizes'][ $key ]['rwd_width'] !== $width || $meta_data['sizes'][ $key ]['rwd_height'] !== $height - || ( 0 !== strcmp( $crop_str, $meta_data['sizes'][ $key ]['crop'] ) ) + || $meta_data['sizes'][ $key ]['rwd_width'] !== $width || $meta_data['sizes'][ $key ]['rwd_height'] !== $height + || ( 0 !== strcmp( $crop_str, $meta_data['sizes'][ $key ]['crop'] ) ) ) { // Get WP Image Editor Instance. $image_path = get_attached_file( $attach_id ); @@ -528,8 +591,8 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr // We taked resized image only if we sure that resize was successful and resized dimensions are correct. // - if original image is bigger than resized copy - resize was successful. if ( ( $meta_data['width'] > $resize_sizes['width'] && $meta_data['height'] > $resize_sizes['height'] ) - // - if crop enabled and resized image match the requested size - resize was successful. - || ( ! empty( $crop ) && $resize_sizes['width'] === $width && $resize_sizes['height'] === $height ) + // - if crop enabled and resized image match the requested size - resize was successful. + || ( ! empty( $crop ) && $resize_sizes['width'] === $width && $resize_sizes['height'] === $height ) ) { // WP Image Editor save image. $image_editor->save(); @@ -630,7 +693,7 @@ protected function get_attachment_metadata( $attachment_id ) { * Set updated values to cache * * @param int $attachment_id Attachment post to update it's metadata cache. - * @param array $meta_data New meta data values. + * @param array $meta_data New meta data values. */ protected function set_attachment_metadata( $attachment_id, $meta_data ) { static::$meta_datas[ $attachment_id ] = $meta_data; From 2f3a95d9710a84181064f47986633a088c2ef63d Mon Sep 17 00:00:00 2001 From: Kirill Samojlenko Date: Tue, 15 May 2018 11:27:04 +0300 Subject: [PATCH 3/9] chnage image sources for add fake image name for empty options --- models/RwdImage.php | 72 ++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/models/RwdImage.php b/models/RwdImage.php index 5e7fec6..ca718e4 100644 --- a/models/RwdImage.php +++ b/models/RwdImage.php @@ -80,6 +80,9 @@ class RwdImage { * @param \WP_Post|int|null $attachment Image attachment to be displayed. */ public function __construct( $attachment ) { + if ( ! defined( 'JRI_DUMMY_IMAGE' ) ) { + define( 'JRI_DUMMY_IMAGE', false ); + } $this->attachment = $this->load_attachment( $attachment ); } @@ -109,16 +112,15 @@ public function verify_svg_mime_type( $attachment ) { public function dummy_src( $options, $retina = false ) { $sizename = $options->key; - $color = substr( md5( $sizename, false ), 0, 6 ); - $textcolor = '000000'; if ( $options->size->h == 9999 ) { - $size = ( $retina ) ? $options->size->w * 2 : $options->size->w; - $textcolor = 'ff0000'; + $size = ( $retina ) ? ($options->size->w * 2).'x'.($options->size->w * 2) : $options->size->w.'x'.$options->size->w; + } elseif ( $options->size->w == 9999 ) { + $size = ( $retina ) ? ($options->size->h * 2).'x'.($options->size->h * 2) : $options->size->h.'x'.$options->size->h; } else { $size = ( $retina ) ? $options->size->w * 2 : $options->size->w . 'x' . ( $retina ) ? $options->size->h * 2 : $options->size->h; } - return "http://via.placeholder.com/{$size}/$color/$textcolor"; + return $sizename.'-'.$size.'.png'; } /** @@ -181,19 +183,12 @@ public function picture( $size, $attributes = array() ) { $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); $src = array( $baseurl . $sources[ $subkey ]['file'] ); - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { - $src = array( $this->dummy_src( $option ) ); - } // get retina sources. if ( $option->retina_options ) { foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { - $src[] = $this->dummy_src( $option, true ) . ' ' . $retina_descriptor; - } else { - $src[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_descriptor; - } + $src[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_descriptor; } } } @@ -266,20 +261,13 @@ public function img( $size, $attributes = array() ) { '{w}' => $meta_data['sizes'][ $option->key ]['width'], ); - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { - $tokens['{src}'] = $this->dummy_src( $option ); - } // get retina sources. if ( $option->retina_options ) { foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { - $srcset[] = $this->dummy_src( $option, true ) . ' ' . $retina_width . 'w'; - } else { - $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; - } + $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; } } } @@ -346,11 +334,7 @@ public function background( $selector, $size ) { $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $sources[ $subkey ]['file'] ) ) { - $src = $this->dummy_src( $option ); - } else { - $src = $baseurl . $sources[ $subkey ]['file']; - } + $src = $baseurl . $sources[ $subkey ]['file']; $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); if ( ! isset( $rwd_background_styles[ $media ] ) ) { @@ -368,12 +352,7 @@ public function background( $selector, $size ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE && ! file_exists( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { - $src_retina = $this->dummy_src( $option, true ); - } else { - $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; - } + $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; $media_retina = strtr( $option->bg_retina, array( '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", '{min_res}' => "(min-resolution : {$media_resolution})", @@ -517,6 +496,7 @@ public function get_set_sources() { $this->set_attachment_metadata( $attachment->ID, $meta_data ); } else { // Resize image if not exists. + $meta_data = $this->resize_image( $attachment->ID, $meta_data, @@ -528,15 +508,29 @@ public function get_set_sources() { // Resize retina images if not exists. if ( $option->retina_options ) { foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); $meta_data = $this->resize_image( $attachment->ID, $meta_data, - ImageSize::get_retina_key( $option->key, $retina_descriptor ), + $retina_image_size, $option->size->w * $multiplier, $option->size->h * $multiplier, $option->size->crop ); + + if( JRI_DUMMY_IMAGE ){ + $dirname = _wp_get_attachment_relative_path( $meta_data['file'] ); + if ( $dirname ) { + $dirname = trailingslashit( $dirname ); + } + $upload_dir = wp_get_upload_dir(); + $image_basedir = trailingslashit( $upload_dir['basedir'] ) . $dirname; + if(!file_exists($image_basedir.$meta_data['sizes'][ $retina_image_size ]['file'])){ + $meta_data['sizes'][ $retina_image_size ]['file'] = $this->dummy_src($option, true ); + } + } } + //var_dump($option->retina_options, $option->key,ImageSize::get_retina_key( $option->key, $retina_descriptor ),$meta_data); die; } } @@ -546,6 +540,18 @@ public function get_set_sources() { continue; } + if( JRI_DUMMY_IMAGE ){ + $dirname = _wp_get_attachment_relative_path( $meta_data['file'] ); + if ( $dirname ) { + $dirname = trailingslashit( $dirname ); + } + $upload_dir = wp_get_upload_dir(); + $image_basedir = trailingslashit( $upload_dir['basedir'] ) . $dirname; + if(!file_exists($image_basedir.$meta_data['sizes'][ $option->key ]['file'])){ + $meta_data['sizes'][ $option->key ]['file'] = $this->dummy_src($option); + } + } + $sources[ $subkey ] = $meta_data['sizes'][ $option->key ]; $sources[ $subkey ]['attachment_id'] = $attachment->ID; } // End foreach(). From 2b4aa4ee424a9d86748d37fe5a6ecd3b73ed3e33 Mon Sep 17 00:00:00 2001 From: Kirill Samojlenko Date: Thu, 17 May 2018 15:53:21 +0300 Subject: [PATCH 4/9] remove var_dump --- models/RwdImage.php | 1 - 1 file changed, 1 deletion(-) diff --git a/models/RwdImage.php b/models/RwdImage.php index ca718e4..b1c322a 100644 --- a/models/RwdImage.php +++ b/models/RwdImage.php @@ -530,7 +530,6 @@ public function get_set_sources() { } } } - //var_dump($option->retina_options, $option->key,ImageSize::get_retina_key( $option->key, $retina_descriptor ),$meta_data); die; } } From cb3fa6651e3ac98923d70bcf0ddc9025bb5244b4 Mon Sep 17 00:00:00 2001 From: Kirill Samojlenko Date: Thu, 17 May 2018 16:28:45 +0300 Subject: [PATCH 5/9] Merge branch 'develop' of https://github.com/justcoded/just-responsive-images into feature/dummyImages Conflicts: models/RwdImage.php --- models/RwdImage.php | 100 ++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/models/RwdImage.php b/models/RwdImage.php index b1c322a..5bd7559 100644 --- a/models/RwdImage.php +++ b/models/RwdImage.php @@ -101,27 +101,6 @@ public function verify_svg_mime_type( $attachment ) { } } - /** - * Generate generate fake src for empty image sizes - * - * @param object $options empty image size options. - * @param bool $retina for retina image - * - * @return string - */ - public function dummy_src( $options, $retina = false ) { - $sizename = $options->key; - - if ( $options->size->h == 9999 ) { - $size = ( $retina ) ? ($options->size->w * 2).'x'.($options->size->w * 2) : $options->size->w.'x'.$options->size->w; - } elseif ( $options->size->w == 9999 ) { - $size = ( $retina ) ? ($options->size->h * 2).'x'.($options->size->h * 2) : $options->size->h.'x'.$options->size->h; - } else { - $size = ( $retina ) ? $options->size->w * 2 : $options->size->w . 'x' . ( $retina ) ? $options->size->h * 2 : $options->size->h; - } - - return $sizename.'-'.$size.'.png'; - } /** * Generate tag for the current attachment with specified size @@ -267,7 +246,7 @@ public function img( $size, $attributes = array() ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; - $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; + $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; } } } @@ -334,7 +313,7 @@ public function background( $selector, $size ) { $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - $src = $baseurl . $sources[ $subkey ]['file']; + $src = $baseurl . $sources[ $subkey ]['file']; $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); if ( ! isset( $rwd_background_styles[ $media ] ) ) { @@ -352,7 +331,7 @@ public function background( $selector, $size ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; + $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; $media_retina = strtr( $option->bg_retina, array( '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", '{min_res}' => "(min-resolution : {$media_resolution})", @@ -457,7 +436,6 @@ public function get_set_sources() { if ( empty( $this->rwd_set ) ) { return null; } - $sources = array(); $attachment_meta = $this->get_attachment_metadata( $this->attachment->ID ); $is_attachment_svg = $this->verify_svg_mime_type( $this->attachment ); @@ -509,7 +487,7 @@ public function get_set_sources() { if ( $option->retina_options ) { foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - $meta_data = $this->resize_image( + $meta_data = $this->resize_image( $attachment->ID, $meta_data, $retina_image_size, @@ -518,16 +496,8 @@ public function get_set_sources() { $option->size->crop ); - if( JRI_DUMMY_IMAGE ){ - $dirname = _wp_get_attachment_relative_path( $meta_data['file'] ); - if ( $dirname ) { - $dirname = trailingslashit( $dirname ); - } - $upload_dir = wp_get_upload_dir(); - $image_basedir = trailingslashit( $upload_dir['basedir'] ) . $dirname; - if(!file_exists($image_basedir.$meta_data['sizes'][ $retina_image_size ]['file'])){ - $meta_data['sizes'][ $retina_image_size ]['file'] = $this->dummy_src($option, true ); - } + if ( JRI_DUMMY_IMAGE ) { + $meta_data['sizes'][ $retina_image_size ]['file'] = $this->dummy_src( $option, true, $meta_data ); } } } @@ -539,16 +509,8 @@ public function get_set_sources() { continue; } - if( JRI_DUMMY_IMAGE ){ - $dirname = _wp_get_attachment_relative_path( $meta_data['file'] ); - if ( $dirname ) { - $dirname = trailingslashit( $dirname ); - } - $upload_dir = wp_get_upload_dir(); - $image_basedir = trailingslashit( $upload_dir['basedir'] ) . $dirname; - if(!file_exists($image_basedir.$meta_data['sizes'][ $option->key ]['file'])){ - $meta_data['sizes'][ $option->key ]['file'] = $this->dummy_src($option); - } + if ( JRI_DUMMY_IMAGE ) { + $meta_data['sizes'][ $option->key ]['file'] = $this->dummy_src( $option, false, $meta_data ); } $sources[ $subkey ] = $meta_data['sizes'][ $option->key ]; @@ -558,6 +520,44 @@ public function get_set_sources() { return $sources; } + /** + * Generate generate fake src for empty image sizes + * + * @param object $options empty image size options. + * @param bool $retina for retina image + * + * @return string + */ + public function dummy_src( $options, $retina = false, $meta_data = false ) { + + $sizename = $options->key; + + if ( $options->size->h == 9999 ) { + $size = ( $retina ) ? ( $options->size->w * 2 ) . 'x' . ( $options->size->w * 2 ) : $options->size->w . 'x' . $options->size->w; + } elseif ( $options->size->w == 9999 ) { + $size = ( $retina ) ? ( $options->size->h * 2 ) . 'x' . ( $options->size->h * 2 ) : $options->size->h . 'x' . $options->size->h; + } else { + $size = ( $retina ) ? $options->size->w * 2 : $options->size->w . 'x' . ( $retina ) ? $options->size->h * 2 : $options->size->h; + } + + if ( $meta_data ) { + $dirname = _wp_get_attachment_relative_path( $meta_data['file'] ); + if ( $dirname ) { + $dirname = trailingslashit( $dirname ); + } + $upload_dir = wp_get_upload_dir(); + $image_basedir = trailingslashit( $upload_dir['basedir'] ) . $dirname; + if ( ! file_exists( $image_basedir . $meta_data['sizes'][ $sizename ]['file'] ) ) { + return $sizename . '-' . $size . '.png'; + } else { + return $meta_data['sizes'][ $sizename ]['file']; + } + } else { + return $sizename . '-' . $size . '.png'; + } + + } + /** * Dynamically resize image. * @@ -577,8 +577,8 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr $image_baseurl = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . image_get_intermediate_size( $attach_id, $key )['path']; if ( ! file_exists( $image_baseurl ) || ! isset( $meta_data['sizes'][ $key ]['rwd_width'] ) - || $meta_data['sizes'][ $key ]['rwd_width'] !== $width || $meta_data['sizes'][ $key ]['rwd_height'] !== $height - || ( 0 !== strcmp( $crop_str, $meta_data['sizes'][ $key ]['crop'] ) ) + || $meta_data['sizes'][ $key ]['rwd_width'] !== $width || $meta_data['sizes'][ $key ]['rwd_height'] !== $height + || ( 0 !== strcmp( $crop_str, $meta_data['sizes'][ $key ]['crop'] ) ) ) { // Get WP Image Editor Instance. $image_path = get_attached_file( $attach_id ); @@ -596,8 +596,8 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr // We taked resized image only if we sure that resize was successful and resized dimensions are correct. // - if original image is bigger than resized copy - resize was successful. if ( ( $meta_data['width'] > $resize_sizes['width'] && $meta_data['height'] > $resize_sizes['height'] ) - // - if crop enabled and resized image match the requested size - resize was successful. - || ( ! empty( $crop ) && $resize_sizes['width'] === $width && $resize_sizes['height'] === $height ) + // - if crop enabled and resized image match the requested size - resize was successful. + || ( ! empty( $crop ) && $resize_sizes['width'] === $width && $resize_sizes['height'] === $height ) ) { // WP Image Editor save image. $image_editor->save(); From 3b45a7522d63fddf8b40fe16548a5ac28c2a4ed8 Mon Sep 17 00:00:00 2001 From: Alex Prokopenko Date: Wed, 27 Jun 2018 19:47:58 +0300 Subject: [PATCH 6/9] Final refactoring and test of JRI dummy image --- models/RwdImage.php | 146 ++++++++++++++++++++++++++++---------------- 1 file changed, 94 insertions(+), 52 deletions(-) diff --git a/models/RwdImage.php b/models/RwdImage.php index 5bd7559..5878055 100644 --- a/models/RwdImage.php +++ b/models/RwdImage.php @@ -80,9 +80,7 @@ class RwdImage { * @param \WP_Post|int|null $attachment Image attachment to be displayed. */ public function __construct( $attachment ) { - if ( ! defined( 'JRI_DUMMY_IMAGE' ) ) { - define( 'JRI_DUMMY_IMAGE', false ); - } + defined( 'JRI_DUMMY_IMAGE' ) || define( 'JRI_DUMMY_IMAGE', false ); $this->attachment = $this->load_attachment( $attachment ); } @@ -153,7 +151,7 @@ public function picture( $size, $attributes = array() ) { foreach ( $this->rwd_set->options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->picture ) ) { if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { - $src = $this->dummy_src( $option ); + $src = $this->dummy_source( $option ); } else { continue; } @@ -161,13 +159,16 @@ public function picture( $size, $attributes = array() ) { $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $src = array( $baseurl . $sources[ $subkey ]['file'] ); + $src = array( + $this->get_attachment_url( $baseurl, $sources[ $subkey ] ), + ); // get retina sources. if ( $option->retina_options ) { foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_descriptor; + $src[] = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ) + . ' ' . $retina_descriptor; } } } @@ -180,7 +181,7 @@ public function picture( $size, $attributes = array() ) { ); $template = $option->picture ? $option->picture : $default_template; - $html .= strtr( $template, $tokens ) . $this->eol; + $html .= strtr( $template, $tokens ) . $this->eol; } $html .= ''; } // End if(). @@ -227,7 +228,7 @@ public function img( $size, $attributes = array() ) { foreach ( $this->rwd_set->options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->srcset ) ) { if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { - $src = $this->dummy_src( $option ); + $src = $this->dummy_source( $option ); } else { continue; } @@ -236,7 +237,7 @@ public function img( $size, $attributes = array() ) { $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); $tokens = array( - '{src}' => esc_attr( $baseurl . $sources[ $subkey ]['file'] ), + '{src}' => esc_attr( $this->get_attachment_url( $baseurl, $sources[ $subkey ] ) ), '{w}' => $meta_data['sizes'][ $option->key ]['width'], ); @@ -246,11 +247,11 @@ public function img( $size, $attributes = array() ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; - $srcset[] = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file'] . ' ' . $retina_width . 'w'; + $srcset[] = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ) + . ' ' . $retina_width . 'w'; } } } - } $src = $tokens['{src}']; $srcset[] = strtr( "{src} $option->srcset", $tokens ); @@ -305,7 +306,7 @@ public function background( $selector, $size ) { foreach ( $rwd_options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->srcset ) ) { if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { - $src = $this->dummy_src( $option ); + $src = $this->dummy_source( $option ); } else { continue; } @@ -313,7 +314,7 @@ public function background( $selector, $size ) { $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - $src = $baseurl . $sources[ $subkey ]['file']; + $src = $this->get_attachment_url( $baseurl, $sources[ $subkey ] ); $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); if ( ! isset( $rwd_background_styles[ $media ] ) ) { @@ -331,7 +332,7 @@ public function background( $selector, $size ) { $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src_retina = $baseurl . $meta_data['sizes'][ $retina_image_size ]['file']; + $src_retina = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ); $media_retina = strtr( $option->bg_retina, array( '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", '{min_res}' => "(min-resolution : {$media_resolution})", @@ -363,7 +364,7 @@ public function svg( $size, $attributes ) { // prepare image attributes (class, alt, title etc). $attr = array( - 'class' => "wp-post-image", + 'class' => 'wp-post-image', 'alt' => trim( strip_tags( get_post_meta( $this->attachment->ID, '_wp_attachment_image_alt', true ) ) ), ); @@ -446,6 +447,8 @@ public function get_set_sources() { $attachment_meta['sizes'][ $this->rwd_set->key ]['width'] : $attachment_meta['width']; } + $dummy_sizes = array(); + $dummy_meta = array(); foreach ( $this->rwd_set->options as $subkey => $option ) { $attachment = empty( $this->rwd_rewrite[ $subkey ] ) ? $this->attachment : $this->rwd_rewrite[ $subkey ]; $meta_data = $this->get_attachment_metadata( $attachment->ID ); @@ -453,7 +456,6 @@ public function get_set_sources() { // svg images doesn't have meta data, so we need to generate it. if ( $is_subsize_svg ) { - if ( ! is_array( $meta_data ) ) { $meta_data = array(); } @@ -474,7 +476,6 @@ public function get_set_sources() { $this->set_attachment_metadata( $attachment->ID, $meta_data ); } else { // Resize image if not exists. - $meta_data = $this->resize_image( $attachment->ID, $meta_data, @@ -483,6 +484,10 @@ public function get_set_sources() { $option->size->h, $option->size->crop ); + if ( JRI_DUMMY_IMAGE && empty( $meta_data['sizes'][ $option->key ]['file'] ) ) { + $dummy_sizes[ $option->key ] = $this->dummy_source( $option, false, $meta_data ); + } + // Resize retina images if not exists. if ( $option->retina_options ) { foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { @@ -496,66 +501,81 @@ public function get_set_sources() { $option->size->crop ); - if ( JRI_DUMMY_IMAGE ) { - $meta_data['sizes'][ $retina_image_size ]['file'] = $this->dummy_src( $option, true, $meta_data ); + if ( JRI_DUMMY_IMAGE && empty( $dummy_sizes[ $retina_image_size ]['file'] ) ) { + $dummy_sizes[ $retina_image_size ] = $this->dummy_source( $option, $multiplier, $meta_data ); } } } } + if ( JRI_DUMMY_IMAGE ) { + if ( ! isset( $dummy_meta[ $attachment->ID ] ) ) { + $dummy_meta[ $attachment->ID ] = $meta_data; + } + $meta_data['sizes'] = array_merge( $dummy_meta[ $attachment->ID ]['sizes'], $dummy_sizes ); + $dummy_meta[ $attachment->ID ] = $meta_data; + } + // however if we didn't find correct size - we skip this size with warning. if ( ! isset( $meta_data['sizes'][ $option->key ] ) ) { $this->warnings[] = "Attachment {$attachment->ID}: missing image size \"{$this->rwd_set->key}:{$subkey}\""; continue; } - if ( JRI_DUMMY_IMAGE ) { - $meta_data['sizes'][ $option->key ]['file'] = $this->dummy_src( $option, false, $meta_data ); - } - $sources[ $subkey ] = $meta_data['sizes'][ $option->key ]; $sources[ $subkey ]['attachment_id'] = $attachment->ID; } // End foreach(). + // cache all dummy retina sizes. + if ( JRI_DUMMY_IMAGE ) { + foreach ( $dummy_meta as $attachment_id => $meta_data ) { + $this->set_attachment_metadata( $attachment_id, $meta_data ); + } + } + return $sources; } /** * Generate generate fake src for empty image sizes * - * @param object $options empty image size options. - * @param bool $retina for retina image + * @param RwdOption $option empty image size options. + * @param int|bool $retina_multiplier retina multiplier for retina size. + * @param array $meta_data image attachment WP metadata. * * @return string */ - public function dummy_src( $options, $retina = false, $meta_data = false ) { + public function dummy_source( $option, $retina_multiplier = false, $meta_data = false ) { - $sizename = $options->key; + $sizename = $option->key; - if ( $options->size->h == 9999 ) { - $size = ( $retina ) ? ( $options->size->w * 2 ) . 'x' . ( $options->size->w * 2 ) : $options->size->w . 'x' . $options->size->w; - } elseif ( $options->size->w == 9999 ) { - $size = ( $retina ) ? ( $options->size->h * 2 ) . 'x' . ( $options->size->h * 2 ) : $options->size->h . 'x' . $options->size->h; - } else { - $size = ( $retina ) ? $options->size->w * 2 : $options->size->w . 'x' . ( $retina ) ? $options->size->h * 2 : $options->size->h; + $w = $option->size->w; + $h = $option->size->h; + + $ratio = null; + if ( ! empty( $meta_data['width'] ) && ! empty( $meta_data['height'] ) ) { + $ratio = $meta_data['width'] / $meta_data['height']; } - if ( $meta_data ) { - $dirname = _wp_get_attachment_relative_path( $meta_data['file'] ); - if ( $dirname ) { - $dirname = trailingslashit( $dirname ); - } - $upload_dir = wp_get_upload_dir(); - $image_basedir = trailingslashit( $upload_dir['basedir'] ) . $dirname; - if ( ! file_exists( $image_basedir . $meta_data['sizes'][ $sizename ]['file'] ) ) { - return $sizename . '-' . $size . '.png'; - } else { - return $meta_data['sizes'][ $sizename ]['file']; - } - } else { - return $sizename . '-' . $size . '.png'; + if ( is_null( $h ) || 9999 === $h ) { + $h = $ratio ? floor( $w / $ratio ) : $w; + } elseif ( is_null( $w ) || 9999 === $w ) { + $w = $ratio ? floor( $h * $ratio ) : $h; } + if ( $retina_multiplier ) { + $w *= $retina_multiplier; + $h *= $retina_multiplier; + } + + $color = substr( md5( "{$meta_data['file']}-$w-$h" ), 0, 6 ); + $dummy_url = "http://via.placeholder.com/{$w}x{$h}/$color"; + + return [ + 'file' => $dummy_url, + 'width' => $w, + 'height' => $h, + ]; } /** @@ -577,9 +597,14 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr $image_baseurl = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . image_get_intermediate_size( $attach_id, $key )['path']; if ( ! file_exists( $image_baseurl ) || ! isset( $meta_data['sizes'][ $key ]['rwd_width'] ) - || $meta_data['sizes'][ $key ]['rwd_width'] !== $width || $meta_data['sizes'][ $key ]['rwd_height'] !== $height - || ( 0 !== strcmp( $crop_str, $meta_data['sizes'][ $key ]['crop'] ) ) + || $meta_data['sizes'][ $key ]['rwd_width'] !== $width || $meta_data['sizes'][ $key ]['rwd_height'] !== $height + || ( 0 !== strcmp( $crop_str, $meta_data['sizes'][ $key ]['crop'] ) ) ) { + // in dummy mode we do not resize anything. + if ( JRI_DUMMY_IMAGE ) { + return $meta_data; + } + // Get WP Image Editor Instance. $image_path = get_attached_file( $attach_id ); $image_editor = wp_get_image_editor( $image_path ); @@ -596,8 +621,8 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr // We taked resized image only if we sure that resize was successful and resized dimensions are correct. // - if original image is bigger than resized copy - resize was successful. if ( ( $meta_data['width'] > $resize_sizes['width'] && $meta_data['height'] > $resize_sizes['height'] ) - // - if crop enabled and resized image match the requested size - resize was successful. - || ( ! empty( $crop ) && $resize_sizes['width'] === $width && $resize_sizes['height'] === $height ) + // - if crop enabled and resized image match the requested size - resize was successful. + || ( ! empty( $crop ) && $resize_sizes['width'] === $width && $resize_sizes['height'] === $height ) ) { // WP Image Editor save image. $image_editor->save(); @@ -630,7 +655,7 @@ public function resize_image( $attach_id, $meta_data, $key, $width, $height, $cr $this->set_attachment_metadata( $attach_id, $meta_data ); // update metadata. wp_update_attachment_metadata( $attach_id, $meta_data ); - // update JIO attachment status + // update JIO attachment status. update_post_meta( $attach_id, '_just_img_opt_status', self::STATUS_IN_QUEUE ); } } @@ -734,6 +759,23 @@ protected function get_attachment_baseurl( $attachment_id ) { return static::$base_urls[ $attachment_id ]; } + /** + * Generate final file URL. + * + * @param string $baseurl Attachment folder base url. + * @param array $source File sources array. + * + * @return string + */ + protected function get_attachment_url( $baseurl, $source ) { + $url = $source['file']; + + if ( ! preg_match( '/^http/', $url ) ) { + $url = $baseurl . $url; + } + return $url; + } + /** * Alias for global variable to simlify code. * From f236953ed27771262e12aa8880a1c89bd6c7e57f Mon Sep 17 00:00:00 2001 From: Alex Prokopenko Date: Wed, 27 Jun 2018 19:59:08 +0300 Subject: [PATCH 7/9] Dummy images feature versioning --- just-responsive-images.php | 4 ++-- readme.txt | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/just-responsive-images.php b/just-responsive-images.php index f207be2..81daf0c 100644 --- a/just-responsive-images.php +++ b/just-responsive-images.php @@ -4,7 +4,7 @@ Plugin Name: Just Responsive Images Description: Providing full control to set your own responsive image properties for WordPress 4.4+, the ability to use the <picture> tag, auto-generate image backgrounds and supports retina images. Tags: responsive post thumbnail, post thumbnail as background, retina support, retina image, retina post thumbnail, responsive post attachment, responsive images, responsive attachments, post thumbnails, media -Version: 1.5.1 +Version: 1.6.0 Author: JustCoded / Alex Prokopenko Author URI: http://justcoded.com/ License: GPL3 @@ -49,7 +49,7 @@ class JustResponsiveImages extends core\Singleton { protected function __construct() { // init plugin name and version. self::$plugin_name = __( 'Just Responsive Images', JustResponsiveImages::TEXTDOMAIN ); - self::$version = '1.501'; + self::$version = '1.600'; // init features, which this plugin is created for. new components\Maintenance(); diff --git a/readme.txt b/readme.txt index a5e1fef..5364ef4 100644 --- a/readme.txt +++ b/readme.txt @@ -6,7 +6,7 @@ Tags: responsive post thumbnail, post thumbnail as background, retina support, r Author: JustCoded / Alex Prokopenko Author URI: http://justcoded.com/ Requires at least: 4.5 -Tested up to: 4.9.4 +Tested up to: 4.9.6 Requires PHP: >=5.6 License: GPL3 Stable tag: trunk @@ -40,6 +40,14 @@ Our plugin resize images ONLY when you open the page with an image, printed with In version 1.2 default RWD set background options updated to mobile-first strategy (from desktop-first). If you use nested rules from RWD set you should update your main size background option to have @media query with `min-width` rule. += DEV Mode = + +In DEV mode plugin does not resize any images and simply use placeholder images. This can be used to reduce disk space, while you develop and configure our plugin to match all required screen sizes. + +To enable it you need to define new constant in your wp-config.php file: + +`define( 'JRI_DUMMY_IMAGE', true );` + == Installation == 1. Download, unzip and upload to your WordPress plugins directory @@ -82,6 +90,8 @@ There are no any special upgrade instructions for version 1.0 - 1.3 To upgrade remove the old plugin folder. After than follow the installation steps 1-2. == Changelog == += Version 1.6.0 - 27 June 2018 = + * Dev mode with placeholder images instead of real images in case correct sizes are missing. = Version 1.5.1 - 3 April 2018 = * Added compatibility with Crop Images plugin * Fix main editor content responsive images (it was broken after some WP update) From 0015dea55b9ecc3e71b40d660546f206b9f1e14f Mon Sep 17 00:00:00 2001 From: Alex Prokopenko Date: Wed, 27 Jun 2018 20:09:57 +0300 Subject: [PATCH 8/9] Cleanup wrong merged code in dummy images feature --- models/RwdImage.php | 142 ++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 77 deletions(-) diff --git a/models/RwdImage.php b/models/RwdImage.php index 5878055..ddde3d2 100644 --- a/models/RwdImage.php +++ b/models/RwdImage.php @@ -150,29 +150,26 @@ public function picture( $size, $attributes = array() ) { foreach ( $this->rwd_set->options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->picture ) ) { - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { - $src = $this->dummy_source( $option ); - } else { - continue; - } - } else { - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + continue; + } - $src = array( - $this->get_attachment_url( $baseurl, $sources[ $subkey ] ), - ); - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src[] = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ) - . ' ' . $retina_descriptor; - } + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + + $src = array( + $this->get_attachment_url( $baseurl, $sources[ $subkey ] ), + ); + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { + $src[] = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ) + . ' ' . $retina_descriptor; } } } + $tokens = array( '{src}' => esc_attr( implode( ', ', $src ) ), '{alt}' => $attr['alt'], @@ -227,32 +224,29 @@ public function img( $size, $attributes = array() ) { // generation of responsive sizes. foreach ( $this->rwd_set->options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->srcset ) ) { - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { - $src = $this->dummy_source( $option ); - } else { - continue; - } - } else { - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); + continue; + } - $tokens = array( - '{src}' => esc_attr( $this->get_attachment_url( $baseurl, $sources[ $subkey ] ) ), - '{w}' => $meta_data['sizes'][ $option->key ]['width'], - ); + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { - $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; - $srcset[] = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ) - . ' ' . $retina_width . 'w'; - } + $tokens = array( + '{src}' => esc_attr( $this->get_attachment_url( $baseurl, $sources[ $subkey ] ) ), + '{w}' => $meta_data['sizes'][ $option->key ]['width'], + ); + + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + if ( ! empty( $meta_data['sizes'][ $retina_image_size ]['width'] ) ) { + $retina_width = $meta_data['sizes'][ $retina_image_size ]['width']; + $srcset[] = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ) + . ' ' . $retina_width . 'w'; } } } + $src = $tokens['{src}']; $srcset[] = strtr( "{src} $option->srcset", $tokens ); if ( $option->sizes ) { @@ -305,47 +299,41 @@ public function background( $selector, $size ) { // generation of responsive sizes. foreach ( $rwd_options as $subkey => $option ) { if ( ! isset( $sources[ $subkey ] ) || is_null( $option->srcset ) ) { - if ( defined( 'JRI_DUMMY_IMAGE' ) && JRI_DUMMY_IMAGE ) { - $src = $this->dummy_source( $option ); - } else { - continue; - } - } else { - $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); - $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); + continue; + } + $baseurl = $this->get_attachment_baseurl( $sources[ $subkey ]['attachment_id'] ); + $meta_data = $this->get_attachment_metadata( $sources[ $subkey ]['attachment_id'] ); - $src = $this->get_attachment_url( $baseurl, $sources[ $subkey ] ); - $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); + $src = $this->get_attachment_url( $baseurl, $sources[ $subkey ] ); + $media = str_replace( '{w}', $meta_data['sizes'][ $option->key ]['width'], $option->bg ); - if ( ! isset( $rwd_background_styles[ $media ] ) ) { - $rwd_background_styles[ $media ] = array(); - } - $rwd_background_styles[ $media ][ $selector ] = "$selector{background-image:url('$src');}"; - - // get retina sources. - if ( $option->retina_options ) { - foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { - // Check media pixel and media resolution dpi. - $media_pixel_ration = ( $multiplier < 2.5 ? 1.5 : 2.5 ); - $media_resolution = ( $multiplier < 2.5 ? '144dpi' : '192dpi' ); - - $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); - - if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { - $src_retina = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ); - $media_retina = strtr( $option->bg_retina, array( - '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", - '{min_res}' => "(min-resolution : {$media_resolution})", - ) ); - if ( ! isset( $rwd_background_styles[ $media_retina ] ) ) { - $rwd_background_styles[ $media_retina ] = array(); - } - $rwd_background_styles[ $media_retina ][ $selector ] = "$selector{background-image:url('$src_retina');}"; + if ( ! isset( $rwd_background_styles[ $media ] ) ) { + $rwd_background_styles[ $media ] = array(); + } + $rwd_background_styles[ $media ][ $selector ] = "$selector{background-image:url('$src');}"; + + // get retina sources. + if ( $option->retina_options ) { + foreach ( $option->retina_options as $retina_descriptor => $multiplier ) { + // Check media pixel and media resolution dpi. + $media_pixel_ration = ( $multiplier < 2.5 ? 1.5 : 2.5 ); + $media_resolution = ( $multiplier < 2.5 ? '144dpi' : '192dpi' ); + + $retina_image_size = ImageSize::get_retina_key( $option->key, $retina_descriptor ); + + if ( ! empty( $meta_data['sizes'][ $retina_image_size ] ) ) { + $src_retina = $this->get_attachment_url( $baseurl, $meta_data['sizes'][ $retina_image_size ] ); + $media_retina = strtr( $option->bg_retina, array( + '{dpr}' => "(-webkit-min-device-pixel-ratio:{$media_pixel_ration})", + '{min_res}' => "(min-resolution : {$media_resolution})", + ) ); + if ( ! isset( $rwd_background_styles[ $media_retina ] ) ) { + $rwd_background_styles[ $media_retina ] = array(); } + $rwd_background_styles[ $media_retina ][ $selector ] = "$selector{background-image:url('$src_retina');}"; } - } // End if(). - - } + } + } // End if(). } // End foreach(). } // End if(). From cbb8edc251b04c8d9f765c0f6b030e8aabc5ce8a Mon Sep 17 00:00:00 2001 From: Alex Prokopenko Date: Wed, 27 Jun 2018 20:12:56 +0300 Subject: [PATCH 9/9] Dummy image retina size existence wrong check --- models/RwdImage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/RwdImage.php b/models/RwdImage.php index ddde3d2..aaaa36e 100644 --- a/models/RwdImage.php +++ b/models/RwdImage.php @@ -489,7 +489,7 @@ public function get_set_sources() { $option->size->crop ); - if ( JRI_DUMMY_IMAGE && empty( $dummy_sizes[ $retina_image_size ]['file'] ) ) { + if ( JRI_DUMMY_IMAGE && empty( $meta_data['sizes'][ $retina_image_size ]['file'] ) ) { $dummy_sizes[ $retina_image_size ] = $this->dummy_source( $option, $multiplier, $meta_data ); } } @@ -514,7 +514,7 @@ public function get_set_sources() { $sources[ $subkey ]['attachment_id'] = $attachment->ID; } // End foreach(). - // cache all dummy retina sizes. + // cache all dummy retina sizes to get correct width/height options for retina. if ( JRI_DUMMY_IMAGE ) { foreach ( $dummy_meta as $attachment_id => $meta_data ) { $this->set_attachment_metadata( $attachment_id, $meta_data );