Source of trunk/img.php at revision 365 (05/07/2008 10:05:35, 1336 bytes, 68 lines, language: php) [download]:
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | ** Image Fetch and Cache |
| 5 | ** for CodewiseBlog |
| 6 | ** |
| 7 | |
| 8 | ** Copyright (c) 2008 Codewise.org |
| 9 | */ |
| 10 | |
| 11 | function default_icon() |
| 12 | { |
| 13 | header("Content-Type: image/jpeg"); |
| 14 | readfile("default_icon.jpg"); |
| 15 | exit(); |
| 16 | } |
| 17 | |
| 18 | require("settings.php"); |
| 19 | require("l1_mysql.php"); |
| 20 | |
| 21 | $db = new L1_MySQL(SQL_HOST, SQL_USER, SQL_PASS, SQL_DB); |
| 22 | |
| 23 | $q = $db->issue_query("SELECT photo FROM blogs WHERE blogid = ".$db->prepare_value($_GET['blogid'])); |
| 24 | if ($db->num_rows[$q] == 0 || ($img_url = $db->fetch_var($q)) == "") { |
| 25 | default_icon(); |
| 26 | } |
| 27 | |
| 28 | if (!file_exists("cache/" . md5($img_url))) { |
| 29 | |
| 30 | $size = 75; |
| 31 | |
| 32 | |
| 33 | if (!$image) { |
| 34 | default_icon(); |
| 35 | } |
| 36 | |
| 37 | $w = imagesx($image); |
| 38 | $h = imagesy($image); |
| 39 | |
| 40 | if ($w <= $size && $h <= $size) |
| 41 | { |
| 42 | imagejpeg($image, FSPATH . "/cache/" . md5($img_url)); |
| 43 | header("Content-Type: image/jpeg"); |
| 44 | readfile(FSPATH . "/cache/" . md5($img_url)); |
| 45 | exit; |
| 46 | } |
| 47 | |
| 48 | $scalefactor = $size / (($w > $h) ? $w : $h); |
| 49 | |
| 50 | $new_w = $w * $scalefactor; |
| 51 | $new_h = $h * $scalefactor; |
| 52 | |
| 53 | $thumb = imagecreatetruecolor($new_w, $new_h); |
| 54 | |
| 55 | imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_w, $new_h, $w, $h); |
| 56 | |
| 57 | imagejpeg($thumb, FSPATH . "/cache/" . md5($img_url)); |
| 58 | |
| 59 | } |
| 60 | |
| 61 | header("Content-Type: image/jpeg"); |
| 62 | |
| 63 | readfile(FSPATH . "/cache/" . md5($img_url)); |
| 64 | |
| 65 | exit(); |
| 66 | |
| 67 | ?> |
| 68 |