Foto's uploaden; verkleinen en hernoemen

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Herbert Foppen

Herbert Foppen

12/10/2009 23:11:00
Quote Anchor link
Beste mensen,

ik heb onderstaande pagina momenteel online staan om meldingen (brandweer etc.) toe te voegen incl. enkele foto's. Op zich gaat dit allemaal goed. Hij schrijft de foto's netjes weg in /ups en de thumbs in /ups/thumb.

Probleem is een beetje dat bestanden op een gegeven moment dezelfde filenaam kunnen hebben en dan worden oude foto's overschreven. Ik zit er dus aan te denken om bijv. het meldingsid als naam te nemen met een volgnummer. <meldingid><volgnummer>.jpg

Verder moet het zo zijn dat een foto wordt verkleind tot max. 400kb. Is dat te realiseren??

Resume: als ik een foto van 1,5mb van 1250x636 met omschrijving IMG_1256.jpg upload, dan zou het resultaat moeten zijn: <meldingsid><volgnummer>.jpg met afmeting van 800x533 en max 400kb

Ik hoop dat jullie me wat op weg kunnen/willen helpen. Onderstaand de code (als ik 't ergens anders moet plaatsen ivm leesbaarheid graag even die url opgeven...weet die site niet meer)

Herbert

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?
    session_start();
?>

<html style="background-color: buttonface; color: buttontext;">

<head>
<meta http-equiv="content-type" content="text/xml; charset=utf-8" />
<title>Add meldingen</title>

  <!-- calendar stylesheet -->
  <link rel="stylesheet" type="text/css" media="all" href="css/calendar-win2k-cold-1.css" title="win2k-cold-1" />

  <!-- main calendar program -->
  <script type="text/javascript" src="js/calendar.js"></script>

  <!-- language for the calendar -->
  <script type="text/javascript" src="js/lang/calendar-en.js"></script>

  <!-- the following script defines the Calendar.setup helper function, which makes
       adding a calendar a matter of 1 or 2 lines of code. -->
  <script type="text/javascript" src="js/calendar-setup.js"></script>
</head>
<body>

<link href="css/opmaak.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
<!--
body {
    background-color: #F7F7F7;
}
-->
</style>
<?
    if (isset($_SESSION["username"])) {
?>

<?
            $aantal_uploads
= 20;
            $updir = 'ups/'; // Met afsluitende '/'
            $exta = array('.jpg', 'jpeg', '.png', '.gif');
            $mime = array('image/jpeg', 'image/pjepg', 'image/gif', 'image/png');
            $max = 150;
            if ($_SERVER['REQUEST_METHOD'] == "POST")
            {

                //als het formulier gepost is, komt hij hierbinnen terecht. Hier moeten we dus de saving in de db uitvoeren (lkkr zin ;))
                $error = array();
                //print_r($_FILES);
                if (isset($_FILES))
                {
//toegevoegd op 09/04/2008 ivm foutmeldingen
                    //als het goed is worden de fotos iig al geuplaod, u moet de rest nog in de db :-)



                    if ($_FILES['bestand']['error'][0] <> 4)
                    {

                        $arr = array();
                        for ($i = 0; $i <= $aantal_uploads - 1; $i++)
                        {

                            if (strlen(trim($_FILES['bestand']['name'][$i])) > 0)
                            {


                                $ext = strtolower(substr($_FILES['bestand']['name'][$i], strlen($_FILES['bestand']['name'][$i]) - 4));

                                if (in_array($ext, $exta) == true)
                                {

                                    // if(in_array($_FILES['bestand']['type'][$i], $mime)){
                                    if (move_uploaded_file($_FILES['bestand']['tmp_name'][$i], $updir . $_FILES['bestand']['name'][$i]))
                                    {


                                        $bnaam = $updir . $_FILES['bestand']['name'][$i];
                                        list($width, $height, $type) = getimagesize($bnaam);
                                        if ($width > $max or $height > $max)
                                        {

                                            if ($width > $height)
                                            {

                                                $nb = $max;
                                                //$nh = round($max * $height / $width);
                                                $nh = round($height * ($max / $width));


                                            }
elseif ($height > $width)
                                            {

                                                $nh = $max;
                                                //                    $nb = round($max * $width / $width);
                                                $nb = round($width * ($max / $height));
                                            }
else
                                            {
                                                $nb = 150;
                                                $nh = 150;
                                            }


                                            switch ($ext)
                                            {
                                                case
".jpg":
                                                    $new_img = imagecreatefromjpeg($bnaam);
                                                    break;
                                                case
"jpeg":
                                                    $new_img = imagecreatefromjpeg($bnaam);
                                                    break;
                                                case
".gif":
                                                    $new_img = imagecreatefromgif($bnaam);
                                                    break;
                                                case
".png":
                                                    $new_img = imagecreatefrompng($bnaam);
                                                    break;
                                            }


                                            $resized_img = imagecreatetruecolor($nb, $nh);
                                            /*19/04 - om niet de randen rondom de foto's te zetten
                                            $back=ImageColorAllocate($resized_img,255,255,255);
                                            $fore=ImageColorAllocate($resized_img,144,144,144);
                                            imagefilledrectangle ( $resized_img, 0, 0, $nb, $nh, $fore );
                                            imagefilledrectangle ( $resized_img, 1, 1, $nb-2, $nh-2, $back );
                                            */
                                            //                imageline ( resource image, int x1, int y1, int x2, int y2, int color )
                                            #
                                            //the resizing is going on here!
                                            #
                                            //imagecopyresampled($resized_img, $new_img, 5, 5, 0, 0, $nb - 10, $nh - 10, $width, $height);

                                            imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $nb, $nh, $width, $height);

                                            #
                                            //finally, save the image
                                            #

                                            switch ($ext)
                                            {
                                                case
".jpg":
                                                    if (ImageJpeg($resized_img, $updir . 'thumb/' . $_FILES['bestand']['name'][$i]))
                                                    {

                                                        $arr[] = $_FILES['bestand']['name'][$i];
                                                    }
                                                    ;

                                                    break;
                                                case
"jpeg":
                                                    if (ImageJpeg($resized_img, $updir . 'thumb/' . $_FILES['bestand']['name'][$i]))
                                                    {

                                                        $arr[] = $_FILES['bestand']['name'][$i];
                                                    }
                                                    ;

                                                    break;
                                                case
".gif":
                                                    if (Imagegif($resized_img, $updir . 'thumb/' . $_FILES['bestand']['name'][$i]))
                                                    {

                                                        $arr[] = $_FILES['bestand']['name'][$i];
                                                    }
                                                    ;

                                                    break;
                                                case
".png":
                                                    if (Imagepng($resized_img, $updir . 'thumb/' . $_FILES['bestand']['name'][$i]))
                                                    {

                                                        $arr[] = $_FILES['bestand']['name'][$i];
                                                    }
                                                    ;

                                                    break;
                                            }



                                            #
                                            ImageDestroy($resized_img);
                                            ImageDestroy($new_img);

                                        }
else
                                        {
                                            if (copy($bnaam, $updir . 'thumb/' . $_FILES['bestand']['name'][$i]))
                                            {

                                                $arr[] = $_FILES['bestand']['name'][$i];
                                            }
else
                                            {
                                                $error[] = "kan niet kopieren";
                                            }
                                            ;

                                            //kleiner
                                        }
                                        #
                                        chmod($updir . $_FILES['bestand']['name'][$i], 0777);
                                        chmod($updir . 'thumb/' . $_FILES['bestand']['name'][$i], 0777);

                                    }
else
                                    {
                                        $error[] = "Het bestand kan niet worden geupload";
                                    }

                                }
else
                                {
                                    $error[] = 'Vul een afbeelding in. Dit bestand heeft de verkeerde extensie';
                                }

                            }
                        }
//hier kun je evt nog verder controleren of de velden ingevuld zijn en evt aan de $error array fouten toevoegen
                    }
                    if (count($error) == 0)
                    {

                        //echo "we zijn door de tst gekomen of error leeg is";
                        if (count($arr) > 0)
                        {

                            $fotos = implode(",", $arr);
                        }
else
                        {
                            $fotos = null;
                        }

                        print_r($arr);
                        //include ("connect.php");
                        if (isset($arr))
                        {

                            foreach ($arr as $value)
                            {

                                $s = "INSERT INTO fotos (naam) VALUES ('" . $value . "')";
                                mysql_query($s) or die(mysql_error());
                                $foto_id[] = mysql_insert_id(); //hiermee wordt de laatste waarde van de id (het auto_increment_veld) opgehaald
                            }
                        }


                        $datum = $_POST['datum'];
                        list($day, $month, $year) = explode('-', $datum);
                        $datumnieuw = $year . '-' . $month . '-' . $day;
                        $tijdnieuw = date("H:i:s", $datum);
 //                       $datumtijd2 = Date::convert($datum, 'm-d-Y H:i:s', 'Y-m-d H:i:s');
//                        $datumtijd = date("Y-m-d H:i:s", $datum);
                        
                        
                        //$datum = "25-08-1989 23:29:12";

                        $einde = substr($datum,-9); //== laatste gedeelte incl '-'
                        $mm = substr($datum, 3,2);
                        $dd = substr($datum, 0,2);
                        $yy = substr($datum, 6,4);
                        
                        $insertdatum = $yy . "-" . $mm ."-". $dd. $einde;
                        //echo $insertdatum;

                                                                
                        $sql = "INSERT INTO meldingen SET titel='" . $_POST['titel'] .
                            "', omschrijving='" . $_POST['bericht'] . "', omschrijving_kort='" . $_POST['bericht_kort'] . "', korps='" . $_POST['korps'] .
                            "', datumtijd='" .$insertdatum . "', adres='" . $_POST['adres'] .
                            "', plaats='" . $_POST['plaats'] . "', soort='" . $_POST['soort'] .
                            "', door='" . $_SESSION['naam'] . "'";
                        //echo $sql;
                        if (mysql_query($sql) === true)
                        {

                            echo "Het opslaan in db is gelukt.<br />";                          
                            $pid = mysql_insert_id();
                            foreach ($foto_id as $value)
                            {
// hierin wordt voor elke foto (waarvan het database id in de array $foto_id staat) decombinatie van huidige project id en het foto_id in de tabel gezet.
                                mysql_query("INSERT INTO fotos_meldingen (fotoid, projectid) VALUES ('" . $value . "','" . $pid . "')") or die(mysql_error());
                            }
                        }
else
                        {
                            echo "Het opslaan in db is mislukt. Probeer het opnieuw." . mysql_error();
                            
                        }

                        
                        ?>

                        <meta http-equiv="refresh" content="1;URL=main.php">
                        <?php

                        /*                    if(mysql_query($sql) === TRUE){
                        echo "Het opslaan in db is gelukt.<br />";
                        }else{
                        echo "Het opslaan in db is mislukt. Probeer het opnieuw.".mysql_error();
                        }
                        */

                        if (count($arr) > 0)
                        {

                            echo "De volgende afbeeldingen zijn gepload en verkleind: <table><tr>";
                            for ($i = 1; $i <= count($arr); $i++)
                            {

                                echo '<td><a href="' . $updir . $arr[$i - 1] . '"><img border="0" src="' . $updir . 'thumb/' . $arr[$i - 1] . '"></a></td>';
                                if ($i % 2 == 0)
                                {

                                    echo '</tr><tr>';
                                }
                            }

                            echo '</tr></table>';
                        }
                    }
else
                    {
                        echo '<div>De volgende fouten zijn opgetreden:<ul>';
                        if (!is_array($error))
                        {

                            echo "nix ervan"; //waar staat PRintan ma stom ding>? heb ff in mijn firefox getest...
                        } else
                        {
                            echo "toch wel";
                            print_r($error);
                        }

                        foreach ($error as $value)
                            echo '<li>' . $value;
                        echo '</div>'; //ietsje mooier allemaal ;)
                    }
                }
            }
else
            {


?>
<fieldset id="testfieldset"><legend><b>Melding toevoegen</b></legend><form name="addmelding" action="addmeldingen.php" method="post" enctype="multipart/form-data">
        <input type="hidden" id="tekstfield" name="actie" value="newproject"/>
            
<?php
    /*
            <p><label class="formulier" for="tekstfield">Datum:</label><br/><input type="text" id="tekstfield" name="datum"/> (dd-mm-jjjj)</p>
        <hr>
        
        */

?>

                <p><label class="formulier" for="tekstfield">Datum/tijd:</label><br/>
                <table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tr>
                 <td><input type="text" name="datum" id="f_date_c" readonly="1" size="20"/></td>
                 <td><img src="images/cal.gif" id="f_trigger_c" style="cursor: pointer; border: 1px solid #fff;" title="Datumkiezer"
                      onmouseover="this.style.background='#fff';" onmouseout="this.style.background=''" /></td>
                </table>
                
                <script type="text/javascript">
                    Calendar.setup({
                        inputField     :    "f_date_c",     // id of the input field
                        ifFormat       :    "%d-%m-%Y %H:%M:%S",      // format of the input field
                        button         :    "f_trigger_c",  // trigger for the calendar (button ID)
                        showsTime      :    true,
                        align          :    "Tl",           // alignment (defaults to "Bl")
                        singleClick    :    true
                    });
                </script>

<?php /*            <p><label class="formulier" for="tekstfield">Tijd:</label><br/><input type="text" id="tekstfield" name="tijd"/> (uu:mm)</p> */ ?>
            <p><label class="formulier" for="tekstfield">Titel/korte omschrijving:</label><br/><input type="text" id="tekstfield" name="titel" size="100"/></p>
             <p><label class="formulier" for="tekstfield">Adres: (nodig ivm Google Maps)</label><br/><input type="text" id="tekstfield" name="adres" size="50"/> <input type="text" id="tekstfield" name="plaats" size="50"/></p>
            <p><label class="formulier" for="tekstfield">Soort melding:</label><br/>
              <select name="soort" id="tekstfield">
                <option selected="selected" value="Brandweer">Brandweer</option>
                <option value="Ambulance">Ambulance</option>
                <option value="Politie">Politie</option>
                <option value="Lifeliner">Lifeliner</option>
                <option value="K.N.R.M.">K.N.R.M.</option>
                <option value="Algemeen">Algemeen</option>
                <option value="N.v.t.">N.v.t.</option>
                <option value="Overig">Overig</option>
                <option value="----">Overig 2</option>
                <option value="----">Overig 3</option>
                <option value="----">Overig 4</option>
                <option value="----">Overig 5</option>                
              </select>
            </p>
            <p><label class="formulier" for="tekstfield">Korps:</label><br/>
            <select name="korps" id="tekstfield">
                <option selected="selected" value="Elburg">Elburg</option>
                <option value="Elspeet">Elspeet</option>
                <option value="Ermelo">Ermelo</option>
                <option value="Harderwijk">Harderwijk</option>
        <option value="Hattem">Hattem</option>
                <option value="Hattemerbroek">Hattemerbroek</option>
                <option value="Hierden">Hierden</option>
                <option value="Heerde">Heerde</option>
                <option value="Nunspeet">Nunspeet</option>
                <option value="Oldebroek">Oldebroek</option>
                <option value="Overig">Overig</option>
                <option value="Putten">Putten</option>
                <option value="Regio">Regio</option>
                <option value="ASK 't Harde">ASK 't Harde</option>                
                <option value="Wapenveld">Wapenveld</option>
                <option value="N.v.t.">N.v.t.</option>
              </select>

          <p>
          <b>Foto's toevoegen:</b><br /><br />
<?php
                for ($i = 0; $i <= $aantal_uploads - 1; $i++)
                {

                    $k = $i + 1;
                    echo '<label style="display:block; float:left; width: 200px;" for="foto' . $k .
                        '">Foto ' . $k . '</label><input style="float:left;" id="foto' . $k .
                        '" type="file" name="bestand[]"><br clear="all"/>';
                }

?>

          <label class="formulier" for="tekstfield"><b>Korte omschrijving:</b></label><br />
          <?php
                //stukje code voor de WYSIWYG-editor
                $oFCKeditor = new FCKeditor('bericht_kort');
                $oFCKeditor->BasePath = 'FCKeditor/';
                //$oFCKeditor->ToolbarSet = 'MyToolBar' ;
                //$oFCKeditor->Value = '';

                $oFCKeditor->Width = "100%";
                $oFCKeditor->Height = "200";
                $oFCKeditor->Value = $row['bericht_kort'];
                $oFCKeditor->Create();
?>
                    </p>

          <label class="formulier" for="tekstfield"><b>Lange omschrijving:</b></label><br />
          <?php
                //stukje code voor de WYSIWYG-editor
                $oFCKeditor = new FCKeditor('bericht');
                $oFCKeditor->BasePath = 'FCKeditor/';
                //$oFCKeditor->ToolbarSet = 'MyToolBar' ;
                //$oFCKeditor->Value = '';

                $oFCKeditor->Width = "100%";
                $oFCKeditor->Height = "600";
                $oFCKeditor->Value = $row['bericht'];
                $oFCKeditor->Create();
?>
                    </p>
            <p><label class="formulier">&nbsp;</label><input type="submit"  name="button" value="Opslaan" id="knop" />
            </fieldset>
            
        </form>
<?php
    // afsluiten! (rg 23??)
    }
?>


<?php
    //voor controle binnen sessie
    }

    else{
    $text = "U bent niet goed ingelogd, U gaat terug naar de loginpagina";
    echo($text);
?>


        <meta http-equiv="refresh" content="1;URL=index.php">
<?
    }
?>
 
Er zijn nog geen reacties op dit bericht.



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.