Advertisement
Guest User

Untitled

a guest
Feb 10th, 2012
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. <?php
  2. function catch_that_image ($post_id=0, $width=0, $height=0, $img_script='') {
  3. global $wpdb;
  4. if($post_id > 0) {
  5.  
  6. // select the post content from the db
  7.  
  8. $sql = 'SELECT post_content FROM ' . $wpdb->posts . ' WHERE id = ' . $wpdb->escape($post_id);
  9. $row = $wpdb->get_row($sql);
  10. $the_content = $row->post_content;
  11. if(strlen($the_content)) {
  12.  
  13. // use regex to find the src of the image
  14.  
  15. preg_match("/<img src\=('|\")(.*)('|\") .*( |)\/>/", $the_content, $matches);
  16. if(!$matches) {
  17. preg_match("/<img class\=\".*\" title\=\".*\" src\=('|\")(.*)('|\") .*( |)\/>/U", $the_content, $matches);
  18. }
  19. $the_image = '';
  20. $the_image_src = $matches[2];
  21. $frags = preg_split("/(\"|')/", $the_image_src);
  22. if(count($frags)) {
  23. $the_image_src = $frags[0];
  24. }
  25.  
  26. // if src found, then create a new img tag
  27.  
  28. if(strlen($the_image_src)) {
  29. if(strlen($img_script)) {
  30.  
  31. // if the src starts with http/https, then strip out server name
  32.  
  33. if(preg_match("/^(http(|s):\/\/)/", $the_image_src)) {
  34. $the_image_src = preg_replace("/^(http(|s):\/\/)/", '', $the_image_src);
  35. $frags = split("\/", $the_image_src);
  36. array_shift($frags);
  37. $the_image_src = '/' . join("/", $frags);
  38. }
  39. $the_image = '<img alt="" src="' . $img_script . $the_image_src . '" />';
  40. }
  41. else {
  42. $the_image = '<img alt="" src="' . $the_image_src . '" width="' . $width . '" height="' . $height . '" />';
  43. }
  44. }
  45. return $the_image;
  46. }
  47. }
  48. }
  49. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement