data wordt niet opgeslagen

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Ruud

ruud

18/05/2008 00:07:00
Quote Anchor link
Ik heb het volgende script:

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
<?php

$con
= mysql_connect("localhost","****","****");
if (!$con)
  {

  die('Kan niet verbinden: ' . mysql_error());
  }
mysql_select_db("****", $con);$sql="INSERT INTO ***** (watermarkee, locatie, naam, wattezien, datum, camera)
VALUES
('$target_name','$_POST[locatie]','$_POST[naam]','$_POST[wattezien]','$_POST[datum]','$_POST[camera]')"
;if (!mysql_query($sql,$con))
  {

  die('Fout: ' . mysql_error());
  }



    ob_start();

    $disp_width_max=150;                    // used when displaying watermark choices
    $disp_height_max=80;                    // used when displaying watermark choices
    $edgePadding=0;                        // used when placing the watermark near an edge
    $quality=100;                           // used when generating the final image
    $default_watermark='overlay.png';  // the default image to use if no watermark was chosen
    
    if(isset($_POST['process'])){
        // an image has been posted, let's get to the nitty-gritty
        if(isset($_FILES['watermarkee']) && $_FILES['watermarkee']['error']==0){
        
            // be sure that the other options we need have some kind of value
            if(!isset($_POST['save_as'])) $_POST['save_as']='jpeg';
            if(!isset($_POST['v_position'])) $_POST['v_position']='center';
            if(!isset($_POST['h_position'])) $_POST['h_position']='center';
            if(!isset($_POST['wm_size'])) $_POST['wm_size']='1';
            if(!isset($_POST['watermark'])) $_POST['']=$default_watermark;
        
            // file upload success
            $size=getimagesize($_FILES['watermarkee']['tmp_name']);
            if($size[2]==2 || $size[2]==3){
                // it was a JPEG or PNG image, so we're OK so far
                
                $original=$_FILES['watermarkee']['tmp_name'];
                $target_name=date('YmdHis').'_'.
                    // if you change this regex, be sure to change it in generated-images.php:26
                    preg_replace('`[^a-z0-9-_.]`i','',$_FILES['watermarkee']['name']);
                $target=dirname(__FILE__).'/results/'.$target_name;
                $watermark=dirname(__FILE__).'/watermarks/'.$_POST['watermark'];
                $wmTarget=$watermark.'.tmp';

                $origInfo = getimagesize($original);
                $origWidth = $origInfo[0];
                $origHeight = $origInfo[1];

                $waterMarkInfo = getimagesize($watermark);
                $waterMarkWidth = $waterMarkInfo[0];
                $waterMarkHeight = $waterMarkInfo[1];
        
                // watermark sizing info
                if($_POST['wm_size']=='larger'){
                    $placementX=0;
                    $placementY=0;
                    $_POST['h_position']='center';
                    $_POST['v_position']='center';
                    $waterMarkDestWidth=$waterMarkWidth;
                    $waterMarkDestHeight=$waterMarkHeight;
                    
                    // both of the watermark dimensions need to be 5% more than the original image...
                    // adjust width first.

                    if($waterMarkWidth > $origWidth*1.05 && $waterMarkHeight > $origHeight*1.05){
                        // both are already larger than the original by at least 5%...
                        // we need to make the watermark *smaller* for this one.
                        
                        // where is the largest difference?

                        $wdiff=$waterMarkDestWidth - $origWidth;
                        $hdiff=$waterMarkDestHeight - $origHeight;
                        if($wdiff > $hdiff){
                            // the width has the largest difference - get percentage
                            $sizer=($wdiff/$waterMarkDestWidth)-0.05;
                        }
else{
                            $sizer=($hdiff/$waterMarkDestHeight)-0.05;
                        }

                        $waterMarkDestWidth-=$waterMarkDestWidth * $sizer;
                        $waterMarkDestHeight-=$waterMarkDestHeight * $sizer;
                    }
else{
                        // the watermark will need to be enlarged for this one
                        
                        // where is the largest difference?

                        $wdiff=$origWidth - $waterMarkDestWidth;
                        $hdiff=$origHeight - $waterMarkDestHeight;
                        if($wdiff > $hdiff){
                            // the width has the largest difference - get percentage
                            $sizer=($wdiff/$waterMarkDestWidth)+0.05;
                        }
else{
                            $sizer=($hdiff/$waterMarkDestHeight)+0.05;
                        }

                        $waterMarkDestWidth+=$waterMarkDestWidth * $sizer;
                        $waterMarkDestHeight+=$waterMarkDestHeight * $sizer;
                    }
                }
else{
                    $waterMarkDestWidth=round($origWidth * floatval($_POST['wm_size']));
                    $waterMarkDestHeight=round($origHeight * floatval($_POST['wm_size']));
                    if($_POST['wm_size']==1){
                        $waterMarkDestWidth-=2*$edgePadding;
                        $waterMarkDestHeight-=2*$edgePadding;
                    }
                }


                // OK, we have what size we want the watermark to be, time to scale the watermark image
                resize_png_image($watermark,$waterMarkDestWidth,$waterMarkDestHeight,$wmTarget);
                
                // get the size info for this watermark.
                $wmInfo=getimagesize($wmTarget);
                $waterMarkDestWidth=$wmInfo[0];
                $waterMarkDestHeight=$wmInfo[1];

                $differenceX = $origWidth - $waterMarkDestWidth;
                $differenceY = $origHeight - $waterMarkDestHeight;

                // where to place the watermark?
                switch($_POST['h_position']){
                    // find the X coord for placement
                    case 'left':
                        $placementX = $edgePadding;
                        break;
                    case
'center':
                        $placementX =  round($differenceX / 2);
                        break;
                    case
'right':
                        $placementX = $origWidth - $waterMarkDestWidth - $edgePadding;
                        break;
                }


                switch($_POST['v_position']){
                    // find the Y coord for placement
                    case 'top':
                        $placementY = $edgePadding;
                        break;
                    case
'center':
                        $placementY =  round($differenceY / 2);
                        break;
                    case
'bottom':
                        $placementY = $origHeight - $waterMarkDestHeight - $edgePadding;
                        break;
                }

      
                if($size[2]==3)
                    $resultImage = imagecreatefrompng($original);
                else
                    $resultImage = imagecreatefromjpeg($original);
                imagealphablending($resultImage, TRUE);
        
                $finalWaterMarkImage = imagecreatefrompng($wmTarget);
                $finalWaterMarkWidth = imagesx($finalWaterMarkImage);
                $finalWaterMarkHeight = imagesy($finalWaterMarkImage);
        
                imagecopy($resultImage,
                          $finalWaterMarkImage,
                          $placementX,
                          $placementY,
                          0,
                          0,
                          $finalWaterMarkWidth,
                          $finalWaterMarkHeight
                );
                
                if($size[2]==3){
                    imagealphablending($resultImage,FALSE);
                    imagesavealpha($resultImage,TRUE);
                    imagepng($resultImage,$target,$quality);
                }
else{
                    imagejpeg($resultImage,$target,$quality);
                }


                imagedestroy($resultImage);
                imagedestroy($finalWaterMarkImage);

                // display resulting image for download
?>

   <div>
    <h1>Image Upload</h1>
    <p>
     <a href="<?php echo $_SERVER['PHP_SELF'] ?>">Back To Form</a>
    </p>
    <p>
      Klik op de foto voor een vergroting ervan.
    </p>
    <p align="center">
     <a href="results/<?php echo $target_name ?>?id=<?php echo md5(time()) ?>" target="_blank"><img width="800" border="0" src="results/<?php echo

$target_name ?>
?id=<?php echo md5(time()) ?>" alt="Klik voor een vergroting" /></a>
    </p>
    <hr>
   </div>
<?php
                unlink($wmTarget);
            }
else{
?>

   <div>
    <h1>Watermarked Image</h1>
    <p class="errorMsg">The image uploaded was not a JPEG or PNG image.</p>
   </div>
<?php
            }
        }
else{
?>

   <div>
    <h1>Watermarked Image</h1>
    <p class="errorMsg">Unable to upload the image to watermark.</p>
   </div>
<?php
        }
    }
else{
        // no image posted, show form for options
?>

   <div>
    <h1>Image Upload</h1>
    <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>">
     <table cellpadding="2" cellspacing="0" border="1" width="">
      <tr>
       <td>
        Selecteer bestand:
       </td>
       <td>
        <input type="file" name="watermarkee" />
       </td>
      </tr>
      <tr>
              <td valign="top">
        
<?php
        $dir
=dirname(__FILE__).'/watermarks';
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                $i=0;
                $watermarks=array();
                while (($file = readdir($dh)) !== FALSE) {
                    if(!preg_match('`\.png$`',$file)) continue;
                    $watermarks[]=$file;
                }

                closedir($dh);
                
                // now sort the array according to file name
                asort($watermarks);
                
                foreach($watermarks as $file){
                    // restrain display width
                    $size=getimagesize($dir.'/'.$file);
                    if($size[0] > $disp_width_max && $disp_width_max){
                        $reduction=$disp_width_max / $size[0];
                        $size[0]=$size[0]*$reduction;
                        $size[1]=$size[1]*$reduction;
                        $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
                        
                        // also restrain the height after a resize
                        if($size[1] > $disp_height_max && $disp_height_max){
                            $reduction=$disp_height_max / $size[1];
                            $size[0]=$size[0]*$reduction;
                            $size[1]=$size[1]*$reduction;
                            $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
                        }
                    }


                    // also restrain the height
                    if($size[1] > $disp_height_max && $disp_height_max){
                        $reduction=$disp_height_max / $size[1];
                        $size[0]=$size[0]*$reduction;
                        $size[1]=$size[1]*$reduction;
                        $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);
                    }


                    echo '      <tr>',"\n",
                         '       <td>Selecteer copyright:</td><td colspan="1">',"\n",
                         '        <input type="radio" value="',$file,'" name="watermark" /> ',
                         '<img src="watermarks/',$file,'" ',$size[3],' alt="" />',"\n",
                         '       </td>',"\n",
                         '      </tr>',"\n";

                    $i++;
                }

                if($i==0){
                    // no images at this time
                    echo '     <tr>',"\n",
                         '      <td>',"\n",
                         '       There are currently no watermark images to use with this the system on this server.',"\n",
                         '      </td>',"\n",
                         '     </tr>',"\n";
                }
            }
        }

?>

        </table><P>
<table border="1">
<tr>
<td colspan="2" align="left"><b><u>Informatie bij de foto:</u></b></td>
</tr>
<tr>
<td>Locatie (bv. EHEH):</td><td><input type="text" name="locatie" MAXLENGTH="4"></td>
</tr>
<tr>
<td>Fotograaf:</td><td><input type="text" name="naam"></td>
</tr>
<tr>
<td>Wat zie je:</td><td>

<select name="wattezien" size="1" id="wattezien">
<option value="verkeerstoren">Verkeerstoren</option>
<option value="terminal(binnen)">Terminal (binnen)</option>
<option value="terminal(buiten)">Terminal (buiten)</option>
<option value="overvieuw">Overview</option>
<option value="apron">Apron</option>
<option value="Runway">Runway</option>
</select>

</td>
</tr>
<tr>
<td>Datum:</td><td><input type="text" name="datum"></td>
</tr>
<tr>
<td>Camera:</td><td><input type="text" name="camera"></td>
</tr>
</table>
       </td>
      </tr>
        <input type="hidden" name="h_position" value="center"/><br />
        <input type="hidden" name="v_position" value="center"/><br />
        <input type="hidden" name="wm_size" value=".3"/><br />
      <tr>
       <td align="center" colspan="2">
        <input type="submit" name="process" value="Upload" />
       </td>
      </tr>
     </table>
    </form>
   </div>
<?php
    }
    $page_display=ob_get_clean();

function
resize_png_image($img,$newWidth,$newHeight,$target){
    $srcImage=imagecreatefrompng($img);
    if($srcImage==''){
        return FALSE;
    }

    $srcWidth=imagesx($srcImage);
    $srcHeight=imagesy($srcImage);
    $percentage=(double)$newWidth/$srcWidth;
    $destHeight=round($srcHeight*$percentage)+1;
    $destWidth=round($srcWidth*$percentage)+1;
    if($destHeight > $newHeight){
        // if the width produces a height bigger than we want, calculate based on height
        $percentage=(double)$newHeight/$srcHeight;
        $destHeight=round($srcHeight*$percentage)+1;
        $destWidth=round($srcWidth*$percentage)+1;
    }

    $destImage=imagecreatetruecolor($destWidth-1,$destHeight-1);
    if(!imagealphablending($destImage,FALSE)){
        return FALSE;
    }

    if(!imagesavealpha($destImage,TRUE)){
        return FALSE;
    }

    if(!imagecopyresampled($destImage,$srcImage,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight)){
        return FALSE;
    }

    if(!imagepng($destImage,$target)){
        return FALSE;
    }

    imagedestroy($destImage);
    imagedestroy($srcImage);
    return TRUE;
}


    echo '<?xml version="1.0" encoding="iso-8859-1"?>',"\n";
?>

<!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">
 <head>
  <title>Watermarking JPEG and PNG Images with PHP and GD2</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta name="description" content="A PHP implementation to watermark JPEG or PNG images with a PNG-24 image with alpha transparency." />
  <style type="text/css">
   @import url(style.css);
    th{
        text-align: right;
        font-weight: bold;
    }
    th p{
        font-weight: normal;
        font-size: 75%;
        color: #c00;
        background-color: transparent;
    }
  </style>
  <!--[if lt IE 7]><script src="/ie7/ie7-standard.js" type="text/javascript"></script><![endif]-->
 

</head>


 <body>
  <div id="container">
  

   <div>
    <?php echo $page_display ?>
   </div>

 </div>

<?php include_once 'site_menu.php'; ?>

 </body>
</html>


Nou heb ik 2 problemen.
1: Als ik de pagina refresh (F5 bijvoorbeeld), dan wordt de data opgeslagen in de database, dus krijg ik een lege regel.

2: Alles wordt opgeslagen in de database, behalve de locatie van het bestand. Wat doe ik fout?
 
PHP hulp

PHP hulp

22/12/2024 09:31:28
 
- SanThe -

- SanThe -

18/05/2008 10:35:00
Quote Anchor link
Post alleen relevante code. Beetje onzinnig om 412 regels te posten.
 
Burdy

Burdy

18/05/2008 11:29:00
Quote Anchor link
Probleem 1: je INSERT-query staat helemaal bovenin je code, zonder enige controle. Dus eerst de input controleren, dan pas INSERT in de database.

Probleem 2: je moet (denk ik) de locatie van het bestand ook meeposten.
 
Jesper Diovo

Jesper Diovo

18/05/2008 11:34:00
Quote Anchor link
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
<?php
// QUOTE:
  die('Kan niet verbinden: ' . mysql_error());

// (..)

ob_start();
?>


Auw...
Gewijzigd op 01/01/1970 01:00:00 door Jesper Diovo
 



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.