• Resolved Jose Vega

    (@josevega)


    Hi, I tested this plugin on a dev site with photon enabled and it doesnt work by default because jetpack modifies the output of wp_get_attachment_image_src().

    So I modified that part of your plugin to make it work with jetpack and I share it here in case you want to add it to your plugin:

    File: wp-tevko-responsive-images.php – function tevkori_get_src_sizes();

    function tevkori_get_src_sizes( $id, $size ) {
    	$arr = array();
    
    	// See which image is being returned and bail if none is found
    	if ( ! $image = wp_get_attachment_image_src( $id, $size ) ) {
    		return false;
    	};
    
    	// break image data into url, width, and height
    	list( $img_url, $img_width, $img_height ) = $image;
    
    	// Jetpack compatibility
    	if( preg_match( '/[\d]{1}\.wp\.com/i', $img_url) && strpos( $img_url, 'resize=') ){
    
    		$parsed_url = @parse_url( $img_url );
    
    		if ( ! $parsed_url )
    			return false;
    
    		// Parse URL and ensure needed keys exist, since the array returned by <code>parse_url</code> only includes the URL components it finds.
    		$url_info = wp_parse_args( $parsed_url, array(
    			'query' => null,
    		) );
    		$vg_size = explode( '%2C', str_replace('resize=', '', $url_info['query']));
    		$img_width = $vg_size[0];
    		$img_height = $vg_size[1];
    	}
    
    	// image meta
    	$image_meta = wp_get_attachment_metadata( $id );
    
    	// default sizes
    	$default_sizes = $image_meta['sizes'];
    
    	// add full size to the default_sizes array
    	$default_sizes['full'] = array(
    		'width' 	=> $image_meta['width'],
    		'height'	=> $image_meta['height'],
    		'file'		=> $image_meta['file']
    	);
    
    	// set ratio (rounded to hundredths)
    	$ratio = round( ($img_width / $img_height), 2);
    
    	// Remove any hard-crops
    	// Jose: Why are we removing hard-crops?
    	/*foreach ( $default_sizes as $key => $image_size ) {
    		$crop_ratio = round( ($image_size['width'] / $image_size['height']), 2 );
    
    		if( $crop_ratio !== $ratio ) {
    			unset( $default_sizes[$key] );
    		}
    	}*/
    
    	// No sizes? Checkout early
    	if( ! $default_sizes )
    	return false;
    
    	// Loop through each size we know should exist
    	foreach( $default_sizes as $key => $size ) {
    
    		// Reference the size directly by it's pixel dimension
    		$image_src = wp_get_attachment_image_src( $id, $key );
    		$arr[] = $image_src[0] . ' ' . $size['width'] .'w';
    	}
    
    	return 'srcset="' . implode( ', ', $arr ) . '"';
    }

    At the moment I¨m using my version of your plugin. I would appreciate if you let me know if you decide to add this to your plugin so I can continue using yours.

    I commented out the part of the code where you remove the hard-crops just as a personal preference.

    https://www.ads-software.com/plugins/ricg-responsive-images/

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Jetpack photon compatibility’ is closed to new replies.