Advertisement
Guest User

Image Caption Easy - max-width support

a guest
Apr 9th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Image Caption Easy
  4. Version: 0.5
  5. Plugin URI: http://imagecaptioneasy.contentspring.com/
  6. Description: Transforms the alt text of an image into a caption which can be controlled through css.
  7. Author: Mark W. B. Ashcroft
  8. Author URI: http://contentspring.com
  9.  
  10. Copyright 2008 Mark W. B. Ashcroft (email : mark [at] contentspring.com)
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21.  
  22. To received a copy of the GNU General Public License write to the
  23. Free Software Foundation, Inc.,
  24. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  25.  
  26. TO DO:
  27. -If have the same image (in single entry) with same caption (alt) title fix bug.
  28. Workaround for user is to use different image captions (change alt in image).
  29.  
  30. LAST MODIFIED: 7 January 2008.
  31. */
  32.  
  33. add_filter('the_content', 'imagecaptioneasy');
  34.  
  35. //Runs Image Caption Easy
  36. function imagecaptioneasy($html) {
  37.  
  38. $c = 0; //set count for each image found.
  39.  
  40. if ( preg_match_all("/<img(.*?)>/is", $html, $img_matches) == true ) { //look through all img tags
  41. foreach ($img_matches[0] as $img_meta) {
  42. //Go through images.
  43. $image_code = $img_matches[0][$c];
  44. $image_code_escape = preg_quote($image_code, '/');
  45.  
  46. $caption_text = ice_extractimageattribute($img_meta, "alt");
  47. if ( $caption_text != false ) {
  48. //Dont add if no alt or ends with file name
  49. $test_alt = substr($caption_text, strlen($caption_text) - 4);
  50. if ( $caption_text == '' ) { $c++; continue; } //false
  51. if ( $caption_text == 'src=' ) { $c++; continue; } //false
  52. if ( $caption_text == 'align=' ) { $c++; continue; } //false
  53. if ( $test_alt == '.jpg' ) { $c++; continue; } //false
  54. if ( $test_alt == '.gif' ) { $c++; continue; } //false
  55. if ( $test_alt == '.png' ) { $c++; continue; } //false
  56. if ( $test_alt == '.JPG' ) { $c++; continue; } //false
  57. if ( $test_alt == '.GIF' ) { $c++; continue; } //false
  58. if ( $test_alt == '.PNG' ) { $c++; continue; } //false
  59. } else {
  60. $c++; continue; //no title, false
  61. }
  62.  
  63. //Image alignment.
  64. $align = ice_extractimageattribute($img_meta, "align");
  65. if ( $align === false ) {
  66. $align = "nowrap"; //set so can be no wrap and centerted.
  67. }
  68. //only float left/right/none is supported by css, so override to be nowrap.
  69. $alignOK = 0;
  70. if ( $align != 'left' ) { $alignOK = 1; }
  71. if ( $align != 'right' ) { $alignOK = 1; }
  72. if ( $align != 'nowrap' ) { $alignOK = 1; }
  73. if ( $alignOK === 0 ) { $align = "nowrap"; }
  74.  
  75. //Image width, so know what size to make div.
  76. $width = ice_extractimageattribute($img_meta, "width");
  77. if ( $width === false ) {
  78. $src = ice_extractimageattribute($img_meta, "src");
  79. $width = ice_getimagewidth($src);
  80. }
  81.  
  82. //Image max-width, so know what size to make div.
  83. $maxwidth = ice_extractimageattribute($img_meta, "max-width");
  84. if ( $maxwidth === false ) {
  85. $src = ice_extractimageattribute($img_meta, "src");
  86. $maxwidth = ice_getimagewidth($src);
  87. }
  88.  
  89. //And alignment of top of entry or not.
  90. $top_of_page = ""; //default is not aligned at top.
  91. if ( $c === 0 ) { //first image
  92. $pos = strpos($html, "<img", 0);
  93. $test_ifattop = substr($html, 0, $pos);
  94. $test_ifattop_strip = strip_tags($test_ifattop);
  95. $test_ifattop_strip = trim($test_ifattop_strip);
  96. if ($test_ifattop_strip == '') {
  97. $top_of_page = "top_";
  98. }
  99. } //end if $c === 0 (is the first image)
  100.  
  101. //If image is hyperlink wraped, extract and insert within the div not encapsulation it.
  102. $find_this_image_regex = "<a([^>]*?)>" . preg_quote($image_code, '/');
  103. if ( preg_match("/$find_this_image_regex/i", $html, $image_lined_res) ) {
  104. //remove hyperlink so can be placed within div latter.
  105. $find_to_replace = preg_quote($image_lined_res[0], '/') . "(.*?)\<\/a\>";
  106. $html = preg_replace("/$find_to_replace/", "$image_code", $html);
  107. $image_code = $image_lined_res[0] . "</a>";
  108. } //end if found image hyperlinked.
  109.  
  110. //Now replace image code with new div-ed image code.
  111. $to_replace_with = "</p><div class=\"imagecaptioneasy imagecaptioneasy_" . $top_of_page.$align . "\" style=\"width:" . $width . "max-width:" . $maxwidth . ";\">" . $image_code . "<br style=\"clear:both\" /><span>" . $caption_text . "</span></div> ";
  112. $html = preg_replace("/$image_code_escape/", "$to_replace_with", $html);
  113.  
  114. $c++;
  115. } //end foreach img
  116. } //end if found any img
  117.  
  118. return $html; //completo return results...
  119.  
  120. } //end function (imagecaptioneasy)
  121.  
  122.  
  123. //Function to extract elements from within the image code, like: alt, align, src etc.
  124. function ice_extractimageattribute($image_text, $attribute_tag) {
  125.  
  126. //Use this function instead of preg_match (REGEX) to make it run quicker.
  127. $posL = strpos($image_text, $attribute_tag);
  128. if ($posL === false) { return false; }
  129. $posL = $posL + strlen($attribute_tag) + 2;
  130. $imageattribute = substr($image_text, $posL);
  131. $posR = strpos($imageattribute, '"');
  132. $imageattribute = substr($imageattribute, 0, $posR);
  133. return $imageattribute;
  134.  
  135. } //end function (ice_extractimageattribute)
  136.  
  137.  
  138. //If image does not have width in html uses this function.
  139. function ice_getimagewidth($url) {
  140.  
  141. if ( strpos($url, get_bloginfo('siteurl')) === false ) { //fix if not absolute url
  142. if ( substr($url, 0, 4) != 'http' ) {
  143. if ( substr($url, 0, 1) == '/' ) {
  144. $url = get_bloginfo('siteurl') . $url;
  145. } else {
  146. $url = get_bloginfo('siteurl') . '/'. $url;
  147. }
  148. }
  149. }
  150.  
  151. error_reporting(E_ERROR);
  152.  
  153. if (function_exists('gd_info')) {
  154.  
  155. if (function_exists('curl_init')) {
  156. // Use cURL
  157. $ch = curl_init();
  158. curl_setopt($ch, CURLOPT_URL,$url);
  159. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  160. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,25); // set to zero for no timeout
  161. $result = curl_exec($ch);
  162. curl_close($ch); //close
  163. } else {
  164. // Use file_get_contents. Requires allow_url_fopen = On in php.ini, else try HTTP_Request.
  165. ini_set('default_socket', 25);
  166. $result = file_get_contents($url);
  167. if ($result == '') {
  168. echo "<strong><big>Image-Caption-Easy Plugin Error!</big></strong>\n";
  169. echo "<p>cURL and allow_url_fopen are both unavailable on your web server. Read image-caption-easy.php file for more information on using HTTP_Request.</p>\n";
  170. //If cURL and allow_url_fopen are both unavalable on your web server then try HTTP_Request or use a different web host.
  171. //For more information on using HTTP_Request: http://pear.php.net/package/HTTP_Request/
  172. }
  173. }
  174.  
  175. if ( imagecreatefromstring($result) == true ) {
  176. $img = imagecreatefromstring($result);
  177. } else {
  178. return ""; //nada cant find images width OR could set 128 which is default thumbnail size if can find image or its width.
  179. }
  180.  
  181. return imagesx($img); //got it...
  182.  
  183. } else { //if no GD try GetImageSize.
  184.  
  185. if (function_exists('getimagesize')) {
  186.  
  187. list ($img_width) = GetImageSize($url);
  188. return $img_width; //got it...
  189.  
  190. } else { //nada, nada
  191. return ""; //nada cant find images width OR could set 128 which is default thumbnail size if can find image or its width.
  192. } //end if getimagesize
  193.  
  194. } //end if not GD
  195.  
  196. } //end function (ice_getimagewidth)
  197.  
  198. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement