Posted By

kamaleao on 08/26/14


Tagged


Versions (?)

IMG.php


 / Published in: PHP
 

The code bellow retrives the first jpg image (img src=****.jpg) from URL with Post ID. How to close the code to prevent "Too many connections" in MySQL Database?

  1. <?
  2. include "header.php";
  3.  
  4. $weblog_url = url("index", "$weblog_info[weblog]");
  5. if(isset($_GET['e_id'])) { $e_id = $_GET['e_id']; } else { header("Location: $weblog_url"); exit(); }
  6. if(mysql_num_rows(mysql_query("SELECT e_id FROM bhost_entries WHERE e_id='$e_id' AND w_id='$weblog_info[w_id]' AND draft='0' $privacy")) != 1) {
  7. if(mysql_num_rows(mysql_query("SELECT e_id FROM bhost_entries WHERE e_id='$e_id' AND w_id='$weblog_info[w_id]' AND draft='0'")) != 0) {
  8. $entry_info = mysql_fetch_assoc(mysql_query("SELECT privacy FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND e_id='$e_id'"));
  9. if($entry_info[privacy] == "2") {
  10. $error = "<b>$entry1</b><br>$entry2 <a href='".url("login", "$weblog_info[weblog]", "", "$e_id", "", "", "&ref=1")."'>$entry3</a>.";
  11. } elseif($entry_info[privacy] == "1") {
  12. $error = "<b>$entry1</b><br>$entry4 <a href='".url("login", "$weblog_info[weblog]", "", "$e_id", "", "", "&ref=1")."'>$entry3</a>.";
  13. } elseif($entry_info[privacy] == "0") {
  14. $error = "<b>$entry1</b><br>$entry5 <a href='".url("login", "$weblog_info[weblog]", "", "$e_id", "", "", "&ref=1")."'>$entry3</a>.";
  15. }
  16. echo $head;
  17.  
  18.  
  19. echo $foot;
  20. exit();
  21. }
  22. header("Location: $weblog_url");
  23. exit();
  24. }
  25. $entry_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_entries WHERE w_id='$weblog_info[w_id]' AND e_id='$e_id'"));
  26.  
  27. // RETRIEVE TEMPLATE
  28. $template = $weblog_info[template];
  29. $template = bh_engine($template);
  30.  
  31.  
  32.  
  33. // CHECK FOR ENTRIES
  34. $entrycount = occurrencecount("<Entry>", "</Entry>", $template, "0");
  35. for($i=0;$i<$entrycount;$i++) {
  36. $insert = "";
  37. $entry = between("<Entry>", "</Entry>", $template);
  38. $entry_author = mysql_fetch_assoc(mysql_query("SELECT username, display_name, avatar FROM bhost_users WHERE u_id='$entry_info[author_u_id]'"));
  39. $numofcomments = mysql_num_rows(mysql_query("SELECT c_id FROM bhost_comments WHERE w_id='$weblog_info[w_id]' AND e_id='$entry_info[e_id]'"));
  40. $numoftrackbacks = mysql_num_rows(mysql_query("SELECT tb_id FROM bhost_trackbacks WHERE w_id='$weblog_info[w_id]' AND e_id='$entry_info[e_id]'"));
  41. $insert .= "<a name='e$entry_info[e_id]'></a>".$entry;
  42.  
  43. $first_img = str_replace("&lt;", "<", $entry_info[contents]);
  44. $first_img = str_replace("&gt;", ">", $first_img);
  45. $first_img = str_replace("&#39;", "'", $first_img);
  46.  
  47.  
  48.  
  49. if($entry_info[ec_id] == 0) {
  50. $ifcategoriesallowedcount = occurrencecount("<IfCategoriesAllowed>", "</IfCategoriesAllowed>", $insert, "0");
  51. for($a=0;$a<$ifcategoriesallowedcount;$a++) {
  52. $insert = str_replace("<IfCategoriesAllowed>".between("<IfCategoriesAllowed>", "</IfCategoriesAllowed>", $insert)."</IfCategoriesAllowed>", "", $insert);
  53. }
  54. $entry_category = "$entry7";
  55. } else {
  56. $entry_cat = mysql_fetch_assoc(mysql_query("SELECT ec_id, name FROM bhost_entrycategories WHERE ec_id='$entry_info[ec_id]' AND w_id='$weblog_info[w_id]'"));
  57. $entry_category = "<a href='".url("index_cat", $weblog_info[weblog], "", "0", "", "", "", "", "$entry_cat[name]")."'>$entry_cat[name]</a>";
  58. }
  59.  
  60.  
  61.  
  62. $template = str_replace("<Entry>".$entry."</Entry>", $insert, $template);
  63.  
  64. }
  65.  
  66.  
  67.  
  68. /////
  69. // Retrieve images on the site
  70. $data = $first_img;
  71. $pattern = "/src=[\"']?([^\"']?.*(png|jpg|gif))[\"']?/i";
  72. preg_match_all($pattern, $data, $images);
  73.  
  74. $imagepath= $images[1][0];
  75. $image=imagecreatefromstring(file_get_contents($imagepath));
  76. // get image height
  77.  
  78. //allocate color for image caption (white)
  79.  
  80. //Add text to image bottom
  81. // put this above any php image generation code:
  82. header("Cache-Control: private, max-age=10800, pre-check=10800");
  83. header("Pragma: private");
  84. header("Expires: " . date(DATE_RFC822,strtotime(" 30 day")));
  85.  
  86. // the browser will send a $_SERVER['HTTP_IF_MODIFIED_SINCE'] if it has a cached copy
  87. if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
  88. // if the browser has a cached version of this image, send 304
  89. header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304);
  90. }
  91. header('Content-Type: image/jpeg');
  92.  
  93.  
  94. //if (!empty($imagepath)) { echo "http://kamaleao.com/botaokamaleao.gif";
  95. //}
  96.  
  97. /////////////////////
  98.  
  99.  
  100. // Get new dimensions
  101. list($width, $height) = getimagesize($imagepath);
  102. $new_width = 150;
  103. $new_height = 150;
  104.  
  105. // Resample
  106. $image_p = imagecreatetruecolor($new_width, $new_height);
  107.  
  108. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  109.  
  110. // Output
  111. imagejpeg($image_p, null, 100);
  112. mysql_close($image_p);
  113.  
  114.  
  115. // OUTPUT WEBLOG
  116.  
  117.  
  118. ?>

Report this snippet  

You need to login to post a comment.