]> git.mxchange.org Git - friendica.git/blob - include/Photo.php
de-duplicate photo importation logic
[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         private $valid;
10
11         public function __construct($data) {
12                 $this->valid = false;
13                 $this->image = @imagecreatefromstring($data);
14                 if($this->image !== FALSE) {
15                         $this->width  = imagesx($this->image);
16                         $this->height = imagesy($this->image);
17                         $this->valid  = true;
18                 }
19         }
20
21         public function __destruct() {
22                 if($this->image)
23                         imagedestroy($this->image);
24         }
25
26         public function is_valid() {
27                 return $this->valid;
28         }
29
30         public function getWidth() {
31                 return $this->width;
32         }
33
34         public function getHeight() {
35                 return $this->height;
36         }
37
38         public function getImage() {
39                 return $this->image;
40         }
41
42         public function scaleImage($max) {
43
44                 $width = $this->width;
45                 $height = $this->height;
46
47                 $dest_width = $dest_height = 0;
48
49                 if((! $width)|| (! $height))
50                         return FALSE;
51
52                 if($width > $max && $height > $max) {
53                         if($width > $height) {
54                                 $dest_width = $max;
55                                 $dest_height = intval(( $height * $max ) / $width);
56                         }
57                         else {
58                                 $dest_width = intval(( $width * $max ) / $height);
59                                 $dest_height = $max;
60                         }
61                 }
62                 else {
63                         if( $width > $max ) {
64                                 $dest_width = $max;
65                                 $dest_height = intval(( $height * $max ) / $width);
66                         }
67                         else {
68                                 if( $height > $max ) {
69                                         $dest_width = intval(( $width * $max ) / $height);
70                                         $dest_height = $max;
71                                 }
72                                 else {
73                                         $dest_width = $width;
74                                         $dest_height = $height;
75                                 }
76                         }
77                 }
78
79
80                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
81                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
82                 if($this->image)
83                         imagedestroy($this->image);
84                 $this->image = $dest;
85                 $this->width  = imagesx($this->image);
86                 $this->height = imagesy($this->image);
87
88         }
89
90
91
92         public function scaleImageUp($min) {
93
94                 $width = $this->width;
95                 $height = $this->height;
96
97                 $dest_width = $dest_height = 0;
98
99                 if((! $width)|| (! $height))
100                         return FALSE;
101
102                 if($width < $min && $height < $min) {
103                         if($width > $height) {
104                                 $dest_width = $min;
105                                 $dest_height = intval(( $height * $min ) / $width);
106                         }
107                         else {
108                                 $dest_width = intval(( $width * $min ) / $height);
109                                 $dest_height = $min;
110                         }
111                 }
112                 else {
113                         if( $width < $min ) {
114                                 $dest_width = $min;
115                                 $dest_height = intval(( $height * $min ) / $width);
116                         }
117                         else {
118                                 if( $height < $min ) {
119                                         $dest_width = intval(( $width * $min ) / $height);
120                                         $dest_height = $min;
121                                 }
122                                 else {
123                                         $dest_width = $width;
124                                         $dest_height = $height;
125                                 }
126                         }
127                 }
128
129
130                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
131                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
132                 if($this->image)
133                         imagedestroy($this->image);
134                 $this->image = $dest;
135                 $this->width  = imagesx($this->image);
136                 $this->height = imagesy($this->image);
137
138         }
139
140
141
142         public function scaleImageSquare($dim) {
143
144                 $dest = imagecreatetruecolor( $dim, $dim );
145                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
146                 if($this->image)
147                         imagedestroy($this->image);
148                 $this->image = $dest;
149                 $this->width  = imagesx($this->image);
150                 $this->height = imagesy($this->image);
151         }
152
153
154         public function cropImage($max,$x,$y,$w,$h) {
155                 $dest = imagecreatetruecolor( $max, $max );
156                 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
157                 if($this->image)
158                         imagedestroy($this->image);
159                 $this->image = $dest;
160                 $this->width  = imagesx($this->image);
161                 $this->height = imagesy($this->image);
162         }
163
164         public function saveImage($path) {
165                 imagejpeg($this->image,$path,100);
166         }
167
168         public function imageString() {
169                 ob_start();
170                 imagejpeg($this->image,NULL,100);
171                 $s = ob_get_contents();
172                 ob_end_clean();
173                 return $s;
174         }
175
176
177
178         public function store($uid, $cid, $rid, $filename, $album, $scale, 
179                 $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
180
181                 $r = q("INSERT INTO `photo`
182                         ( `uid`, `contact-id`, `resource-id`, `created`, `edited`, `filename`, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
183                         VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
184                         intval($uid),
185                         intval($cid),
186                         dbesc($rid),
187                         dbesc(datetime_convert()),
188                         dbesc(datetime_convert()),
189                         dbesc(basename($filename)),
190                         dbesc($album),
191                         intval($this->height),
192                         intval($this->width),
193                         dbesc($this->imageString()),
194                         intval($scale),
195                         intval($profile),
196                         dbesc($allow_cid),
197                         dbesc($allow_gid),
198                         dbesc($deny_cid),
199                         dbesc($deny_gid)
200                 );
201                 return $r;
202         }
203
204
205
206
207
208 }}
209
210
211 function import_profile_photo($photo,$uid,$cid) {
212
213         $a = get_app();
214
215         $photo_failure = false;
216
217         $filename = basename($photo);
218         $img_str = fetch_url($photo,true);
219         $img = new Photo($img_str);
220         if($img->is_valid()) {
221
222                 $img->scaleImageSquare(175);
223                                         
224                 $hash = photo_new_resource();
225
226                 $r = $img->store($uid, $cid, $hash, $filename, t('Contact Photos'), 4 );
227
228                 if($r === false)
229                         $photo_failure = true;
230
231                 $img->scaleImage(80);
232
233                 $r = $img->store($uid, $cid, $hash, $filename, t('Contact Photos'), 5 );
234
235                 if($r === false)
236                         $photo_failure = true;
237
238                 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
239                 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
240         }
241         else
242                 $photo_failure = true;
243
244         if($photo_failure) {
245                 $photo = $a->get_baseurl() . '/images/default-profile.jpg';
246                 $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
247         }
248
249         return(array($photo,$thumb));
250
251 }