WordPress.org

Plugin Directory

Changeset 1584756


Ignore:
Timestamp:
01/30/17 03:11:14 (13 months ago)
Author:
factmaven
Message:

Update to v1.1.0

Location:
specify-image-dimensions
Files:
6 added
2 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • specify-image-dimensions/trunk/readme.txt

    r1542304 r1584756  
    11=== Specify Image Dimensions === 
    22Contributors: factmaven, ethanosullivan, nateallen 
    3 Tags: specify image dimensions, image, img, dimensions, width, height, gtmetrix, yslow, pagespeed, page speed, optimization, performance 
     3Tags: dimensions, gtmetrix, height, image, img, optimization, page speed, pagespeed, performance, specify image dimensions, width, yslow 
    44Requires at least: 1.5.1 
    5 Tested up to: 4.6.1 
    6 Stable tag: 1.0.4 
     5Tested up to: 4.7.2 
     6Stable tag: 1.1.0 
    77License: GPLv3 
    88License URI: http://www.gnu.org/licenses/gpl-3.0.html 
     
    1515= Before = 
    1616` 
    17 <img src="http://example.com/some/image.jpg" title="Some Title" /> 
    18 <img src="http://example.com/some/vector.svg" class="svg" /> 
    19 <img src="http://example.com/some/another-vector.webp" /> 
     17<img src="http://example.com/image.jpg" title="Some Title" /> 
     18<img src="http://example.com/vector.svg" class="svg" /> 
     19<img src="http://example.com/another-vector.webp" /> 
    2020` 
    2121 
    2222= After = 
    2323` 
    24 <img src="http://example.com/some/image.jpg" title"Some Title" width="100" height="25" /> 
    25 <img src="http://example.com/some/vector.svg" class="svg" width="100%" height="auto" /> 
    26 <img src="http://example.com/some/another-vector.webp" width="100%" height="auto" /> 
     24<img src="http://example.com/image.jpg" title"Some Title" width="100" height="25" /> 
     25<img src="http://example.com/vector.svg" class="svg" width="100%" height="auto" /> 
     26<img src="http://example.com/another-vector.webp" width="100%" height="auto" /> 
    2727` 
    2828 
     
    5151 
    5252== Changelog == 
     53= 1.1.0 = 
     54*2016-01-29* 
     55* **Fix**: images with blank dimensions weren't being detected 
     56* Added support for additional [`img` attributes](http://www.w3schools.com/tags/tag_img.asp) 
     57 
    5358=1.0.4 = 
    5459*2016-11-28* 
  • specify-image-dimensions/trunk/specify-image-dimensions.php

    r1542304 r1584756  
    44 * Plugin URI: https://wordpress.org/plugins/specify-image-dimensions/ 
    55 * Description: Automatically specify image dimensions that are missing width and/or height attributes. Helps with website speed tools. 
    6  * Version: 1.0.4 
     6 * Version: 1.1.0 
    77 * Author: Fact Maven 
    88 * Author URI: https://www.factmaven.com 
     
    1313if ( ! defined( 'ABSPATH' ) ) exit; 
    1414 
    15 class Fact_Maven_SID { 
     15class Fact_Maven_Specify_Image_Dimensions { 
    1616 
    1717    public function __construct() { 
     
    2727                return $content; 
    2828            } 
    29  
     29            # Find all image tags 
    3030            preg_match_all( '/<img[^>]+>/i', $content, $images ); 
    3131            # If there are no images, return 
     
    3535 
    3636            foreach ( $images[0] as $image ) { 
    37                 preg_match_all( '/(src|alt|title|class|id|width|height)=("[^"]*")/i', $image, $img ); 
     37                # Match all image attributes 
     38                $attributes = 'src|srcset|longdesc|alt|class|id|usemap|align|border|hspace|vspace|crossorigin|ismap|sizes|width|height'; 
     39                preg_match_all( '/(' . $attributes . ')=("[^"]*")/i', $image, $img ); 
    3840                # If image has a 'src', continue 
    3941                if ( ! in_array( 'src', $img[1] ) ) { 
    4042                    continue; 
    4143                } 
    42                 # If no 'width' or 'height' is available, calculate dimensions 
    43                 if ( ! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ) { 
     44                # If no 'width' or 'height' is available or blank, calculate dimensions 
     45                if ( ! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) || ( in_array( 'width', $img[1] ) && in_array( '""', $img[2] ) ) || ( in_array( 'height', $img[1] ) && in_array( '""', $img[2] ) ) ) { 
     46                    # Split up string of attributes into variables 
     47                    $attributes = explode( '|', $attributes ); 
     48                    foreach ( $attributes as $variable ) { 
     49                        ${$variable} = in_array( $variable, $img[1] ) ? ' ' . $variable . '=' . $img[2][array_search( $variable, $img[1] )] : ''; 
     50                    } 
    4451                    $src = $img[2][array_search( 'src', $img[1] )]; 
    45                     $alt = in_array( 'alt', $img[1] ) ? 'alt=' . $img[2][array_search( 'alt', $img[1] )] : ''; 
    46                     $title = in_array( 'title', $img[1] ) ? 'title=' . $img[2][array_search( 'title', $img[1] )] : ''; 
    47                     $class = in_array( 'class', $img[1] ) ? 'class=' . $img[2][array_search( 'class', $img[1] )] : ''; 
    48                     $id = in_array( 'id', $img[1] ) ? 'id=' . $img[2][array_search( 'id', $img[1] )] : ''; 
     52                    # If image is an SVG/WebP with no dimensions, set specific dimensions 
    4953                    if ( preg_match( '/(.*).svg|.webp/i', $src ) ) { 
    50                         $width = '100%'; 
    51                         $height = 'auto'; 
     54                        if ( ! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) || ( in_array( 'width', $img[1] ) && in_array( '""', $img[2] ) ) || ( in_array( 'height', $img[1] ) && in_array( '""', $img[2] ) ) ) { 
     55                            $width = '100%'; 
     56                            $height = 'auto'; 
     57                        } 
    5258                    } 
     59                    # Else, get accurate width and height attributes 
    5360                    else { 
    54                         list( $width, $height, $type, $attr ) = getimagesize( str_replace( "\"", "" , $src ) ); 
     61                        list( $width, $height ) = getimagesize( str_replace( "\"", "" , $src ) ); 
    5562                    } 
    56  
    57                     $image_tag = sprintf( '<img src=%s %s %s %s %s width="%s" height="%s" />', $src, $alt, $title, $class, $id, $width, $height ); 
    58                     $content = str_replace( $image, $image_tag, $content ); 
     63                    # Recreate the image tag with dimensions set 
     64                    $tag = sprintf( '<img src=%s%s%s%s%s%s%s%s%s%s%s%s%s%s width="%s" height="%s">', $src, $srcset, $longdesc, $alt, $class, $id, $usemap, $align, $border, $hspace, $vspace, $crossorigin, $ismap, $sizes, $width, $height ); 
     65                    $content = str_replace( $image, $tag, $content ); 
    5966                } 
    6067            } 
    61             # Return all image with dimensions set 
     68            # Return all image with dimensions 
    6269            return $content; 
    6370        } ); 
     
    6572} 
    6673# Instantiate the class 
    67 new Fact_Maven_SID(); 
     74new Fact_Maven_Specify_Image_Dimensions(); 
Note: See TracChangeset for help on using the changeset viewer.