Skip to content

Instantly share code, notes, and snippets.

@vajrasar
Created September 27, 2013 10:36
Show Gist options
  • Save vajrasar/6726755 to your computer and use it in GitHub Desktop.
Save vajrasar/6726755 to your computer and use it in GitHub Desktop.
Scenario: You 'only' have 'url' of first image occurring in your 'the_content' and you want to find that image's width/height. Code goes in functions.php
<?php
/*
Here we will find out the WIDTH of 'first' image occuring in our 'the_content'
*/
/*
Step: 1
Using the famous 'catch_that_image' snippet, we will fetch the url of first image
occuring in the content and will pass the same url while calling a function to find
it's WIDTH.
*/
add_action('genesis_entry_header','image_above_t',7); //to place image above title
function image_above_t() {
global $post;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0]; //path of first image occurring in the_content
$img_width = custom_get_img_width($first_img); //pass the path of first image to function which will return image's width
/*
Here will be the putting code which uses $img_width in some manner that we want.
*/
}
/*
Step 2:
This function will determine the WIDTH of our image
Thanks to - @philipnewcomer
*/
function custom_get_img_width($first_img) {
global $wpdb;
$attachment_url = $first_img; //attachment(image) url
$upload_dir_paths = wp_upload_dir(); // Get the upload directory paths
// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {
// If this is the URL of an auto-generated thumbnail, get the URL of the original image
$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );
// Remove the upload path base directory from the attachment URL
$attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );
// Finally, run a custom database query to get the attachment ID from the modified attachment URL
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) ); // to get attachment(image) id
}
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); // returns an array of attachment(image) attributes
$img_width= $image_attributes[1]; //finally the width of image
return $img_width;
}
@mustafaozcaninfo
Copy link

Mustafa Özcan

Kişisel Blog makalelerimi SEO konusundaki deneyimleri paylaştığım blogum.Fırsatları ayağınıza getiren mustafa ozcan blog kişisel temalı yazılar ile gündemde yer alan güncel yazıları sizlere sunma fırsatı ile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment