PHPFetchPicture (adv. ThumbnailGen + cache)

Gepost door Vincent Driessen op 29-11-2003 15:52.

Nieuwe revisie (1.2.1)

Dit script heeft nodig:
PHP versie 4 of hoger
GD versie 2 of hoger

Nog meer features, finetuning en dergelijke. De highlights zijn:
- Verbetering berekenen dimensies bij kantelen plaatje
- Mogelijkheid om interpoleren te gebruiken
- Toevoegen van PHPDoc (zie link onder)

Er zijn nu de volgende bewerkingen mogelijk met het script:
1) Aanpassen formaat (met/zonder interpoleren)
2) Roteren
3) Spiegelen
4) Uitknippen
5) Gamma correctie

VOORBEELD URL:
http://yourserver/yourdirectory/fetchpicture.php?file=picture.jpg&rotate=45 &cut=1000,1000,1500,1500&gamma=0.6&width=200&height=500&quality=40&mirror=horizontal&interpolate=true

Dit script is gemaakt om een verzameling van grote plaatjes (+1MB) beschikbaar te maken. Om deze reden worden gegenereerde plaatjes gecached (iedere keer opnieuw genereren kost veel CPU tijd). Wanneer cached items niet binnen een bepaalde tijd opgevraagd worden, worden ze verwijderd.

PHPDOC DOCUMENTATIE:
http://beumer.xs4all.nl/bart/scripts/FetchPicture/
een overzicht van de functies binnen de classes, hun functie, hoe te gebruiken e.d.

Bestanden van dit script

index.php

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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
<?
/*--------------------------------------------------------------------------------
PHPFetchPicture version 1.2.1 (BETA)
Copyright (C) 2003  Bart Beumer (bart@beumer.xs4all.nl)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
--------------------------------------------------------------------------------
This script is ment for image manipulation. It can manipulate images, and output
the result to a file and/or browser. If an image is generated, a copy is stored
in the cache-dir, so that it can be used another time (without beeing regenerated)

The following manipulations are possible:
* RESIZE (determine the maximum size of picture to be send to browser)
* ROTATE (rotate a picture in degrees)
* MIRROR (horizontally or vertically)
* CUT    (only use a portion of the original image)
* GAMMA  (color correction)

WARING: compared to previous versions, the WEBSITE LOGO FEATURE HAS BEEN REMOVED. This
is because of the major redesign. This feature will be implemented in the future
together with a picture in picture feature

EXAMPLE HOW TO CALL PARAMETERS IN URL (returns added for increased readability):
http://yourserver/yourdirectory/fetchpicture.php?file=picture.jpg&rotate=45
    &cut=1000,1000,1500,1500&gamma=0.6&width=200&height=500&quality=40&mirror=horizontal
    &interpolate=true


This script has been redesigned completely compared to older versions. It has
not been fully tested. If problems are experienced, please contact the
programmer ( http://beumer.xs4all.nl/bart/mailme.php )
--------------------------------------------------------------------------------*/

/*
CHANGELOG: fetchpicture.php
Version 1.2 ( 29 Nov 2003 )
* changes to enable interpolated picture resize-ing
     - added $_GET variable interpolate

*/
    
    /**
     * @variable string image_dir
     * Directory where original images are stored.<br>
     * CONFIGURATION PARAMETER
     */
    $image_dir                    = "./no_access/";

    /**
     * @variable string cache_dir
     * Directory where cached images are (to be) stored.<br>
     * CONFIGURATION PARAMETER
     */
    $cache_dir                    = "./cachedir/";

    //the age of a cache entry after which it may be deleted (in DAYS).
    /**
     * @variable integer image_max_age
     * The maximum age a cache entry can be.
     * If the entry is older, it will be deleted.
     */
    $image_max_age                = 7;



//----------------------------------------------------------------------
// P A R A M E T E R S   F R O M   U S E R S
//----------------------------------------------------------------------
$image_file            = $_GET["file"];
$image_width            = $_GET["width"];
$image_height            = $_GET["height"];
$image_rotate            = $_GET["rotate"];
$image_outputformat    = $_GET["format"];
$image_gamma            = $_GET["gamma"];
$image_quality            = $_GET["quality"];
$image_mirror            = $_GET["mirror"];
$image_cut                = $_GET["cut"];
$image_interpolate    = $_GET["interpolate"];

$image_fullpath = $image_dir . $image_file;
$image_file_realpath = realpath($image_fullpath);
$image_dir_realpath = realpath($image_dir);

//CHECK IF FILE MEETS ALL THE SET CONDITIONS
if(    !ereg("^".$image_dir_realpath."/",$image_file_realpath) || /*located in the image diretory*/
    !file_exists($image_fullpath) || /*file must exsist*/
    !is_readable($image_fullpath) || /*file must be readable*/
    !($image_info = getimagesize ($image_fullpath)) /*file must be an image*/
    ) die();

//CHECK IF USER WANTS ORIGINAL OR RESIZED VERSION
if (    !(isset($image_width)            && $image_width != "") &&
        !(isset($image_height)        && $image_height != "") &&
        !(isset($image_outputformat)    && $image_outputformat != "") &&
        !(isset($image_rotate)        && $image_rotate != "" ) &&
        !(isset($image_gamma)            && $image_gamma != "" ) &&
        !(isset($image_quality)        && $image_quality != "" ) &&
        !(isset($image_mirror)        && $image_mirror != "" ) &&
        !(isset($image_cut)            && $image_cut != "" ) &&
        !(isset($image_interpolate)    && $image_interpolate != "" )
    )
{
    header("Content-type: " . $imageinfo['mime'] . "");
    readfile($image_fullpath);
}
else
{

    $img = new CThumbnailGenerator();
    $img->set_file($image_fullpath);
    $img->set_rotation($image_rotate);
    
    if($image_interpolate=="true")$image_interpolate = true;
    else $image_interpolate = false;
    $img->set_resize($image_width,$image_height,false,false,$image_interpolate);

    if( ereg("([0-9]{1,4}|[x]),([0-9]{1,4}|[x]),([0-9]{1,4}|[x]),([0-9]{1,4}|[x])",$image_cut,$cut_values) )
    {
        for($i=0;$i<4;$i++)
        {
            if($cut_values[$i]=="x") unset($cut_values[$i]);
        }
        
        $img->set_cut($cut_values[1],$cut_values[2],$cut_values[3],$cut_values[4]);
    }
    $img->set_mirror($image_mirror);
    $img->set_gamma($image_gamma);
    $img->set_output_format(2,$image_quality);

    $md5filename = $cache_dir . $img->calculate_uniqueidentifier() . ".jpg";
    if(file_exists($md5filename) && is_file($md5filename) && is_readable($md5filename))
    {
        //PICTURE IS AVAILABLE IN CACHE
        header("Content-type: image/jpeg");
        readfile($md5filename);
    }
    else
    {
        if(!$img->output($md5filename,true,true) ) die("not all required parameters where set");
    }
}



/*
CHANGELOG: CThumbnail
Version 1.2 ( 29 Nov 2003 )
* use of round() instead of floor() for better matching of wanted resolution
* changes to enable interpolated picture resize-ing
     - added class variable $modify_resize_interpolate
     - changed set_resize ( ... ) to match new functionality

*/

/**
* This class makes it possilbe to generate thumbnails
* in an easy and fast manner. This class uses CImage
*
* @brief represents Image with easy and fast manipulation
*
* @author Bart Beumer
* @copyright GNU/GPL
* @date 26 nov 2003
*
* @package beumer
*/
class CThumbnailGenerator
{
    /**
     * stores the path of the picture to be used
     * @variable string picture_path
     * @private
     */
    var $picture_path;
    /**
     * stores the size of the picture (in bytes)
     * @variable integer picture_size
     * @private
     */
    var $picture_size;
    /**
     * stores the date when the picture was last manipulated
     * @variable integer picture_date
     * @private
     */
    var $picture_date;
    /**
     * stores the width of the original picture in pixels
     * @variable integer picture_width
     * @private
     */
    var $picture_width;
    /**
     * stores the height of the original picture in pixels
     * @variable integer picture_height
     * @private
     */
    var $picture_height;
    
    /**
     * stores the number of degrees the picture should be rotated
     * @variable integer modify_rotate_deg
     * @private
     */
    var $modify_rotate_deg;
    /**
     * stores wether the picture should be mirrored, and how.
     * @variable string modify_mirror
     * @private
     */
    var $modify_mirror;
    /**
     * stores the gamma correction to be applied to the picture
     * @variable float modify_gamma
     * @private
     */
    var $modify_gamma;

    /**
     * stores amount of pixels at top of picture to be removed
     * @variable integer modify_cut_top
     * @private
     */
    var $modify_cut_top;
    /**
     * stores amount of pixels at left side of picture to be removed
     * @variable integer modify_cut_left
     * @private
     */
    var $modify_cut_left;
    /**
     * stores amount of pixels atbottom of picture to be removed
     * @variable integer modify_cut_bottom
     * @private
     */
    var $modify_cut_bottom;
    /**
     * stores amount of pixels at right side of picture to be removed
     * @variable integer modify_cut_right
     * @private
     */
    var $modify_cut_right;
    /**
     * stores new width of picture
     * @variable integer modify_resize_X
     * @private
     */
    var $modify_resize_X;
    /**
     * stores new height of picture
     * @variable integer modify_resize_Y
     * @private
     */
    var $modify_resize_Y;
    /**
     * should picture be stretched when resized
     * @variable boolean modify_stretch
     * @private
     */
    var $modify_resize_stretch;
    /**
     * should the edges of the picture be filled to make a picture which fits maximum dimensions
     * @variable boolean modify_resize_fillunused
     * @private
     */
    var $modify_resize_fillunused;
    /**
     * if interpolation it to be used. This can give a better end-result with smaller pictures
     * @variable boolean modify_resize_interpolate
     * @private
     */
    var $modify_resize_interpolate;
    /**
     * the picture formate to output
     * @variable integer output_type
     * @private
     */
    var $output_type;
    /**
     * quality factor of the picture to output. This parameter is only used if picture output is in JPEG format.
     * @variable integer output_qualityfactor
     * @private
     */
    var $output_qualityfactor;
    
    /**
     * @constructor CThumbnailGenerator
     * @public
     */
    function CThumbnailGenerator()
    {
        $this->clear();
    }
    
    /**
     * Clear all settings. Start from scratch
     * @public clear
     */
    function clear()
    {
        unset($this->picture_path);
        unset($this->picture_size);
        unset($this->picture_date);
        unset($this->picture_width);
        unset($this->picture_height);
        
        $this->modify_rotate_deg = 0;
        $this->modify_mirror = "none";
        $this->modify_gamma = 1;

        unset($this->modify_cut_top);
        unset($this->modify_cut_left);
        unset($this->modify_cut_bottom);
        unset($this->modify_cut_right);
        unset($this->modify_resize_X);
        unset($this->modify_resize_Y);

        $this->modify_resize_stretch = false;
        $this->modify_resize_fillunused = false;
        $this->modify_resize_interpolate = false;

        $this->output_type = 2; //JPEG
        $this->qualityfactor = 75;
    }
    
    /**
     * Set the file containing source image.
     * @public set_file
     * @param string filename file to use as source image
     * @return bool indicate successful execution
     */
    function set_file($filename)
    {
        //check if file is valid
        if(    file_exists($filename) && /*file must exsist*/
            is_readable($filename) && /*file must be readable*/
            ($image_info = getimagesize ($filename)) /*file must be an image*/
            )
        {
            $this->clear();
            
            $this->picture_path = $filename;
            $this->picture_size = filesize($filename);
            $this->picture_date = filemtime($filename);

            $this->picture_width = $image_info[0];
            $this->picture_height = $image_info[1];
            
            $this->set_cut(0,0,$this->picture_height,$this->picture_width);
            return true;
        }
        else return false;
    }
    
    /**
     * Set the number of degrees the page has to be turned
     * @public set_rotation
     * @param integer rotate_deg
     * @return boolean indicate successful execution
     */
    function set_rotation($rotate_deg)
    {
        if(!isset($this->picture_path)) return false;
        
        if($rotate_deg >= -360 && $rotate_deg <= 360)
        {
            if($rotate_deg < 0) $$rotate_deg+=360;
            $this->modify_rotate_deg = $rotate_deg;
            return true;
        }
        else return false;
    }
    
    /**
     * Set the new size for the picture
     * @public set_resize
     * @param integer X width
     * @param integer Y height
     * @param boolean stretch if image should be stretched to fit max. size
     * @param boolean fillunused if image should be proportionally sized, and added empty space
     * @param boolean interpolate if interpolation should be used during resize-ing
     * @return boolean indicate sucessful execution
     */
    function set_resize($X,$Y,$stretch,$fillunused,$interpolate)
    {
        if(!isset($this->picture_path)) return false;
        
        $this->modify_resize_X = $X;
        $this->modify_resize_Y = $Y;
        if($stretch)$this->modify_resize_stretch = true;
        if($fillunused)$this->modify_resize_fillunused = true;
        if($interpolate)$this->modify_resize_interpolate = true;
        
        return true;
    }
    /**
     * Set the new cut parameters
     * @public set_cut
     * @param integer top number of pixels to cut of the top
     * @param integer left number of pixels to cut of the left side
     * @param integer bottom number of pixels to cut of the bottom
     * @param integer right number of pixels to cut of the right side
     * @return boolean indicate successful execution
     */
    function set_cut($top,$left,$bottom,$right)
    {
        if(!isset($this->picture_path)) return false;
        
        if(!isset($top))        $top=0;
        if(!isset($left))        $left=0;
        if(!isset($bottom))    $bottom=$this->picture_height;
        if(!isset($right))    $right=$this->picture_width;
        
        if($top < 0)                            $top=0;
        if($top > $this->picture_height)    $top=$this->picture_height;
        if($left < 0)                            $left=0;
        if($left > $this->picture_width)    $left=$this->picture_width;
        
        if($bottom > $this->picture_height)    $bottom=$this->picture_height;
        if($right > $this->picture_width)    $right=$this->picture_width;
        
        if($bottom <= $top) $bottom = $top+1;
        if($right <= $left) $right = $left+1;

        $this->modify_cut_top = $top;
        $this->modify_cut_left = $left;
        $this->modify_cut_bottom = $bottom;
        $this->modify_cut_right = $right;

    }
    
    /**
     *  Set how and if the picture should be mirrored
     * @public set_mirror
     * @param string mirror "horizontal" "vertical" or "none". Indicates the rotation to use.
     * @return boolean indicate sucessful execution
     */
    function set_mirror($mirror)
    {
        if(!isset($this->picture_path)) return false;
        
        if($mirror == "horizontal");
        else if($mirror == "vertical");
        else if($mirror == "none");
        else return false;
        
        $this->modify_mirror = $mirror;
        return true;
    }
    
    /**
     * Set the gamma correction to be used
     * @public set_gamma
     * @param float gamma the gamma correction to be used.
     * @return boolean indicate successful execution
     */
    function set_gamma($gamma)
    {
        if(!isset($this->picture_path)) return false;
        
        if($gamma > 0 && $gamma < 10)
        {
            $this->modify_gamma = $gamma;
            return true;
        }
        else return false;
    }
    
    /**
     * Set the output format
     * @public set_output_format
     * @param integer type the type of image to generate (1=GIF, 2=JPEG, 3=PNG)
     * @param integer quality_factor the quality factor to be used (only with JPEG)
     * @return boolean indicate successful execution
     */
    function set_output_format($type,$quality_factor)
    {
        if(!isset($this->picture_path)) return false;
        
        if(!($type >0 && $type <=3)) return false;
        if( $type==2 )
        {
            if(!isset($quality_factor)) $quality_factor = 75;
            else if($quality_factor < 0 || $quality_factor > 100) return false;
        }
        else
        {
            $quality_factor=0;
        }

        $this->output_type = $type;
        $this->output_qualityfactor = $quality_factor;

        return true;
    }
    
    
    /**
     * Calculate a md5 string which is allmost unique for
     * every picture including the parameters
     * @public calculate_uniqueidentifier
     * @return string unique identifier
     */
    function calculate_uniqueidentifier()
    {
        if(!isset($this->picture_path)) return;
        
        //ORIGINEEL
        $width = $this->picture_width;
        $height = $this->picture_height;
        //CUT
        $top = $this->modify_cut_top;
        $left = $this->modify_cut_left;
        $bottom = $this->modify_cut_bottom;
        $right = $this->modify_cut_right;
        
        $width = ($this->modify_cut_right - $this->modify_cut_left);
        $height = ($this->modify_cut_bottom - $this->modify_cut_top);
        //RESIZE & ROTATE
        $newdimensions = calculate_dimensions_rotate_resize(
                                $width,
                                $height,
                                $this->modify_resize_X,
                                $this->modify_resize_Y,
                                $this->$modify_rotate_deg
                                );
        $img_width_resize  = $newdimensions["X"];
        $img_height_resize = $newdimensions["Y"];
        $width = $newdimensions["X afterrotate"];
        $height = $newdimensions["Y afterrotate"];        
        //MIRROR
        //GAMMA
        
        //CALCULATE CHACHE ENTRY NAME FOR THIS PICTURE WITH THESE SETTINGS
        $md5filename = md5("NAME".$this->picture_path
                            ." SIZE".$this->picture_size
                            ." DATE".$this->picture_date
                            ." WIDTH".$width
                            ." HEIGHT".$height
                            ." ROTATE".$this->modify_rotate_deg
                            ." MIRROR".$this->modify_mirror
                            ." GAMMA".$this->modify_gamma
                            ." JPGQ".$this->output_qualityfactor
                            ." INTERPOLATE".$this->modify_resize_interpolate
                            ." CUT " . $top .",". $left .",". $bottom .",". $right
                            );
        
        return $md5filename;
    }
    
    /**
     * Ouput the picture with given settings to file and/or screen
     * @public output
     * @param string path filepath to store picture
     * @param boolean save wether file to be saved
     * @param boolean send_to_client wether file should be send to client
     * @return boolean indicate successful execution
     */
    function output($path,$save,$send_to_client)
    {
        if(!isset($this->picture_path)) return;
        
        $img = new CImage();
        $img->create_fromfile($this->picture_path);
        
        //ORIGINEEL
        $width = $this->picture_width;
        $height = $this->picture_height;

        //CUT
        $top = $this->modify_cut_top;
        $left = $this->modify_cut_left;
        $bottom = $this->modify_cut_bottom;
        $right = $this->modify_cut_right;

        $img->modify_cut($top,$left,$bottom,$right);

        $width = ($this->modify_cut_right - $this->modify_cut_left);
        $height = ($this->modify_cut_bottom - $this->modify_cut_top);
        //RESIZE & ROTATE

        $newdimensions = calculate_dimensions_rotate_resize(
                                $width,
                                $height,
                                $this->modify_resize_X,
                                $this->modify_resize_Y,
                                $this->$modify_rotate_deg
                                );
        $img_width_resize  = $newdimensions["X"];
        $img_height_resize = $newdimensions["Y"];
        $width = $newdimensions["X afterrotate"];
        $height = $newdimensions["Y afterrotate"];        

        //RESIZE
        $img->modify_resize_absolute($img_width_resize,$img_height_resize,false,false,$this->modify_resize_interpolate);

        //MIRROR
        if($this->modify_mirror == "horizontal")
            $img->modify_mirror(true);
        else if($this->modify_mirror == "vertical")
            $img->modify_mirror(false);

        //GAMMA
        $img->modify_gamma($this->modify_gamma);

        //ROTATE
        $img->modify_rotate($this->modify_rotate_deg,'');

        $img->output($path,$this->output_type,$this->output_qualityfactor,false/*$save*/,$send_to_client);

        return true;
    }
}









function calculate_dimensions_rotate_resize($origX,$origY,$maxX,$maxY,$rotate_deg)
{
    if(!isset($maxX) && !isset($maxY))
    {
        $maxX = $origX;
        $maxY = $origY;
    }

    // ROTATION ANGLE
    $rotate_rad = ($rotate_deg/360)*2*pi();
    $a = $origX * abs(sin($rotate_rad));
    $b = $origY * abs(cos($rotate_rad));
    $c = $origY * abs(sin($rotate_rad));
    $d = $origX * abs(cos($rotate_rad));
    
    $ratioX = $maxX / ($c + $d);
    $ratioY = $maxY / ($a + $b);
    
    //DETERMINE IF WIDTH OR HEIGHT IS LIMITING FACTOR
    if(!isset($maxX))
        $ratio = $ratioY;
    else if(!isset($maxY))
        $ratio = $ratioX;
    else
    {
        if($ratioX < $ratioY)
            $ratio = $ratioX;
        else $ratio = $ratioY;
    }

    $a = $a * $ratio;
    $b = $b * $ratio;
    $c = $c * $ratio;
    $d = $d * $ratio;

    // CALCULATE THE SIZE TO WHICH THE PICTURE SHOULD BE RESIZED    
    $newX = round($d/abs(cos($rotate_rad)));
    $newY = round($b/abs(cos($rotate_rad)));

    // CALCULATE THE SIZE OF THE RESULTING PICTURE
    $img_resulting_X = round($c + $d);
    $img_resulting_Y = round($a + $b);

    $result = Array();
    $result["X"] = $newX;
    $result["Y"] = $newY;
    $result["X afterrotate"] = $img_resulting_X;
    $result["Y afterrotate"] = $img_resulting_Y;
    
    return $result;
}    



/*
CHANGELOG: CImage.php
Version 1.2 ( 29 Nov 2003 )
* use of round() instead of floor() for better matching of wanted resolution
* added the use of optional interpolation ( modify_resize_absolute( ... ) )
    which can be used to get a smoother picture.

*/

/**
* This class makes it possible to load, manipulate and
* output (to browser and/or file) an Image, by calling a
* limited number of functions.
*
* @brief represents Image with easy manipulation
*
* @author Bart Beumer
* @copyright GNU/GPL
* @date 25 nov 2003
*
* @package beumer
*/
class CImage
{
    /**
     * stores the loaded GD Image
     * @private m_img
     */
    var $m_img;
    /**
     * Stores the standard background color. uses a Image_Color class instance.
     *
     * @see class Image_Color
     *
     * @brief standard background color
     * @private m_img_std_bgcolor
     */
    var $m_img_std_bgcolor;
    /**
     *
     * @constructor CImage
     * @public
     */
    function CImage()
    {
        $this->m_img_std_bgcolor = new Image_Color(255,255,255,0);
    }
    /**
     * Create a new blank image with given dimensions.
     * @public create_new
     * @param integer X width
     * @param integer Y height
     * @return boolean indicate successful execution
     */
    function create_new($X,$Y)
    {
        if( !isset($X) || !isset($Y) ) return false;
        
        if(isset($this->m_img))    imagedestroy($this->m_img);
        
        $this->m_img = ImageCreateTrueColor($X,$Y);
        return true;
    }
    /**
     * load a picture from file. GIF, JPEG and PNG are supported.
     * @public create_fromfile
     * @param string path path to file. Can be absolute or relative.
     * @return boolean indicate successful execution
     */
    function create_fromfile($path)
    {
        if(    file_exists($path) && is_readable($path) && ($image_info = getimagesize($path)) )
        {
            if($image_info[2]==1)            $img = ImageCreateFromGif($path); // GIF FORMAT
            else if($image_info[2]==2)    $img = ImageCreateFromJPEG($path); // LOAD JPG PICTURE
            else if($image_info[2]==3)    $img = ImageCreateFromPng($path); // LOAD PNG PICTURE
            else return false;// type of image is not supported.
            
            if(isset($this->m_img))    ImageDestroy($this->m_img);
            
            $this->m_img = $img;
        }
        else return false;
        return true;
    }
    /**
     * output picture to file and/or client.
     * @public output
     * @param string path path to save file. Can be absolute or relative.
     * @param integer img_type value indicating format to use for output (1=GIF, 2=JPEG, 3=PNG)
     * @param integer img_quality value indicating the quality of a JPEG image (only used when output as JPEG)
     * @param boolean save indicate if image should be saved to file
     * @param boolean output indicate if image should be send to client.
     * @return boolean indicate successful execution
     */
    function output($path,$img_type,$img_quality,$save,$output)
    {
        if($save==false && $output==false) return true;
        if(!isset($img_type)) return false;
        if($img_type == 2 && !isset($img_quality)) return false;
        if(!isset($path) && $save==true) return false;
        
        ob_start();
        if($img_type == 1)        ImageGIF($this->m_img);
        else if($img_type == 2)    ImageJPEG($this->m_img,'',$img_quality);
        else if($img_type == 3)    ImagePNG($this->m_img);
        else
        {
            ob_end_clean();
            return false;
        }
        $data=ob_get_contents();
        ob_end_clean();

        if($output==true)
        {
            switch($img_type)
            {
                case 1:     header("Content-type: image/gif"); break;
                case 2: header("Content-type: image/jpeg"); break;
                case 3: header("Content-type: image/png"); break;
                default: return false;
            }
            echo $data;
        }
        if($save==true)
        {
            if( !($FILE = fopen($path,'w')) )
                return false;
            fwrite($FILE,$data);
        }
        return true;
    }
    /**
     * Rotate the loaded picture by a number of degrees (counter-clockwise).<br>NOTE: when rotating a
     * picture, the length and height of the picture are not alered. The picture will allways become
     * larger.
     * @public modify_rotate
     * @param integer rotate_deg number of degrees to turn
     * @param object color determines the color to be used for the corner which are not filled by the picture.
     * @return boolean indicate successful execution
     */
    function modify_rotate($rotate_deg,$color)
    {
        if($rotate_deg < 0 && $rotate_deg >360) return false;
        if($rotate_deg==0 || $rotate_deg==360) return true;
            
        if (!is_a($color,"Image_Color"))
        {
            $bg_color = ImageColorAllocate(
                            $this->m_img,
                            $this->m_img_std_bgcolor->R,
                            $this->m_img_std_bgcolor->G,
                            $this->m_img_std_bgcolor->B
                            );
        }
        else
        {
            $bg_color = ImageColorAllocate(
                            $this->m_img,
                            $color->R,
                            $color->G,
                            $color->B
                            );
    
        }
        $img_new = imagerotate($this->m_img,$rotate_deg,$bg_color);
        ImageDestroy($this->m_img);
        $this->m_img = $img_new;
        return true;
    }

    /**
     * Mirror a picture horzontally or vertically.
     * @public modify_mirror
     * @param boolean horizontally Mirror horizontally (TRUE) of vertically (FALSE)
     * @return boolean indicate successful execution
     */
    function modify_mirror($horizontally)
    {
        $width = ImageSX($this->m_img);
        $height = ImageSY($this->m_img);
        $new_img = ImageCreateTrueColor($width,$height);
        
        if($horizontally)
        {
            // vertical mirror
            for($i=0;$i<$height;$i++)
            {
                ImageCopy($new_img,$this->m_img,
                    0,($height-$i-1),
                    0,$i,
                    $width,1);
            }
        }
        else
        {
            // horizontal mirror
            for($i=0;$i<$width;$i++)
                ImageCopy($new_img,$this->m_img,
                    ($width-$i-1),0,
                    $i,0,
                    1,$height);
        }
        
        ImageDestroy($this->m_img);
        $this->m_img = $new_img;
    }


    /**
     * Resize a picture to fit the new width and height
     * @public modify_resize_absolute
     * @param integer maxX maximum width of resized picture
     * @param integer maxY maximum height of resized picture
     * @param boolean stretch should the picture be stretchd when resized
     * @param boolean fillunused if new picture should be proportionally resized with white edges to fit the maximum width and height
     * @param boolean interpolate if the picture should be interpolated for a smoother result
     * @return boolean indicates successful execution
     */
    function modify_resize_absolute($maxX,$maxY,$stretch,$fillunused,$interpolate)
    {
        if( ($maxX/ImageSX($this->m_img)) < ($maxY/ImageSY($this->m_img)) ) // X is smaller
            $ratio = $maxX/ImageSX($this->m_img);
        else // Y is smaller
            $ratio = $maxY/ImageSY($this->m_img);
    
        $newX = round(ImageSX($this->m_img) * $ratio);
        $newY = round(ImageSY($this->m_img) * $ratio);
        
        if($fillunused==true || $stretch==true)
            $img_new = ImageCreateTrueColor($maxX,$maxY);
        else
        {
            $maxX = $newX;
            $maxY = $newY;
            $img_new = ImageCreateTrueColor($newX,$newY);
        }

        if($stretch==false)
        {
            $bg_color = ImageColorAllocate(
                            $img_new,
                            $this->m_img_std_bgcolor->R,
                            $this->m_img_std_bgcolor->G,
                            $this->m_img_std_bgcolor->B
                            );
            ImageFill($img_new,0,0,$bg_color);
        }

        if($stretch==true)
        {
            $dstX = 0;
            $dstY = 0;
            $srcX = 0;
            $srcY = 0;
            $dstW = $maxX;
            $dstH = $maxY;
            $srcW = ImageSX($this->m_img);
            $srcH = ImageSY($this->m_img);
        }
        else
        {
            $dstX = round(($maxX-$newX)/2);
            $dstY = round(($maxY-$newY)/2);
            $srcX = 0;
            $srcY = 0;
            $dstW = $newX;
            $dstH = $newY;
            $srcW = ImageSX($this->m_img);
            $srcH = ImageSY($this->m_img);
        }

        if($interpolate) ImageCopyResampled($img_new,$this->m_img,$dstX,$dstY,$srcX,$srcY,$dstW,$dstH,$srcW,$srcH);
        else ImageCopyResized($img_new,$this->m_img,$dstX,$dstY,$srcX,$srcY,$dstW,$dstH,$srcW,$srcH);

        ImageDestroy($this->m_img);
        $this->m_img = $img_new;
        return true;
    }

    //function modify_resize_ratio($ratioX,$ratioY,$stretch)
    //{
    //}

    /**
     * Cut a portion of the picture out and save it.
     * @public modify_cut
     * @param integer top number of pixels to throw away from the top
     * @param integer left number of pixels to throw away from the left side
     * @param integer bottom number of pixels to throw away from the bottom
     * @param integer right number of pixels to be thrown away from the right side
     * @return boolean indicates successful execution
     */
    function modify_cut($top,$left,$bottom,$right)
    {
        if(    !isset($top)) $top = 0;
        if(    !isset($left)) $left = 0;
        if(    !isset($bottom)) $bottom = ImageSY($this->m_img);
        if(    !isset($right)) $right = ImageSX($this->m_img);

        if($bottom <= 0) $bottom = 1;
        if($right <= 0) $right = 1;
        if($top <= 0) $top = 0;
        if($left <= 0) $left = 0;
        
        if($bottom > ImageSY($this->m_img)) $bottom = ImageSY($this->m_img);
        if($right > ImageSY($this->m_img)) $right = ImageSX($this->m_img);

        if($top >= $bottom) $top = $bottom - 1;
        if($left >= $right) $left = $right - 1;

        if(    $top == 0 &&
            $left == 0 &&
            $bottom == ImageSY($this->m_img) &&
            $right == ImageSX($this->m_img)
            )
        {
            return true;
        }

        $img_new = ImageCreateTrueColor(($right-$left),($bottom-$top));
        
        ImageCopy(
            $img_new,
            $this->m_img,
            0,
            0,
            $left,
            $top,
            ($right-$left),
            ($bottom-$top)
            );
        ImageDestroy($this->m_img);
        $this->m_img = $img_new;
        return true;
    }

    /**
     * correct colors
     * @public modify_gamma
     * @param float gamma value for the gamma correction to be used
     * @return boolean indicates successful execution
     */
    function modify_gamma($gamma)
    {
        if($gamma == 1) return true;
        if($gamma > 0 && $gamma < 10)
        {
            ImageGammaCorrect($this->m_img,1.0,$gamma);
            return true;
        }
        else return false;
    }

}


/**
* This class is used to represent a color.
*
* @brief represents a color
*
* @author Bart Beumer
* @copyright GNU/GPL
* @date 25 nov 2003
*
* @package beumer
*/
class Image_Color
{
    /**
     * Red.<br> 0< value <255
     *
     * @variable integer R
     * @public
     */
    var $R;
    /**
     * Green.<br> 0< value <255
     *
     * @variable integer G
     * @public
     */
    var $G;
    /**
     * Blue.<br> 0< value <255
     *
     * @variable integer B
     * @public
     */
    var $B;
    /**
     * Alpha Channel. determines the amount of (in)visibility
     *
     * @variable integer alpha
     * @public
     */
    var $alpha;

/**
* Set the color.
*
* @constructor Image_Color
* @param integer R Red
* @param integer G Green
* @param integer B Blue
* @param integer alpha Alpha Channel
*/
function Image_Color($R,$G,$B,$alpha)
{
    $this->R        = $R;
    $this->G        = $G;
    $this->B        = $B;
    $this->alpha    = $alpha;
}
}
?>

Commentaar

09-11-2003 19:15

Geweldig man! Had zelf ook al zoiets gemaakt. En dit gebruiken in een systeem voor thumbnails maakt dit helemaal handig. Want het loaden van al die images duurt bij het berekenen ad-hoc veelste lang. Klasse dat je dit gemaakt heb. En vooral dat je het deelt met ons.

Grtz

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
12-11-2003 20:09

@rAW
ik denk niet dat ik dit werkende krijg voor oudere versies van GD2. De reden hiervoor is, dat ik niet zie hoe ik een True-Color plaatje moet maken ( als iemand dit weet.... graag ). Ik zou het wel werkende kunnen krijgen, maar je krijgt dan hele lelijke plaatjes.

Verder zou het logo het niet doen. Heb je wel naar de goede file verwezen? is het een GIF, PNG of JPEG? Is hij leesbaar voor het script? Graag wat meer info, want bij mij werkt het wel en ik weet nu niet hoe ik het probleem kan reproduceren.

1
<??>
20-11-2003 14:11

Hoi,

zoals ik ook commentaar op je vorige versie heb gegeven krijg je nu ook een klein beetje.

Ik vind het een leuk script alleen is het nogal ingewikkeld en snap ik persoonlijk niet zo wat nou wat doet.

Is het mogelijk dat je eens een paar functies bouwt, bijvoorbeeld

-> loadImage
-> Rotate(%,left/right)
-> Add text
-> add image
-> saveImage(%)

lijkt me een veel beter idee; en als het mag zou ik graag samen met jou wat van je script willen gebruiken voor een online site van mijn (please contact me @ xtentic ... @ ... hotmail ... com.)

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
24-11-2003 13:33

ik heb het scriopt nu in een pagina staan en de image_dir gezet op de map waar jpg's in staan....hoe kijrg ik het script werkend? hoe vraag ik die pagina op?

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
25-11-2003 17:08

Netjes jonge man! :)

Heb je zin om hier samen eens een goede editor voor te gaan schrijven? :D

Grtz,
mr Voornaam :)

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
26-11-2003 00:01

mooi script

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
Inloggen wachtwoord vergeten? Aanmelden
ADSL – Telfort Internet – Ziggo Alles in 1