]> git.mxchange.org Git - friendica.git/blob - include/Photo.php
photo editing
[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         public function store($uid, $cid, $rid, $filename, $album, $scale, 
172                 $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
173
174                 $r = q("INSERT INTO `photo`
175                         ( `uid`, `contact-id`, `resource-id`, `created`, `edited`, `filename`, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
176                         VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
177                         intval($uid),
178                         intval($cid),
179                         dbesc($rid),
180                         dbesc(datetime_convert()),
181                         dbesc(datetime_convert()),
182                         dbesc(basename($filename)),
183                         dbesc($album),
184                         intval($this->height),
185                         intval($this->width),
186                         dbesc($this->imageString()),
187                         intval($scale),
188                         intval($profile),
189                         dbesc($allow_cid),
190                         dbesc($allow_gid),
191                         dbesc($deny_cid),
192                         dbesc($deny_gid)
193                 );
194                 return $r;
195         }
196
197
198
199
200
201 }}
202
203