src/Util/Theme.php
public static function media($path, $size) {
    // Which image size was requested?
    global $_wp_additional_image_sizes;
    $image_size = $_wp_additional_image_sizes[$size];
    
    // Get the path info
    $pathinfo = pathinfo($path);
    $fname = $pathinfo['basename'];
    $fext = $pathinfo['extension'];
    $dir = $pathinfo['dirname'];
    $fdir = realpath(str_replace('//', '/', ABSPATH.$dir)).'/';
    
    // Filename without any size suffix or extension (e.g. without -144x200.jpg)
    $fname_prefix = preg_replace('/(?:-\d+x\d+)?\.'.$fext.'$/i', '', $fname);
    $out_fname = sprintf(
      '%s-%sx%s.%s',
      $fname_prefix,
      $image_size['width'],
      $image_size['height'],
      $fext
    );
    
    // See if the file that we're predicting exists
    // If so, we can avoid a call to the database
    $fpath = $fdir.$out_fname;
    if(file_exists($fpath)) {
      return sprintf(
        '%s/%s',
        $pathinfo['dirname'],
        $out_fname
      );
    }
    
    // Can't find the file? Figure out the correct path from the database
    global $wpdb;
    $guid = site_url().$dir.'/'.$fname_prefix.'.'.$fext;
    $prepared = $wpdb->prepare(
      "SELECT
        pm.meta_value
      FROM $wpdb->posts p
      INNER JOIN $wpdb->postmeta pm
        ON p.ID = pm.post_id
      WHERE p.guid = %s
      AND pm.meta_key = '_wp_attachment_metadata'
      LIMIT 1",
      $guid
    );
    $row = $wpdb->get_row($prepared);
    if(is_object($row)) {
      $meta = unserialize($row->meta_value);
      if(isset($meta['sizes'][$size]['file'])) {
        $meta_fname = $meta['sizes'][$size]['file'];
        return sprintf(
          '%s/%s',
          $pathinfo['dirname'],
          $meta_fname
        );
      }
    }
    
    // Still nothing? Just return the path given
    return $path;
  }
public static function media($path, $size) {
    // Which image size was requested?
    global $_wp_additional_image_sizes;
    $image_size = $_wp_additional_image_sizes[$size];
    
    // Get the path info
    $pathinfo = pathinfo($path);
    $fname = $pathinfo['basename'];
    $fext = $pathinfo['extension'];
    $dir = $pathinfo['dirname'];
    $fdir = realpath(str_replace('//', '/', ABSPATH.$dir)).'/';
    
    // Filename without any size suffix or extension (e.g. without -144x200.jpg)
    $fname_prefix = preg_replace('/(?:-\d+x\d+)?\.'.$fext.'$/i', '', $fname);
    $out_fname = sprintf(
      '%s-%sx%s.%s',
      $fname_prefix,
      $image_size['width'],
      $image_size['height'],
      $fext
    );
    
    // See if the file that we're predicting exists
    // If so, we can avoid a call to the database
    $fpath = $fdir.$out_fname;
    if(file_exists($fpath)) {
      return sprintf(
        '%s/%s',
        $pathinfo['dirname'],
        $out_fname
      );
    }
    
    // Can't find the file? Figure out the correct path from the database
    global $wpdb;
    $guid = site_url().$dir.'/'.$fname_prefix.'.'.$fext;
    $prepared = $wpdb->prepare(
      "SELECT
        pm.meta_value
      FROM $wpdb->posts p
      INNER JOIN $wpdb->postmeta pm
        ON p.ID = pm.post_id
      WHERE p.guid = %s
      AND pm.meta_key = '_wp_attachment_metadata'
      LIMIT 1",
      $guid
    );
    $row = $wpdb->get_row($prepared);
    if(is_object($row)) {
      $meta = unserialize($row->meta_value);
      if(isset($meta['sizes'][$size]['file'])) {
        $meta_fname = $meta['sizes'][$size]['file'];
        return sprintf(
          '%s/%s',
          $pathinfo['dirname'],
          $meta_fname
        );
      }
    }
    
    // Still nothing? Just return the path given
    return $path;
  }
public static function media ( $ path , $ size ) { // Which image size was requested? global $ _wp_additional_image_sizes ; $ image_size = $ _wp_additional_image_sizes [ $ size ] ; // Get the path info $ pathinfo = pathinfo ( $ path ) ; $ fname = $ pathinfo [ 'basename' ] ; $ fext = $ pathinfo [ 'extension' ] ; $ dir = $ pathinfo [ 'dirname' ] ; $ fdir = realpath ( str_replace ( '//' , '/' , ABSPATH . $ dir ) ) . '/' ; // Filename without any size suffix or extension (e.g. without -144x200.jpg) $ fname_prefix = preg_replace ( '/(?:-\d+x\d+)?\.' . $ fext . '$/i' , '' , $ fname ) ; $ out_fname = sprintf ( '%s-%sx%s.%s' , $ fname_prefix , $ image_size [ 'width' ] , $ image_size [ 'height' ] , $ fext ) ; // See if the file that we're predicting exists // If so, we can avoid a call to the database $ fpath = $ fdir . $ out_fname ; if ( file_exists ( $ fpath ) ) { return sprintf ( '%s/%s' , $ pathinfo [ 'dirname' ] , $ out_fname ) ; } // Can't find the file? Figure out the correct path from the database global $ wpdb ; $ guid = site_url ( ) . $ dir . '/' . $ fname_prefix . '.' . $ fext ; $ prepared = $ wpdb -> prepare ( "SELECT
        pm.meta_value
      FROM $wpdb->posts p
      INNER JOIN $wpdb->postmeta pm
        ON p.ID = pm.post_id
      WHERE p.guid = %s
      AND pm.meta_key = '_wp_attachment_metadata'
      LIMIT 1" , $ guid ) ; $ row = $ wpdb -> get_row ( $ prepared ) ; if ( is_object ( $ row ) ) { $ meta = unserialize ( $ row -> meta_value ) ; if ( isset ( $ meta [ 'sizes' ] [ $ size ] [ 'file' ] ) ) { $ meta_fname = $ meta [ 'sizes' ] [ $ size ] [ 'file' ] ; return sprintf ( '%s/%s' , $ pathinfo [ 'dirname' ] , $ meta_fname ) ; } } // Still nothing? Just return the path given return $ path ; }
Get URL for an image in the media library
@param string $path
@param string $size (size keys that you've passed to add_image_size)
@return string Relative URL
Get URL for an image in the media library