]> git.mxchange.org Git - friendica.git/blob - include/Photo.php
new todo list
[friendica.git] / include / Photo.php
1 <?php
2
3 if(! class_exists("Photo")) {
4 class Photo {
5
6         private $image;
7         private $width;
8         private $height;
9
10         public function __construct($data) {
11                 $this->image = @imagecreatefromstring($data);
12                 if($this->image !== FALSE) {
13                         $this->width  = imagesx($this->image);
14                         $this->height = imagesy($this->image);
15                 }
16         }
17
18         public function __destruct() {
19                 if($this->image)
20                         imagedestroy($this->image);
21         }
22
23         public function getWidth() {
24                 return $this->width;
25         }
26
27         public function getHeight() {
28                 return $this->height;
29         }
30
31         public function getImage() {
32                 return $this->image;
33         }
34
35         public function scaleImage($max) {
36
37                 $width = $this->width;
38                 $height = $this->height;
39
40                 $dest_width = $dest_height = 0;
41
42                 if((! $width)|| (! $height))
43                         return FALSE;
44
45                 if($width > $max && $height > $max) {
46                         if($width > $height) {
47                                 $dest_width = $max;
48                                 $dest_height = intval(( $height * $max ) / $width);
49                         }
50                         else {
51                                 $dest_width = intval(( $width * $max ) / $height);
52                                 $dest_height = $max;
53                         }
54                 }
55                 else {
56                         if( $width > $max ) {
57                                 $dest_width = $max;
58                                 $dest_height = intval(( $height * $max ) / $width);
59                         }
60                         else {
61                                 if( $height > $max ) {
62                                         $dest_width = intval(( $width * $max ) / $height);
63                                         $dest_height = $max;
64                                 }
65                                 else {
66                                         $dest_width = $width;
67                                         $dest_height = $height;
68                                 }
69                         }
70                 }
71
72
73                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
74                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
75                 if($this->image)
76                         imagedestroy($this->image);
77                 $this->image = $dest;
78                 $this->width  = imagesx($this->image);
79                 $this->height = imagesy($this->image);
80
81         }
82
83
84
85         public function scaleImageUp($min) {
86
87                 $width = $this->width;
88                 $height = $this->height;
89
90                 $dest_width = $dest_height = 0;
91
92                 if((! $width)|| (! $height))
93                         return FALSE;
94
95                 if($width < $min && $height < $min) {
96                         if($width > $height) {
97                                 $dest_width = $min;
98                                 $dest_height = intval(( $height * $min ) / $width);
99                         }
100                         else {
101                                 $dest_width = intval(( $width * $min ) / $height);
102                                 $dest_height = $min;
103                         }
104                 }
105                 else {
106                         if( $width < $min ) {
107                                 $dest_width = $min;
108                                 $dest_height = intval(( $height * $min ) / $width);
109                         }
110                         else {
111                                 if( $height < $min ) {
112                                         $dest_width = intval(( $width * $min ) / $height);
113                                         $dest_height = $min;
114                                 }
115                                 else {
116                                         $dest_width = $width;
117                                         $dest_height = $height;
118                                 }
119                         }
120                 }
121
122
123                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
124                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
125                 if($this->image)
126                         imagedestroy($this->image);
127                 $this->image = $dest;
128                 $this->width  = imagesx($this->image);
129                 $this->height = imagesy($this->image);
130
131         }
132
133
134
135         public function scaleImageSquare($dim) {
136
137                 $dest = imagecreatetruecolor( $dim, $dim );
138                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
139                 if($this->image)
140                         imagedestroy($this->image);
141                 $this->image = $dest;
142                 $this->width  = imagesx($this->image);
143                 $this->height = imagesy($this->image);
144         }
145
146
147         public function cropImage($max,$x,$y,$w,$h) {
148                 $dest = imagecreatetruecolor( $max, $max );
149                 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
150                 if($this->image)
151                         imagedestroy($this->image);
152                 $this->image = $dest;
153                 $this->width  = imagesx($this->image);
154                 $this->height = imagesy($this->image);
155         }
156
157         public function saveImage($path) {
158                 imagejpeg($this->image,$path,100);
159         }
160
161         public function imageString() {
162                 ob_start();
163                 imagejpeg($this->image,NULL,100);
164                 $s = ob_get_contents();
165                 ob_end_clean();
166                 return $s;
167         }
168
169
170 }}
171