| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | function upload_image($form_files, $name, $id, $id_image,$path_prefix, $dest_path, $thumb=false, $dest_thumb_path=NULL, $thumb_bg_color = array(255,255,255)) |
|---|
| 26 | { |
|---|
| 27 | global $db; |
|---|
| 28 | if($form_files) |
|---|
| 29 | { |
|---|
| 30 | $tmp_file = $form_files[$name]['tmp_name']; |
|---|
| 31 | list($width, $height, $type, $attr) = getimagesize($tmp_file); |
|---|
| 32 | switch($type) |
|---|
| 33 | { |
|---|
| 34 | case 3: |
|---|
| 35 | $src = imagecreatefrompng($tmp_file); |
|---|
| 36 | $ext = "png"; |
|---|
| 37 | break; |
|---|
| 38 | case 2: |
|---|
| 39 | $src = imagecreatefromjpeg($tmp_file); |
|---|
| 40 | $ext = "jpeg"; |
|---|
| 41 | break; |
|---|
| 42 | case 1: |
|---|
| 43 | $src = imagecreatefromgif($tmp_file); |
|---|
| 44 | $ext = "gif"; |
|---|
| 45 | break; |
|---|
| 46 | default: |
|---|
| 47 | return 1; |
|---|
| 48 | } |
|---|
| 49 | if($src) |
|---|
| 50 | { |
|---|
| 51 | $elmt[0] = substr($id, 0, 2); |
|---|
| 52 | $elmt[1] = substr($id, 2, 2); |
|---|
| 53 | if($id_image) |
|---|
| 54 | $image_path = $path_prefix.'/'.$dest_path.'/'.$elmt[0].'/'.$elmt[1].'/'.$id_image.'.png'; |
|---|
| 55 | else |
|---|
| 56 | $image_path = $path_prefix.'/'.$dest_path.'/'.$elmt[0].'/'.$elmt[1].'/'.$id.'.png'; |
|---|
| 57 | |
|---|
| 58 | @mkdir($path_prefix.'/'.$dest_path.'/'.$elmt[0]); |
|---|
| 59 | @mkdir($path_prefix.'/'.$dest_path.'/'.$elmt[0].'/'.$elmt[1]); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | if($thumb) |
|---|
| 63 | { |
|---|
| 64 | if($id_image) |
|---|
| 65 | $thumb_path = $path_prefix.'/'.$dest_thumb_path.'/'.$elmt[0].'/'.$elmt[1].'/'.$id_image.'.png'; |
|---|
| 66 | else |
|---|
| 67 | $thumb_path = $path_prefix.'/'.$dest_thumb_path.'/'.$elmt[0].'/'.$elmt[1].'/'.$id.'.png'; |
|---|
| 68 | |
|---|
| 69 | @mkdir($path_prefix.'/'.$dest_thumb_path.'/'.$elmt[0]); |
|---|
| 70 | @mkdir($path_prefix.'/'.$dest_thumb_path.'/'.$elmt[0].'/'.$elmt[1]); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | $wh_ratio = $width/$height; |
|---|
| 74 | if($wh_ratio>(128/160)) |
|---|
| 75 | { |
|---|
| 76 | $dest_width = 128; |
|---|
| 77 | $dest_height = floor($height*128/$width); |
|---|
| 78 | } |
|---|
| 79 | else |
|---|
| 80 | { |
|---|
| 81 | $dest_width = floor($width*160/$height); |
|---|
| 82 | $dest_height = 160; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if($thumb) |
|---|
| 86 | { |
|---|
| 87 | $thumb_width = floor($dest_width/2); |
|---|
| 88 | $thumb_height = floor($dest_height/2); |
|---|
| 89 | } |
|---|
| 90 | error_log("w:$width -> $dest_width / h:$height -> $dest_height"); |
|---|
| 91 | |
|---|
| 92 | $img = imagecreatetruecolor ($dest_width, $dest_height); |
|---|
| 93 | if($thumb) $thumb = imagecreatetruecolor ($thumb_width, $thumb_height); |
|---|
| 94 | $color_white = imagecolorallocate ( $img, 255,255,255); |
|---|
| 95 | $color_black_alpha = imagecolorallocatealpha ( $img, 0,0,0, 40); |
|---|
| 96 | $color_white_alpha = imagecolorallocatealpha ( $img, 255,255,255, 40); |
|---|
| 97 | $color_white_alpha2 = imagecolorallocatealpha ( $img, 255,255,255, 96); |
|---|
| 98 | $color_white_alpha3 = imagecolorallocatealpha ( $img, 255,255,255, 112); |
|---|
| 99 | $color_black = imagecolorallocate ( $img, 0,0,0); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | $color_bg = imagecolorallocate ( $img, $thumb_bg_color[0], $thumb_bg_color[1], $thumb_bg_color[2]); |
|---|
| 103 | $color_bg_alpha = imagecolorallocatealpha ( $img, $thumb_bg_color[0], $thumb_bg_color[1], $thumb_bg_color[2], 40); |
|---|
| 104 | $color_bg_alpha2 = imagecolorallocatealpha ( $img, $thumb_bg_color[0], $thumb_bg_color[1], $thumb_bg_color[2], 96); |
|---|
| 105 | $color_bg_alpha3 = imagecolorallocatealpha ( $img, $thumb_bg_color[0], $thumb_bg_color[1], $thumb_bg_color[2], 112); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | imagefilledrectangle($img, 0,0, $dest_width, $dest_height,$color_white); |
|---|
| 109 | if($thumb) imagefilledrectangle($thumb, 0,0, $thumb_width, $thumb_height,$color_bg); |
|---|
| 110 | |
|---|
| 111 | imagecopyresampled($img, $src, 0,0, 0,0, $dest_width,$dest_height, $width,$height); |
|---|
| 112 | if($thumb) imagecopyresampled($thumb, $src, 0,0, 0,0, $thumb_width, $thumb_height, $width,$height); |
|---|
| 113 | |
|---|
| 114 | imagerectangle($img, 0,0, $dest_width-1, $dest_height-1,$color_white_alpha); |
|---|
| 115 | imagerectangle($img, 1,1, $dest_width-2, $dest_height-2,$color_white_alpha2); |
|---|
| 116 | imagerectangle($img, 2,2, $dest_width-3, $dest_height-3,$color_white_alpha3); |
|---|
| 117 | |
|---|
| 118 | if($thumb) |
|---|
| 119 | { |
|---|
| 120 | imagerectangle($thumb, 0,0, $thumb_width-1, $thumb_height-1,$color_white_alpha); |
|---|
| 121 | imagerectangle($thumb, 1,1, $thumb_width-2, $thumb_height-2,$color_white_alpha2); |
|---|
| 122 | imagerectangle($thumb, 2,2, $thumb_width-3, $thumb_height-3,$color_white_alpha3); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | imagepng($img, $image_path); |
|---|
| 126 | if($thumb) imagepng($thumb, $thumb_path); |
|---|
| 127 | |
|---|
| 128 | $db->query('UPDATE user SET photo=? WHERE id=?', array('y', $id)); |
|---|
| 129 | $db->query('UPDATE cache_user SET photo=? WHERE id=?', array('y', $id)); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | $text = 'http://'.$config['domain'].'/profile/'.$id; |
|---|
| 133 | if($thumb) |
|---|
| 134 | $file = $thumb_path; |
|---|
| 135 | else |
|---|
| 136 | $file = $image_path; |
|---|
| 137 | |
|---|
| 138 | $hdrs = array( |
|---|
| 139 | 'From' => 'moderator-image@dotnode.net', |
|---|
| 140 | 'Subject' => '[dotnode-bot] New image '.$image_path |
|---|
| 141 | ); |
|---|
| 142 | |
|---|
| 143 | $mime =& Mail::factory('mime', "\n"); |
|---|
| 144 | |
|---|
| 145 | $mime->setTXTBody($text); |
|---|
| 146 | $mime->addAttachment($file, "image/png"); |
|---|
| 147 | |
|---|
| 148 | $body = $mime->get(); |
|---|
| 149 | $hdrs = $mime->headers($hdrs); |
|---|
| 150 | |
|---|
| 151 | $mail =& Mail::factory('mail'); |
|---|
| 152 | $mail->send('moderation-image@dotnode.net', $hdrs, $body); |
|---|
| 153 | |
|---|
| 154 | } |
|---|
| 155 | else |
|---|
| 156 | { |
|---|
| 157 | return 2; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | } |
|---|
| 162 | else |
|---|
| 163 | { |
|---|
| 164 | return 3; |
|---|
| 165 | } |
|---|
| 166 | return $image_path; |
|---|
| 167 | } |
|---|
| 168 | ?> |
|---|