Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function catch_that_image ($post_id=0, $width=0, $height=0, $img_script='') {
- global $wpdb;
- if($post_id > 0) {
- // select the post content from the db
- $sql = 'SELECT post_content FROM ' . $wpdb->posts . ' WHERE id = ' . $wpdb->escape($post_id);
- $row = $wpdb->get_row($sql);
- $the_content = $row->post_content;
- if(strlen($the_content)) {
- // use regex to find the src of the image
- preg_match("/<img src\=('|\")(.*)('|\") .*( |)\/>/", $the_content, $matches);
- if(!$matches) {
- preg_match("/<img class\=\".*\" title\=\".*\" src\=('|\")(.*)('|\") .*( |)\/>/U", $the_content, $matches);
- }
- $the_image = '';
- $the_image_src = $matches[2];
- $frags = preg_split("/(\"|')/", $the_image_src);
- if(count($frags)) {
- $the_image_src = $frags[0];
- }
- // if src found, then create a new img tag
- if(strlen($the_image_src)) {
- if(strlen($img_script)) {
- // if the src starts with http/https, then strip out server name
- if(preg_match("/^(http(|s):\/\/)/", $the_image_src)) {
- $the_image_src = preg_replace("/^(http(|s):\/\/)/", '', $the_image_src);
- $frags = split("\/", $the_image_src);
- array_shift($frags);
- $the_image_src = '/' . join("/", $frags);
- }
- $the_image = '<img alt="" src="' . $img_script . $the_image_src . '" />';
- }
- else {
- $the_image = '<img alt="" src="' . $the_image_src . '" width="' . $width . '" height="' . $height . '" />';
- }
- }
- return $the_image;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement