Scripter gezocht voor upload class

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Top Low-Code Developer Gezocht!

Bedrijfsomschrijving Unieke Kansen, Uitstekende Arbeidsvoorwaarden & Inspirerend Team Wij zijn een toonaangevende, internationale organisatie die de toekomst van technologie vormgeeft door het creëren van innovatieve en baanbrekende oplossingen. Ons succes is gebaseerd op een hecht en gepassioneerd team van professionals die altijd streven naar het overtreffen van verwachtingen. Als jij deel wilt uitmaken van een dynamische, vooruitstrevende en inspirerende werkomgeving, dan is dit de perfecte kans voor jou! Functieomschrijving Als Low-Code Developer ben je een cruciaal onderdeel van ons team. Je werkt samen met collega's uit verschillende disciplines om geavanceerde applicaties te ontwikkelen en te optimaliseren met behulp van Low-code

Bekijk vacature »

Remco schrubben

remco schrubben

29/07/2012 02:40:06
Quote Anchor link
Forumleden,

Ik ben op zoek naar iemand die mij kan helpen met een upload class.
Deze class moet verschillende functie's hebben.

- Het uploaden van foto's
- Het verkleinen van een orginele foto.
- Het maken van een thumb en weg zetten in aparte map.
- Het controleren op alle mogelijke dingen ter beveiliging.

Dit alles moet in een class staan die ik in een admin panel of waar dan ook kan aanroepen. Zodra de gebruiker een foto upload moet deze dus weg geschreven worden op een aan gegeven plek. Omdat er voor diverse onderdelen word gewerkt moet alles via de zelfde class kunnen werken. Daarom moet er voor het weg schrijven van de filename naar de database diverse $vars worden gemaakt.

Deze $vars moet op toepassing zijn van de class. Als ik bijvoorbeeld in het script zeg van:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
$orgineel_map
= 'images/aplicatie_1/';
$thumb_map = 'images/aplicatie_1/thumb/';
$thumb = true;
$thumb_maat = 150;
include ('upload_class.php');
?>


Dan moeten die 3 $vars mee genomen worden in de upload_class.
Zo hoef ik niet telkens van alles te type om ergens een upload functie te maken.
En kan ik door middel van bepaalde $vars het uploaden al laten werken.

Graag zou ik via PM horen wat jullie vragen voor z'n class als beschreven.
Gewijzigd op 29/07/2012 02:41:47 door Remco schrubben
 
PHP hulp

PHP hulp

26/04/2024 18:50:27
 
Aron van W

Aron van W

29/07/2012 09:16:07
Quote Anchor link
Misschien kan ik wel helpen.
Ik heb heel veel ervaringen met PHP Classes enzo, maar iets met foto's verkleinen nog niet. Mooie uitdaging.
 
Obelix Idefix

Obelix Idefix

29/07/2012 10:02:38
 
Bart V B

Bart V B

29/07/2012 10:09:49
Quote Anchor link
Waarom het wiel opnieuw uitvinden?
Alleen ff de vars veranderen en je bent klaar..
http://www.verot.net/php_class_upload.htm

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
<?php
$foo
= new Upload($_FILES['form_field']);
if ($foo->uploaded) {
  // save uploaded image with no changes
  $foo->Process('/home/user/files/');
  if ($foo->processed) {
    echo 'original image copied';
  }
else {
    echo 'error : ' . $foo->error;
  }

  // save uploaded image with a new name
  $foo->file_new_name_body = 'foo';
  $foo->Process('/home/user/files/');
  if ($foo->processed) {
    echo 'image renamed "foo" copied';
  }
else {
    echo 'error : ' . $foo->error;
  }

  // save uploaded image with a new name,
  // resized to 100px wide

  $foo->file_new_name_body = 'image_resized';
  $foo->image_resize = true;
  $foo->image_convert = gif;
  $foo->image_x = 100;
  $foo->image_ratio_y = true;
  $foo->Process('/home/user/files/');
  if ($foo->processed) {
    echo 'image renamed, resized x=100
          and converted to GIF'
;
    $foo->Clean();
  }
else {
    echo 'error : ' . $foo->error;
  }
}

?>
 
Remco schrubben

remco schrubben

29/07/2012 16:17:53
Quote Anchor link
Bart ik zie alleen dat hij in die voorbeeld het convert naar gif.
Deze dingen kan er gewoon dan uit hoop ik?
Want bestanden moeten wel de extesion naam houden die het had.
 
Donny Wie weet

Donny Wie weet

29/07/2012 17:18:37
Quote Anchor link
Al geprobeerd om:

$foo->image_convert = gif;

naar

$foo->image_convert = png;

of eigen keuze, te veranderen?
 

29/07/2012 19:09:23
Quote Anchor link
Hello Remco

Hieronder vind u een gemakkelijk bestand met onderaan een paar voorbeelden. Wel moet u wanneer een foto geüpload wordt nog kijken of de grootte niet de groot is. Eventueel kun je dat nog toevoegen aan het script.

Als u eventueel vragen hebt, mag u deze altijd naar mij sturen.
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php

class Resize {

    /**
     * @param mixed $image                -> the original file will be stored here,
     * @param int $width                -> the width of the original image,
     * @param int $height                -> the height of the image,
     * @param mixed $imageResized        -> the resized image
     */

    private
        $image,
        $width,
        $height,
        $imageResized;
        
    /**
     * @param string $fileName            -> the name of the file that will be used,
     * @return bool
     */

    public function __construct($fileName) {
    
        /* Use the openImage function to check if the image is an image, when this
         * isn't true we are going to return false to prevent errors and corrupted files:
         */

        $this->image = $this->openImage($fileName);
        if($this->image == false) {
            return false;
        }

        
        /*
         * Get the width and the height of the image. This will be used to
         * recreate the image:
         */

        $this->width  = imagesx($this->image);
        $this->height = imagesy($this->image);
        
        return true;
        
    }

    
    /**
     * Open the image. And check if it is a real image. Not a corrupted image.
     * @param string $fileName,
     * @return mixed                -> when corrupted false, otherwhishe image resource
     */

    private function openImage($fileName) {
    
        /**
         * Check if the image exists and check if has a height, php or other files that
         * aren't an image are going to be catched here:
         */

        if(!file_exists($fileName) || getimagesize($fileName) == false) {
            return false;
        }

    
        /**
         * Here we are going to check the extension of the file. Again when the extension
         * isn't from an image, false will be returned:
         */

        $extension = pathinfo($fileName, PATHINFO_EXTENSION);
        
        /**
         * Switch the types and when correct create the gd resource.
         */

        switch($extension) {
            case
'jpeg':
            case
'jpg':
                $img = imagecreatefromjpeg($fileName);
            break;
            case
'gif':
                $img = imagecreatefromgif($fileName);
            break;
            case
'png':
                $img = imagecreatefrompng($fileName);
            break;
            default:

                $img = false;
            break;
        }

        
        return $img;
        
    }

    
    /**
     * The public resize function. You need to call this and it will handle everything
     * except saving.
     * @param int $newWidth,
     * @param int $newHeight,
     * @param string $option            -> options are auto, crop, landscape, portrait and exact,
     * @return void
     */

    public function resizeImage($newWidth, $newHeight, $option = 'auto')  {  
  
        /**
         * Get the optimal height and width for the new file. This check also the option
         * to decide wich new height and width will be used:
         */

        $optionArray = $this->getDimensions($newWidth, $newHeight, strtolower($option));
  
        /**
         * Resample the image on a canvas.
         */

        $this->imageResized = imagecreatetruecolor($optionArray['optimalWidth'], $optionArray['optimalHeight']);  
        imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optionArray['optimalWidth'], $optionArray['optimalHeight'], $this->width, $this->height);  
  
        /**
         * When the option is crop, we are going to do that inmedialty, because nothing else
         * is needed:
         */

        if($option == 'crop') {  
            $this->crop($optionArray['optimalWidth'], $optionArray['optimalHeight'], $newWidth, $newHeight);  
        }  

    }

    
    /**
     * The dimension function. Get the optimal height and width for the image. Check
     * also the $option to choose the dimensions.
     * @param int $newWidth,
     * @param int $newHeight,
     * @param string $option,
     * @return array
     */

    private function getDimensions($newWidth, $newHeight, $option) {  
      
        /**
         * Switch the options and set the right height and width
         * for the image:
         */

        switch($option) {  
            case
'exact':  
                $optimalWidth = $newWidth;  
                $optimalHeight= $newHeight;  
            break;  
            case
'portrait':  
                $optimalWidth = $this->getSizeByFixedHeight($newHeight);  
                $optimalHeight= $newHeight;  
            break;  
            case
'landscape':  
                $optimalWidth = $newWidth;  
                $optimalHeight= $this->getSizeByFixedWidth($newWidth);  
            break;  
            case
'auto':  
                $optionArray = $this->getSizeByAuto($newWidth, $newHeight);  
                $optimalWidth = $optionArray['optimalWidth'];  
                $optimalHeight = $optionArray['optimalHeight'];  
            break;  
            case
'crop':  
                $optionArray = $this->getOptimalCrop($newWidth, $newHeight);  
                $optimalWidth = $optionArray['optimalWidth'];  
                $optimalHeight = $optionArray['optimalHeight'];  
            break;  
        }

        
        return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
        
    }

    
    /**
     * When the option is portrait, we are going to scale the height and
     * width like a portrait.
     * @param int $newHeight,
     * @return int $newHeight * $ratio
     */

    private function getSizeByFixedHeight($newHeight) {  
        $ratio = $this->width / $this->height;  
        return $newHeight * $ratio;
    }


    /**
     * Scale the images like a landscape. This option can make the width of the
     * image larger than set.
     * @param int $newWidth,
     * @return int $newWidth
     */

    private function getSizeByFixedWidth($newWidth) {  
        $ratio = $this->height / $this->width;  
        return $newWidth * $ratio;
    }

    
    /**
     * When the user couldn't decide what he wanted, he could choose the auto
     * function and that will be handled here.
     * @param int $newWidth,
     * @param int $newHeight,
     * @return array
     */

    private function getSizeByAuto($newWidth, $newHeight) {
    
        /**
         * When the height of the image is smaller than the width, scale the new image like
         * a landscape picture:
         */

        if($this->height < $this->width)  {  
            $optimalWidth  = $newWidth;  
            $optimalHeight = $this->getSizeByFixedWidth($newWidth);  
        }


        /**
         * When the height is larger than the width, scale the picture as an
         * portrait:
         */

        elseif($this->height > $this->width) {  
            $optimalWidth  = $this->getSizeByFixedHeight($newHeight);  
            $optimalHeight = $newHeight;  
        }
  
        
        /**
         * When the height and the width of the image that has been set
         * is the same, then we are going to look to the preferences:
         */

        else {
        
            /**
             * When the $newHeight is smalled than the $newWidth, then we are
             * going to scale the image like an landscape picture:
             */

            if($newHeight < $newWidth) {  
                $optimalWidth  = $newWidth;  
                $optimalHeight = $this->getSizeByFixedWidth($newWidth);  
            }

            
            /**
             * Scale the picture as an portrait when the $newHeight is larger
             * than the $newWidth:
             */

            elseif ($newHeight > $newWidth) {  
                $optimalWidth = $this->getSizeByFixedHeight($newHeight);  
                $optimalHeight= $newHeight;  
            }


            /**
             * When the $newHeight is the same as the $newWidth, we are going
             * to use those preferences:
             */

            else {
                $optimalWidth  = $newWidth;  
                $optimalHeight = $newHeight;  
            }
            
        }
  
  
        return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);  
        
    }

    
    /**
     * The optimal crop size will be calculated here. The image will be
     * cut, so pieces can be removed.
     * @param int $newWidth,
     * @param int $newHeight,
     * @return array
     */

    private function getOptimalCrop($newWidth, $newHeight) {  
  
        /**
         * Calculate the height and width ratio, so we can choose how
         * to cut the image if needed:
         */

        $heightRatio = $this->height / $newHeight;  
        $widthRatio  = $this->width /  $newWidth;  
  
        /**
         * When the height ratio is bigger than the width
         * ratio. The optimal ratio will be the $heightRatio:
         */

        if($heightRatio < $widthRatio) {  
            $optimalRatio = $heightRatio;  
        }

        
        /**
         * When the $widthRatio is larger than the $heightRatio. Then the
         * $widthRatio will be used as optimal ratio:
         */

        else {  
            $optimalRatio = $widthRatio;  
        }
  
  
        /**
         * Now calculate the $optimalHeight and the $optimalWidth
         * and return them:
         */

        $optimalHeight = $this->height / $optimalRatio;  
        $optimalWidth  = $this->width  / $optimalRatio;  
  
        return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
        
    }

    
    /**
     * The crop function will resize the image with the optimal height and
     * width + the new width and height.
     * @param int $optimalWidth,
     * @param int $optimalHeight,
     * @param int $newWidth,
     * @param int $newHeight,
     * @return void
     */

    private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight) {  
    
        /**
         * Get the exact x and y point to crop the image. This will be used
         * to cut the image:
         */

        $cropStartX = ($optimalWidth / 2) - ($newWidth /2);  
        $cropStartY = ($optimalHeight/ 2) - ($newHeight/2);  
  
        /**
         * Set the image resized picture into the crop variabele because the
         * resized image will be overwritten:
         */

        $crop = $this->imageResized;  
  
        /**
         * Now create the new picture and save it into the $this->imageResized
         * var:
         */

        $this->imageResized = imagecreatetruecolor($newWidth , $newHeight);  
        imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);  
        
    }

    
    
    /**
     * The save function. This is needed to save the image off course.
     * @param string $savePath            -> a path that already exists,
     * @param int $imageQuality,
     * @return bool
     */

    public function saveImage($savePath, $imageQuality = '100') {

        /**
         * When the resized image doesn't exists, we are going to upload
         * the fresh one:
         */

        if($this->imageResized == false) {
            $this->resizeImage($this->width, $this->height, 'exact');
        }

      
        /**
         * Get the image extension and save the image with the
         * preferred extension of the new file:
         */

        $extension = strtolower(pathinfo($savePath, PATHINFO_EXTENSION));
      
        switch($extension) {  
            case
'jpg':  
            case
'jpeg':  
                if(imagetypes() & IMG_JPG) {  
                    imagejpeg($this->imageResized, $savePath, $imageQuality);  
                }
  
            break;  
            case
'gif':  
                if(imagetypes() & IMG_GIF) {  
                    imagegif($this->imageResized, $savePath);  
                }
  
            break;  
            case
'png':  
                $scaleQuality = round(($imageQuality/100) * 9);  
                $invertScaleQuality = 9 - $scaleQuality;  
                if(imagetypes() & IMG_PNG) {  
                    imagepng($this->imageResized, $savePath, $invertScaleQuality);  
                }
  
            break;  
            default:

                return false;
            break;  
        }
  
      
        /**
         * Now we are at the end of the savind function and we do not need the image
         * any more, so delete it and return true:
         */

        imagedestroy($this->imageResized);
        
        return true;
        
    }

    
    /* Returns the status of the image, needs to be checked when uploading an
     * unknow file.
     * @param string type,
     * @return bool true/false
     */

    public function getImageStatus($type = 'image') {
        return in_array($type, array('image', 'imageResized')) ? $this->$type : false;
    }

}


/**
 * Open the resize class and set the picture that will be used
 * in the class:
 */

$upload = new Resize('code.png');

/**
 * When the file is corrupt, the status of the image will be false,
 * so check if the image is corrupt:
 */

if($upload->getImageStatus() == false) {
    echo 'The image is corrupt.';
}


else {

    /**
     * First save the image. So we can use this one later on. The second
     * param is the image quality. You can save the image as jpg, jpeg, gif
     * or png:
     */

    $upload->saveImage('code-save.png', 100);
    
    /**
     * Now create 5 images with the 5 options used and check out the
     * result. First the new width, then the height and the option.
     * Use excact, portrait, landscape, auto, crop as option:
     */

    $upload->resizeImage(250, 250, 'exact');
    $upload->saveImage('code-exact.png');
    $upload->resizeImage(250, 250, 'portrait');
    $upload->saveImage('code-portrait.png');
    $upload->resizeImage(250, 250, 'landscape');
    $upload->saveImage('code-landscape.png');
    $upload->resizeImage(250, 250, 'auto');
    $upload->saveImage('code-auto.png');
    $upload->resizeImage(250, 250, 'crop');
    $upload->saveImage('code-crop.png');
    
}


?>
 
Remco schrubben

remco schrubben

30/07/2012 18:53:18
Quote Anchor link
Bedankt voor de reactie's.
Ik ga het allemaal even bekijken en laat van me horen.
 



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.