Paieška

FDisk (2009-08-07 12:36:32 ) :: Interaktyvus uploadinimas :D bardakas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
ini_set
('error_reporting'E_ALL);
ini_set('display_errors''On');
$max_file_size min(return_bytes(ini_get('post_max_size')),return_bytes(ini_get('upload_max_filesize')),return_bytes(ini_get('memory_limit')));
?>
<? 
if (isset($_FILES) && !empty($_FILES)) {
    foreach (
$_FILES as $file) {
        if (
$file['size'] == 0) continue;
            
$file['name'] = create_th($file);
            
$return $file;
    }
          echo 
json_encode($return);
} elseif (isset(
$_POST) && !empty($_POST['save'])) {
    
//echo json_encode($_POST);
    
var_dump($_POST);
} elseif(!empty(
$_POST['delete']) && !empty($_POST['file'])) {
    echo 
'deleted';
} else {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="MightMedia" />

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css" type="text/css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
    <script src="http://malsup.com/jquery/form/jquery.form.js"></script>
    <script src="http://jquery-checkbox.googlecode.com/svn/trunk/jquery.checkbox.js"></script>

    <title>Untitled 3</title>
    <style type="text/css">
        #picture_upload_response { /*border:1px solid red;*/ }
        #progressbar { color: white; width: 55px; height: 55px; background:url(ajax-loader.gif) center center no-repeat; display: none; }
        #sortable { list-style-type: none; margin: 0; padding: 0; clear: both; }
        #sortable li { cursor: move; border:1px solid gray; background-color: silver; margin: 3px; padding: 1px; float: left; width: 80px; height: 130px; font-size: 4em; text-align: center; }
        .delete_button {position:absolute; right:3px; top:1px; background-color:silver;border:0}
        .comment_input { background-color: #E1E1E1; position: absolute; left: 4px; bottom: 5px; height: 16px; width: 73px; border: 1px solid gray; line-height: 16px;}
        .lock_button { position: absolute; left: 3px; top: 1px; background-color: silver; border: 0; }

        .lock_button {
            cursor:pointer;
            display:inline;
            font-size:16px;
            line-height:16px;
        }
        .lock_button .mark {
            display:inline;
        }
        .lock_button img {
            height:16px;
            vertical-align:middle;
            width:16px;
            background:transparent url(images/lock.png) no-repeat scroll 0 0;
        }
        /*.lock_button img {
        }*/
        /*.lock_button img {
            background-position:0 0;
        }*/
        .lock_button-hover img {
            /*background-position:-24px 0;*/
        }
        .lock_button-checked img {
            background:transparent url(images/lock_unlock.png) no-repeat scroll 0 0;
        }
        .lock_button-checked .lock_button-hover img {
            background:transparent url(images/lock_unlock.png) no-repeat scroll 0 0;
        }
        .lock_button-disabled img {
            background:transparent url(images/lock_disable.png) no-repeat scroll 0 0;
        }
        .lock_button-checked .lock_button-disabled img {
            /*background-position:0 -72px;*/
            background:transparent url(images/lock_unlock_disable.png) no-repeat scroll 0 0;
        }

    </style>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" id="picture_upload">
<div id="picture_upload_response">
    <ul id="sortable">
    </ul>
</div>

<table style=" clear: both;">
    <tr>
        <td>
        <input type="hidden" value="bla" name="betkas" />
        <input type="file" accept="image/jpeg" size="20" name="picture1" />
        </td>
        <td rowspan="2"><div id="progressbar" align="center"></div></td>
    </tr>
    <tr>
        <td><input type="file" accept="image/jpeg" size="20" name="picture2" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <?/*input type="submit" value="Save" name="save" onclick="$('#picture_upload').ajaxFormUnbind();alert($('#picture_upload').serialize());" /*/?>
            <input type="submit" value="Save" name="save" />
        </td>
    </tr>
</table>
<small><?php echo getfilesize($max_file_size); ?></small>
</form>


<script type="text/javascript">
    var count = 1;

//bind ajax to the form
$(function(){
    $('#picture_upload').ajaxForm({
        //target:'#picture_upload_response',   // target element(s) to be updated with server response
        resetForm: true,        // reset the form after successful submit
        //iframe: true,
        success:response,
        beforeSubmit:function(){$("#progressbar").fadeIn('slow');}
    });
});

function response(responseText, statusText) {
    //parse json response
    eval('var z='+responseText);

    if (!z.save) {
        $("#progressbar").fadeOut('slow');
        //alert(responseText);
        $('#picture_upload_response ul').append('<li style="position: relative;" id="sortable_'+count+'"><img src="'+z.name+'" alt="" /><br /><input class="comment_input" type="text" name="komentaras[]" /><input type="hidden" value="'+z.name+'" name="failas[]" /><input type="checkbox" checked="checked" class="lock_button" name="privatus[]" rel="47" value="1" /><a href="#" onclick="$(this).src=\'ajax-loader.gif\';$.ajax({type:\'post\',url:\'<?=$_SERVER['PHP_SELF']?>\',data:\'delete=true&file='+z.name+'\',success:function(html){$(\'#sortable_'+count+'\').remove();}});return false"><img class="delete_button" src="images/no.png" /></a></li>');
        $('#sortable').sortable({ forcePlaceholderSize: true });

        count = count + 1;
        $('input[type=checkbox]').checkbox({
            cls:'lock_button',
            empty: 'images/empty.png'
        });
    } else {
        //saving images
        $("#progressbar").fadeOut('slow');
    }
}

$('input[name^=picture]').change(function(e){
    var fileName = $(this).val();
        var tmpN = fileName.split('.');
        var ext = tmpN.pop();
        ext = ext.toLowerCase();
        if(ext != 'jpg' && ext != 'jpeg' && ext != 'gif' && ext != 'png' && ext != 'bmp'){
            alert('Wrong type');
            return false;
        }
        else {
            //$(this).clone(true).insertAfter(this);
            $(this).submit();

            return false;
        }
});
</script>

</body>
</html>
<? ?>

<?
function create_th($files) {
        
$big_img "tmp/"//Kur bus saugomi didesni paveiksliukai
        
$mini_img "tmp/mini"//Kur bus saugomos miniatiuros

        
$img_thumb_width 250//Mini paveiksliukų dydis

        //Sarašas leidžiamų failų
        
$limitedext = array(".gif"".jpg"".png"".jpeg"".bmp");

        
$file_type $files['type'];
        
$file_name $files['name'];
        
$file_size $files['size'];
        
$file_tmp $files['tmp_name'];

        
//Patikrinam ar failas įkeltas sėkmingai
        
if (!is_uploaded_file($file_tmp)) {
            throw new 
Exception("File: <b>{$file_tmp}</b> was not uploaded");
        }
        else {
            
//gaunamm failo galunę
            
$ext strrchr($file_name'.');
            
$ext strtolower($ext);

            
//Tikrinam ar tinkamas failas
            
if (!in_array($ext$limitedext)) {
                throw new 
Exception("File: <b>{$file_tmp}</b> is wrong type: <b>{$file_type}</b>");
            }

            
//create a random file name
            
$rand_name basename($file_tmp,'.tmp');

            
//the new width variable
            
if ($file_size) {
                if (
$file_type == "image/pjpeg" || $file_type == "image/jpeg") {
                    
$img imagecreatefromjpeg($file_tmp);
                } elseif (
$file_type == "image/x-png" || $file_type == "image/png") {
                    
$img imagecreatefrompng($file_tmp);
                } elseif (
$file_type == "image/gif") {
                    
$img imagecreatefromgif($file_tmp);
                } elseif (
$file_type == "image/bmp") {
                    
$img imagecreatefrombmp($file_tmp);
                }
                
//list the width and height and keep the height ratio.
                
$width imageSX($img);
                
$height imageSY($img);

                
// Build the thumbnail
                
$target_width 75;
                
$target_height 100;
                
$target_ratio $target_width $target_height;

                
$img_ratio $width $height;

                
//calculate the image ratio
                
$imgratio $width $height;

                if (
$target_ratio $img_ratio) {
                    
$new_height $target_height;
                    
$new_width $img_ratio $target_height;
                }
                else {
                    
$new_height $target_width $img_ratio;
                    
$new_width $target_width;
                }

                if (
$new_height $target_height) {
                    
$new_height $target_height;
                }
                if (
$new_width $target_width) {
                    
$new_height $target_width;
                }


                
$new_img ImageCreateTrueColor($target_width$target_height);
                if (!@
imagefilledrectangle($new_img00$target_width 1$target_height 10)) { // Fill the image black
                    
throw new Exception("Wrong GD version");
                }

                if (!@
imagecopyresampled($new_img$img, ($target_width $new_width) / 2, ($target_height $new_height) / 200$new_width$new_height$width$height)) {
                    throw new 
Exception("Wrong GD version");
                }

                
imagejpeg($new_img$mini_img "/" $rand_name $ext95);

                
chmod($mini_img "/" $rand_name $ext0777);
                
ImageDestroy($img);
                
ImageDestroy($new_img);

                
move_uploaded_file($file_tmp$big_img "/" $rand_name $ext);
                
chmod($big_img "/" $rand_name $ext0777);
                return 
$mini_img "/" $rand_name $ext;
            }
        }
    }

function 
return_bytes($val) {
    
$val trim($val);
    if (empty(
$val)) return pow(1024,3);
    
$last strtolower($val{(strlen($val)-1)});
    switch(
$last) {
        
// The 'G' modifier is available since PHP 5.1.0
        
case 'g':
        
$val *= 1024;
        case 
'm':
        
$val *= 1024;
        case 
'k':
        
$val *= 1024;
    }
    return 
$val;
}
function 
getfilesize($size) {
    
//if ($size < 2) return "$size byte";
    
$units = array(' B'' KB'' MB'' GB'' TB');
    for (
$i 0$size 1024$i++) { $size /= 1024; }
    return 
round($size2).$units[$i];
}
?>

Rašyti komentarą

Prašome prisijungti!

Komentarai

# piesklt (2012-02-08 16:24:25 )
avataras
Kad kažko neleidžia įkelt. Spaudi save ar įkelt ir nieko.
# zilwinelis (2010-09-28 15:50:57 )
avataras
nelabai suprantu ką reiškia interaktyvus uploudinimas.
Ir ar imanoma su situ kelti muzika?
# Paulius (2010-04-23 18:40:01 )
avataras
bugų būtų ne daugiau negu 1.30, dar mažiau greičiausiai
# zlotas (2010-04-23 18:25:45 )
avataras
ei nestumk... o 1,33 nesisiusiu juk nepabaigta gali būt bugu ir panašei :/
# Paulius (2010-04-23 17:36:38 )
avataras
1.33 jau buvo, atsibusk...
# zlotas (2010-04-23 17:13:51 )
avataras
Naujoje versijoje? :)
# FDisk (2010-04-22 01:13:12 )
avataras
Tai jau mightmedijoje tai realizavo paulius. Nėr čia ką duot :)
# zlotas (2010-04-21 21:00:04 )
avataras
duok :)
# Paulius (2009-08-07 14:12:16 )
avataras
gera funkcija create_th() :-) su ja pasidariau avatarų sistemą su uploadinimu (be gravatar), įdomu duot atsisiųst
# FDisk (2009-08-07 12:37:02 )
avataras
Nu cia expertams padariau, be paveiksliuku :D