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

Freegle / iznik-server / a079ad69-f343-46da-af1d-7c233ca9a57a

13 Aug 2025 04:16PM UTC coverage: 90.38% (-0.02%) from 90.401%
a079ad69-f343-46da-af1d-7c233ca9a57a

push

circleci

edwh
WIP: refactor phpunit tests to have less horrific amounts of duplicate code.

25931 of 28691 relevant lines covered (90.38%)

30.96 hits per line

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

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

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

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

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

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

52
    switch ($_REQUEST['type']) {
26✔
53
        case 'GET': {
26✔
54
            $w = Utils::presint('w', $_REQUEST, 0);
2✔
55
            $h = Utils::presint('h', $_REQUEST, 0);
2✔
56

57
            $a = new Attachment($dbhr, $dbhm, $id, $type);
2✔
58
            $redirect = $a->canRedirect($w, $h);
2✔
59

60
            if ($redirect) {
2✔
61
                $ret = [
1✔
62
                    'ret' => 0,
1✔
63
                    'redirectto' => $redirect
1✔
64
                ];
1✔
65
            } else if (!$id) {
1✔
66
                # It's not clear how this arises, but we've seen gimg_0.jpg.
67
                $ret = [
×
68
                    'ret' => 0,
×
69
                    'redirectto' => 'https://' . USER_DOMAIN . '/defaultprofile.png'
×
70
                ];
×
71
            } else {
72
                $data = $a->getData();
1✔
73

74
                $i = new Image($data);
1✔
75

76
                $ret = [
1✔
77
                    'ret' => 1,
1✔
78
                    'status' => "Failed to create image $id of type $type",
1✔
79
                ];
1✔
80

81
                if ($i->img) {
1✔
82
                    $w = $w ? $w : $i->width();
×
83
                    $h = $h ? $h : $i->height();
×
84

85
                    if (($w > 0) || ($h > 0)) {
×
86
                        # Need to resize
87
                        $i->scale($w, $h);
×
88
                    }
89

90
                    if ($circle) {
×
91
                        $i->circle($w);
×
92
                        $ret = [
×
93
                            'ret' => 0,
×
94
                            'status' => 'Success',
×
95
                            'img' => $i->getDataPNG()
×
96
                        ];
×
97
                    } else {
98
                        $ret = [
×
99
                            'ret' => 0,
×
100
                            'status' => 'Success',
×
101
                            'img' => $i->getData()
×
102
                        ];
×
103
                    }
104
                }
105
            }
106

107
            break;
2✔
108
        }
109

110
        case 'POST': {
25✔
111
            $ret = [ 'ret' => 1, 'status' => 'No photo provided' ];
25✔
112

113
            $rotate = Utils::presint('rotate', $_REQUEST, NULL);
25✔
114
            $raterecognise = Utils::presdef('raterecognise', $_REQUEST, NULL);
25✔
115

116
            if ($raterecognise) {
25✔
117
                $dbhm->preExec("UPDATE messages_attachments_recognise SET rating = ? WHERE attid = ?", [
×
118
                    $raterecognise,
×
119
                    $id
×
120
                ]);
×
121

122
                $ret = [
×
123
                    'ret' => 0,
×
124
                    'status' => 'Success',
×
125
                ];
×
126
            } else if (array_key_exists('rotate', $_REQUEST)) {
25✔
127
                if ($id) {
1✔
128
                    # We want to rotate.  Do so.
129
                    $a = new Attachment($dbhr, $dbhm, $id, $type);
1✔
130
                    $a->rotate($rotate);
1✔
131

132
                    $ret = [
1✔
133
                        'ret' => 0,
1✔
134
                        'status' => 'Success',
1✔
135
                    ];
1✔
136
                }
137
            } else if ($externaluid) {
25✔
138
                // This is an external image i.e. not in the DB.  Currently this is only uploaded to our own
139
                // storage via tusd.
140
                $a = new Attachment($dbhr, $dbhm, NULL, $imgtype);
1✔
141
                list ($id, $uid) = $a->create($msgid, NULL, $externaluid, NULL, TRUE, $externalmods, NULL);
1✔
142

143
                $ret = [
1✔
144
                    'ret' => 0,
1✔
145
                    'status' => 'Success',
1✔
146
                    'id' => $id,
1✔
147
                    'uid' => $uid,
1✔
148
                ];
1✔
149

150
                if ($recognise) {
1✔
151
                    # We'd like to use AI to identify the image.
152
                    # TODO Not doing this here as slow and sometimes flaky; need to background.
153
                    #$ret['info'] = $a->recognise();
154
                }
155
            } else {
156
                $photo = Utils::presdef('photo', $_FILES, NULL) ? $_FILES['photo'] : $_REQUEST['photo'];
24✔
157

158
                # Make sure what we have looks plausible - the file upload plugin should ensure this is the case.
159
                if ($photo &&
24✔
160
                    Utils::pres('tmp_name', $photo)) {
24✔
161
                    try {
162
                        # We may need to rotate.
163
                        $ret = [ 'ret' => 2, 'status' => 'Invalid data provided' ];
24✔
164
                        $data = file_get_contents($photo['tmp_name']);
24✔
165
                        $image = @imagecreatefromstring($data);
24✔
166

167
                        if ($image) {
24✔
168
                            $exif = @exif_read_data($photo['tmp_name']);
23✔
169

170
                            if($exif && !empty($exif['Orientation'])) {
23✔
171
                                switch($exif['Orientation']) {
19✔
172
                                    case 2:
19✔
173
                                        imageflip($image , IMG_FLIP_HORIZONTAL);
1✔
174
                                        break;
1✔
175
                                    case 3:
18✔
176
                                        $image = imagerotate($image,180,0);
1✔
177
                                        break;
1✔
178
                                    case 4:
17✔
179
                                        $image = imagerotate($image,180,0);
1✔
180
                                        imageflip($image , IMG_FLIP_HORIZONTAL);
1✔
181
                                        break;
1✔
182
                                    case 5:
16✔
183
                                        $image = imagerotate($image,90,0);
1✔
184
                                        imageflip ($image , IMG_FLIP_VERTICAL);
1✔
185
                                        break;
1✔
186
                                    case 6:
15✔
187
                                        $image = imagerotate($image,-90,0);
1✔
188
                                        break;
1✔
189
                                    case 7:
14✔
190
                                        $image = imagerotate($image,-90,0);
1✔
191
                                        imageflip ($image , IMG_FLIP_VERTICAL);
1✔
192
                                        break;
1✔
193
                                    case 8:
13✔
194
                                        $image = imagerotate($image,90,0);
1✔
195
                                        break;
1✔
196
                                }
197

198
                                ob_start();
19✔
199
                                imagejpeg($image, NULL, 100);
19✔
200
                                $data = ob_get_contents();
19✔
201
                                ob_end_clean();
19✔
202
                            }
203

204
                            if ($data) {
23✔
205
                                $a = new Attachment($dbhr, $dbhm, NULL, $imgtype);
23✔
206
                                list ($id, $uid) = $a->create($msgid,$data);
23✔
207

208
                                # Make sure it's not too large, to keep DB size down.  Ought to have been resized by
209
                                # client, but you never know.
210
                                $data = $a->getData();
23✔
211
                                $i = new Image($data);
23✔
212

213
                                if ($i && $i->img) {
23✔
214
                                    $h = $i->height();
7✔
215
                                    $w = $i->width();
7✔
216

217
                                    if ($w > $sizelimit) {
7✔
218
                                        $h = $h * $sizelimit / $w;
×
219
                                        $w = $sizelimit;
×
220
                                        $i->scale($w, $h);
×
221
                                        $data = $i->getData(100);
×
222
                                        $a->setPrivate('data', $data);
×
223
                                    }
224
                                }
225

226
                                $ret = [
23✔
227
                                    'ret' => 0,
23✔
228
                                    'status' => 'Success',
23✔
229
                                    'id' => $id,
23✔
230
                                    'path' => $a->getPath(FALSE),
23✔
231
                                    'paththumb' => $a->getPath(TRUE)
23✔
232
                                ];
23✔
233

234
                                # Return a new thumbnail (which might be a different orientation).
235
                                $ret['initialPreview'] =  [
23✔
236
                                    '<img src="' . $a->getPath(TRUE) . '" class="file-preview-image img-responsive">',
23✔
237
                                ];
23✔
238

239
                                $ret['initialPreviewConfig'] = [
23✔
240
                                    [
23✔
241
                                        'key' => $id
23✔
242
                                    ]
23✔
243
                                ];
23✔
244

245
                                $ret['append'] = TRUE;
23✔
246

247
                                if ($ocr) {
23✔
248
                                    $a = new Attachment($dbhr, $dbhm, $id, $type);
×
249
                                    $ret['ocr'] = $a->ocr();
24✔
250
                                }
251
                            }
252
                        }
253
                    } catch (\Exception $e) {
×
254
                        $ret = [ 'ret' => 5, 'status' => "Image create failed " . $e->getMessage() ];
×
255
                    }
256
                }
257

258
                # Uploader code requires this field.
259
                $ret['error'] = $ret['ret'] == 0 ? NULL : $ret['status'];
24✔
260
            }
261

262
            break;
25✔
263
        }
264

265
        case 'DELETE': {
19✔
266
            # This is used by the client bootstrap-fileinput to delete images.  But we don't actually delete them,
267
            # in case we need them for debug.  The client will later patch the message to remove them.
268
            $ret = [
19✔
269
                'ret' => 0,
19✔
270
                'status' => 'Success'
19✔
271
            ];
19✔
272

273
            break;
19✔
274
        }
275
    }
276

277
    return($ret);
26✔
278
}
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

© 2026 Coveralls, Inc