• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Freegle / iznik-server / 0a312c38-58e2-4613-9dc7-19fb0c677c25

12 Jun 2024 08:10AM UTC coverage: 94.806% (-0.05%) from 94.856%
0a312c38-58e2-4613-9dc7-19fb0c677c25

push

circleci

edwh
Uploadcare - chat images

1 of 3 new or added lines in 1 file covered. (33.33%)

92 existing lines in 4 files now uncovered.

25424 of 26817 relevant lines covered (94.81%)

31.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.35
/http/api/image.php
1
<?php
2
namespace Freegle\Iznik;
3

4
function image() {
5
    global $dbhr, $dbhm;
28✔
6

7
    $ret = [ 'ret' => 100, 'status' => 'Unknown verb' ];
28✔
8
    $id = (Utils::presint('id', $_REQUEST, 0));
28✔
9
    $msgid = Utils::presint('msgid', $_REQUEST, NULL);
28✔
10
    $identify = array_key_exists('identify', $_REQUEST) ? filter_var($_REQUEST['identify'], FILTER_VALIDATE_BOOLEAN) : FALSE;
28✔
11
    $ocr = array_key_exists('ocr', $_REQUEST) ? filter_var($_REQUEST['ocr'], FILTER_VALIDATE_BOOLEAN) : FALSE;
28✔
12
    $objects = array_key_exists('objects', $_REQUEST) ? filter_var($_REQUEST['objects'], FILTER_VALIDATE_BOOLEAN) : FALSE;
28✔
13
    $group = Utils::presdef('group', $_REQUEST, NULL);
28✔
14
    $newsletter = Utils::presdef('newsletter', $_REQUEST, NULL);
28✔
15
    $communityevent = Utils::presdef('communityevent', $_REQUEST, NULL);
28✔
16
    $volunteering = Utils::presdef('volunteering', $_REQUEST, NULL);
28✔
17
    $chatmessage = Utils::presdef('chatmessage', $_REQUEST, NULL);
28✔
18
    $user = Utils::presdef('user', $_REQUEST, NULL);
28✔
19
    $newsfeed = Utils::presdef('newsfeed', $_REQUEST, NULL);
28✔
20
    $story = Utils::presdef('story', $_REQUEST, NULL);
28✔
21
    $noticeboard = Utils::presdef('noticeboard', $_REQUEST, NULL);
28✔
22
    $circle = Utils::presdef('circle', $_REQUEST, NULL);
28✔
23
    $imgtype = Utils::presdef('imgtype', $_REQUEST, Attachment::TYPE_MESSAGE);
28✔
24
    $externaluid = Utils::presdef('externaluid', $_REQUEST, NULL);
28✔
25
    $externalmods = Utils::presdef('externalmods', $_REQUEST, NULL);
28✔
26

27
    $sizelimit = 800;
28✔
28
    
29
    if ($chatmessage) {
28✔
30
        $type = Attachment::TYPE_CHAT_MESSAGE;
2✔
31
    } else if ($communityevent) {
27✔
32
        $type = Attachment::TYPE_COMMUNITY_EVENT;
1✔
33
    } else if ($volunteering) {
27✔
34
        $type = Attachment::TYPE_VOLUNTEERING;
1✔
35
    } else if ($newsletter) {
27✔
36
        $type = Attachment::TYPE_NEWSLETTER;
2✔
37
    } else if ($group) {
27✔
38
        $type = Attachment::TYPE_GROUP;
2✔
39
    } else if ($user) {
27✔
40
        $type = Attachment::TYPE_USER;
1✔
41
    } else if ($newsfeed) {
27✔
42
        $type = Attachment::TYPE_NEWSFEED;
2✔
43
    } else if ($story) {
26✔
44
        $type = Attachment::TYPE_STORY;
1✔
45
    } else if ($noticeboard) {
26✔
UNCOV
46
        $type = Attachment::TYPE_NOTICEBOARD;
×
47
    } else {
48
        $type = Attachment::TYPE_MESSAGE;
26✔
49
    }
50

51
    $_REQUEST['type'] = Utils::pres('typeoverride', $_REQUEST) ? $_REQUEST['typeoverride'] : $_REQUEST['type'];
28✔
52

53
    switch ($_REQUEST['type']) {
28✔
54
        case 'GET': {
28✔
55
            $a = new Attachment($dbhr, $dbhm, $id, $type);
3✔
56
            $redirect = $a->canRedirect();
3✔
57

58
            if ($redirect) {
3✔
59
                $ret = [
1✔
60
                    'ret' => 0,
1✔
61
                    'redirectto' => $redirect
1✔
62
                ];
1✔
63
            } else {
64
                $data = $a->getData();
2✔
65

66
                $i = new Image($data);
2✔
67

68
                $ret = [
2✔
69
                    'ret' => 1,
2✔
70
                    'status' => "Failed to create image $id of type $type",
2✔
71
                ];
2✔
72

73
                if ($i->img) {
2✔
74
                    $w = (Utils::presint('w', $_REQUEST, $i->width()));
2✔
75
                    $h = (Utils::presint('h', $_REQUEST, $i->height()));
2✔
76

77
                    if (($w > 0) || ($h > 0)) {
2✔
78
                        # Need to resize
79
                        $i->scale($w, $h);
2✔
80
                    }
81

82
                    if ($circle) {
2✔
83
                        $i->circle($w);
1✔
84
                        $ret = [
1✔
85
                            'ret' => 0,
1✔
86
                            'status' => 'Success',
1✔
87
                            'img' => $i->getDataPNG()
1✔
88
                        ];
1✔
89
                    } else {
90
                        $ret = [
2✔
91
                            'ret' => 0,
2✔
92
                            'status' => 'Success',
2✔
93
                            'img' => $i->getData()
2✔
94
                        ];
2✔
95
                    }
96
                }
97
            }
98

99
            break;
3✔
100
        }
101

102
        case 'POST': {
27✔
103
            $ret = [ 'ret' => 1, 'status' => 'No photo provided' ];
27✔
104

105
            # This next line is to simplify UT.
106
            $rotate = Utils::presint('rotate', $_REQUEST, NULL);
27✔
107

108
            if (array_key_exists('rotate', $_REQUEST)) {
27✔
109
                if ($id) {
1✔
110
                    # We want to rotate.  Do so.
111
                    $a = new Attachment($dbhr, $dbhm, $id, $type);
1✔
112
                    $a->rotate($rotate);
1✔
113

114
                    $ret = [
1✔
115
                        'ret' => 0,
1✔
116
                        'status' => 'Success',
1✔
117
                    ];
1✔
118
                }
119
            } else if ($externaluid) {
27✔
120
                // This is an external image.  Typically it's uploaded to a service like Uploadcare.
121
                $a = new Attachment($dbhr, $dbhm, NULL, $imgtype);
1✔
122
                list ($id, $uid) = $a->create($msgid, NULL, $externaluid, NULL, TRUE, $externalmods);
1✔
123

124
                $ret = [
1✔
125
                    'ret' => 0,
1✔
126
                    'status' => 'Success',
1✔
127
                    'id' => $id,
1✔
128
                    'uid' => $uid,
1✔
129
                ];
1✔
130
            } else {
131
                $photo = Utils::presdef('photo', $_FILES, NULL) ? $_FILES['photo'] : $_REQUEST['photo'];
26✔
132

133
                # Make sure what we have looks plausible - the file upload plugin should ensure this is the case.
134
                if ($photo &&
26✔
135
                    Utils::pres('tmp_name', $photo)) {
26✔
136
                    try {
137
                        # We may need to rotate.
138
                        $ret = [ 'ret' => 2, 'status' => 'Invalid data provided' ];
26✔
139
                        $data = file_get_contents($photo['tmp_name']);
26✔
140
                        $image = @imagecreatefromstring($data);
26✔
141

142
                        if ($image) {
26✔
143
                            $exif = @exif_read_data($photo['tmp_name']);
25✔
144

145
                            if($exif && !empty($exif['Orientation'])) {
25✔
146
                                switch($exif['Orientation']) {
19✔
147
                                    case 2:
19✔
148
                                        imageflip($image , IMG_FLIP_HORIZONTAL);
1✔
149
                                        break;
1✔
150
                                    case 3:
18✔
151
                                        $image = imagerotate($image,180,0);
1✔
152
                                        break;
1✔
153
                                    case 4:
17✔
154
                                        $image = imagerotate($image,180,0);
1✔
155
                                        imageflip($image , IMG_FLIP_HORIZONTAL);
1✔
156
                                        break;
1✔
157
                                    case 5:
16✔
158
                                        $image = imagerotate($image,90,0);
1✔
159
                                        imageflip ($image , IMG_FLIP_VERTICAL);
1✔
160
                                        break;
1✔
161
                                    case 6:
15✔
162
                                        $image = imagerotate($image,-90,0);
1✔
163
                                        break;
1✔
164
                                    case 7:
14✔
165
                                        $image = imagerotate($image,-90,0);
1✔
166
                                        imageflip ($image , IMG_FLIP_VERTICAL);
1✔
167
                                        break;
1✔
168
                                    case 8:
13✔
169
                                        $image = imagerotate($image,90,0);
1✔
170
                                        break;
1✔
171
                                }
172

173
                                ob_start();
19✔
174
                                imagejpeg($image, NULL, 100);
19✔
175
                                $data = ob_get_contents();
19✔
176
                                ob_end_clean();
19✔
177
                            }
178

179
                            if ($data) {
25✔
180
                                $a = new Attachment($dbhr, $dbhm, NULL, $imgtype);
25✔
181
                                $id = $a->create($msgid,$data);
25✔
182

183
                                # Make sure it's not too large, to keep DB size down.  Ought to have been resized by
184
                                # client, but you never know.
185
                                $data = $a->getData();
25✔
186
                                $i = new Image($data);
25✔
187

188
                                if ($i && $i->img) {
25✔
189
                                    $h = $i->height();
25✔
190
                                    $w = $i->width();
25✔
191

192
                                    if ($w > $sizelimit) {
25✔
193
                                        $h = $h * $sizelimit / $w;
11✔
194
                                        $w = $sizelimit;
11✔
195
                                        $i->scale($w, $h);
11✔
196
                                        $data = $i->getData(100);
11✔
197
                                        $a->setPrivate('data', $data);
11✔
198
                                    }
199
                                }
200

201
                                $ret = [
25✔
202
                                    'ret' => 0,
25✔
203
                                    'status' => 'Success',
25✔
204
                                    'id' => $id,
25✔
205
                                    'path' => $a->getPath(FALSE),
25✔
206
                                    'paththumb' => $a->getPath(TRUE)
25✔
207
                                ];
25✔
208

209
                                # Return a new thumbnail (which might be a different orientation).
210
                                $ret['initialPreview'] =  [
25✔
211
                                    '<img src="' . $a->getPath(TRUE) . '" class="file-preview-image img-responsive">',
25✔
212
                                ];
25✔
213

214
                                $ret['initialPreviewConfig'] = [
25✔
215
                                    [
25✔
216
                                        'key' => $id
25✔
217
                                    ]
25✔
218
                                ];
25✔
219

220
                                $ret['append'] = TRUE;
25✔
221

222
                                if ($identify) {
25✔
223
                                    $a = new Attachment($dbhr, $dbhm, $id);
2✔
224
                                    $ret['items'] = $a->identify();
2✔
225
                                }
226

227
                                if ($ocr) {
25✔
228
                                    $a = new Attachment($dbhr, $dbhm, $id, $type);
1✔
229
                                    $ret['ocr'] = $a->ocr();
1✔
230
                                }
231

232
                                if ($objects) {
25✔
233
                                    $a = new Attachment($dbhr, $dbhm, $id, $type);
1✔
234
                                    $ret['objects'] = $a->objects();
26✔
235
                                }
236
                            }
237
                        }
UNCOV
238
                    } catch (\Exception $e) {
×
UNCOV
239
                        $ret = [ 'ret' => 5, 'status' => "Image create failed " . $e->getMessage() ];
×
240
                    }
241
                }
242

243
                # Uploader code requires this field.
244
                $ret['error'] = $ret['ret'] == 0 ? NULL : $ret['status'];
26✔
245
            }
246

247
            break;
27✔
248
        }
249

250
        case 'DELETE': {
19✔
251
            # This is used by the client bootstrap-fileinput to delete images.  But we don't actually delete them,
252
            # in case we need them for debug.  The client will later patch the message to remove them.
253
            $ret = [
19✔
254
                'ret' => 0,
19✔
255
                'status' => 'Success'
19✔
256
            ];
19✔
257

258
            break;
19✔
259
        }
260
    }
261

262
    return($ret);
28✔
263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc