Passed
Branch master (410c7b)
by Michael
03:30
created

ImageHandler   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 638
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 58
eloc 311
dl 0
loc 638
rs 4.5599
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B getObjects() 0 32 7
A renderFormEdit() 0 23 1
A delete() 0 22 4
A create() 0 15 2
B resizeImage() 0 68 7
A getLastPictures() 0 22 2
B insert2() 0 69 9
A deleteAll() 0 14 4
A receivePicture() 0 73 3
A getLastPicturesForBlock() 0 25 2
A getCount() 0 14 4
A renderFormSubmit() 0 19 1
B makeAvatar() 0 66 7
A get2() 0 17 3
A __construct() 0 12 2

How to fix   Complexity   

Complex Class

Complex classes like ImageHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ImageHandler, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
use CriteriaElement;
18
use Xmf\Request;
19
use XoopsDatabase;
20
use XoopsFormButton;
21
use XoopsFormFile;
22
use XoopsFormHidden;
23
use XoopsFormLabel;
24
use XoopsFormText;
25
use XoopsMediaUploader;
26
use XoopsObject;
27
use XoopsPersistableObjectHandler;
28
use XoopsThemeForm;
29
30
/**
31
 * @category        Module
32
 * @package         suico
33
 * @copyright       {@link https://xoops.org/ XOOPS Project}
34
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
35
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
36
 */
37
38
/**
39
 * Protection against inclusion outside the site
40
 */
41
if (!\defined('XOOPS_ROOT_PATH')) {
42
    die('XOOPS root path not defined');
43
}
44
45
/**
46
 * Includes of form objects and uploader
47
 */
48
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
49
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
50
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
51
52
// -------------------------------------------------------------------------
53
// ------------------Image user handler class -------------------
54
// -------------------------------------------------------------------------
55
56
/**
57
 * suico_imageshandler class.
58
 * This class provides simple mechanism for Image object and generate forms for inclusion etc
59
 */
60
class ImageHandler extends XoopsPersistableObjectHandler
61
{
62
    public $helper;
63
64
    public $isAdmin;
65
66
    /**
67
     * Constructor
68
     * @param \XoopsDatabase|null              $xoopsDatabase
69
     * @param \XoopsModules\Suico\Helper|null $helper
70
     */
71
    public function __construct(
72
        ?XoopsDatabase $xoopsDatabase = null,
73
        $helper = null
74
    ) {
75
        /** @var \XoopsModules\Suico\Helper $this ->helper */
76
        if (null === $helper) {
77
            $this->helper = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper does not seem to exist on XoopsModules\Suico\Helper.
Loading history...
78
        } else {
79
            $this->helper = $helper;
80
        }
81
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
82
        parent::__construct($xoopsDatabase, 'suico_images', Image::class, 'cod_img', 'title', 'caption');
0 ignored issues
show
Unused Code introduced by
The call to XoopsPersistableObjectHandler::__construct() has too many arguments starting with 'caption'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        parent::/** @scrutinizer ignore-call */ 
83
                __construct($xoopsDatabase, 'suico_images', Image::class, 'cod_img', 'title', 'caption');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
83
    }
84
85
    /**
86
     * create a new Groups
87
     *
88
     * @param bool $isNew flag the new objects as "new"?
89
     * @return \XoopsObject Groups
90
     */
91
    public function create(
92
        $isNew = true
93
    ) {
94
        $obj = parent::create($isNew);
95
        //        if ($isNew) {
96
        //            $obj->setDefaultPermissions();
97
        //        }
98
        if ($isNew) {
99
            $obj->setNew();
100
        } else {
101
            $obj->unsetNew();
102
        }
103
        $obj->helper = $this->helper;
104
105
        return $obj;
106
    }
107
108
    /**
109
     * retrieve a Image
110
     *
111
     * @param int  $id of the Image
112
     * @param null $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
113
     * @return mixed reference to the {@link Image} object, FALSE if failed
114
     */
115
    public function get2(
116
        $id = null,
117
        $fields = null
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

117
        /** @scrutinizer ignore-unused */ $fields = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    ) {
119
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_images') . ' WHERE cod_img=' . $id;
120
        if (!$result = $this->db->query($sql)) {
121
            return false;
122
        }
123
        $numrows = $this->db->getRowsNum($result);
124
        if (1 === $numrows) {
125
            $image = new Image();
126
            $image->assignVars($this->db->fetchArray($result));
127
128
            return $image;
129
        }
130
131
        return false;
132
    }
133
134
    /**
135
     * insert a new Image in the database
136
     *
137
     * @param \XoopsObject $xoopsObject   reference to the {@link Image} object
138
     * @param bool         $force
139
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
140
     */
141
    public function insert2(
142
        XoopsObject $xoopsObject,
143
        $force = false
144
    ) {
145
        global $xoopsConfig;
146
        if (!$xoopsObject instanceof Image) {
147
            return false;
148
        }
149
        if (!$xoopsObject->isDirty()) {
150
            return true;
151
        }
152
        if (!$xoopsObject->cleanVars()) {
153
            return false;
154
        }
155
156
        $cod_img = '';
157
        foreach ($xoopsObject->cleanVars as $k => $v) {
158
            ${$k} = $v;
159
        }
160
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
161
        if ($xoopsObject->isNew()) {
162
            // ajout/modification d'un Image
163
            $xoopsObject = new Image();
164
            $format      = 'INSERT INTO %s (cod_img, title, caption, date_created, date_updated, uid_owner, filename, private)';
165
            $format      .= 'VALUES (%u, %s, %s, %s, %s, %s, %s, 0)';
166
            $sql         = \sprintf(
167
                $format,
168
                $this->db->prefix('suico_images'),
169
                $cod_img,
170
                $this->db->quoteString($title),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title seems to be never defined.
Loading history...
171
                $this->db->quoteString($caption),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $caption seems to be never defined.
Loading history...
172
                \time(),//$now,
173
                \time(),//$now,
174
                $this->db->quoteString($uid_owner),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid_owner seems to be never defined.
Loading history...
175
                $this->db->quoteString($filename)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filename seems to be never defined.
Loading history...
176
            );
177
            $force       = true;
178
        } else {
179
            $format = 'UPDATE %s SET ';
180
            $format .= 'cod_img=%u, title=%s, caption=%s, date_created=%s, date_updated=%s, uid_owner=%s, filename=%s, private=%s';
181
            $format .= ' WHERE cod_img = %u';
182
            $sql    = \sprintf(
183
                $format,
184
                $this->db->prefix('suico_images'),
185
                $cod_img,
186
                $this->db->quoteString($title),
187
                $this->db->quoteString($caption),
188
                $xoopsObject->getVar('date_created'), // $now,
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('date_created') can also be of type array and array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
                /** @scrutinizer ignore-type */ $xoopsObject->getVar('date_created'), // $now,
Loading history...
189
                $xoopsObject->getVar('date_updated'), // $now,
190
                $this->db->quoteString($uid_owner),
191
                $this->db->quoteString($filename),
192
                $this->db->quoteString($private),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $private seems to be never defined.
Loading history...
193
                $cod_img
194
            );
195
        }
196
        if ($force) {
197
            $result = $this->db->queryF($sql);
198
        } else {
199
            $result = $this->db->query($sql);
200
        }
201
        if (!$result) {
202
            return false;
203
        }
204
        if (empty($cod_img)) {
0 ignored issues
show
introduced by
The condition empty($cod_img) is always true.
Loading history...
205
            $cod_img = $this->db->getInsertId();
206
        }
207
        $xoopsObject->assignVar('cod_img', $cod_img);
208
209
        return true;
210
    }
211
212
    /**
213
     * delete a Image from the database
214
     *
215
     * @param \XoopsObject $xoopsObject reference to the Image to delete
216
     * @param bool         $force
217
     * @return bool FALSE if failed.
218
     */
219
    public function delete(
220
        XoopsObject $xoopsObject,
221
        $force = false
222
    ) {
223
        if (!$xoopsObject instanceof Image) {
224
            return false;
225
        }
226
        $sql = \sprintf(
227
            'DELETE FROM %s WHERE cod_img = %u',
228
            $this->db->prefix('suico_images'),
229
            $xoopsObject->getVar('cod_img')
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('cod_img') can also be of type array and array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

229
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('cod_img')
Loading history...
230
        );
231
        if ($force) {
232
            $result = $this->db->queryF($sql);
233
        } else {
234
            $result = $this->db->query($sql);
235
        }
236
        if (!$result) {
237
            return false;
238
        }
239
240
        return true;
241
    }
242
243
    /**
244
     * retrieve suico_imagess from the database
245
     *
246
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
247
     * @param bool                                 $id_as_key       use the UID as key for the array?
248
     * @param bool                                 $as_object
249
     * @return array array of {@link Image} objects
250
     */
251
    public function &getObjects(
252
        ?CriteriaElement $criteriaElement = null,
253
        $id_as_key = false,
254
        $as_object = true
255
    ) {
256
        $ret   = [];
257
        $limit = $start = 0;
258
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_images');
259
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
260
            $sql .= ' ' . $criteriaElement->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
            $sql .= ' ' . $criteriaElement->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
261
            if ('' !== $criteriaElement->getSort()) {
262
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
263
            }
264
            $limit = $criteriaElement->getLimit();
265
            $start = $criteriaElement->getStart();
266
        }
267
        $result = $this->db->query($sql, $limit, $start);
268
        if (!$result) {
269
            return $ret;
270
        }
271
        while (false !== ($myrow = $this->db->fetchArray($result))) {
272
            $image = new Image();
273
            $image->assignVars($myrow);
274
            if (!$id_as_key) {
275
                $ret[] = &$image;
276
            } else {
277
                $ret[$myrow['cod_img']] = &$image;
278
            }
279
            unset($image);
280
        }
281
282
        return $ret;
283
    }
284
285
    /**
286
     * count suico_imagess matching a condition
287
     *
288
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
289
     * @return int count of suico_imagess
290
     */
291
    public function getCount(
292
        ?CriteriaElement $criteriaElement = null
293
    ) {
294
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_images');
295
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
296
            $sql .= ' ' . $criteriaElement->renderWhere();
297
        }
298
        $result = $this->db->query($sql);
299
        if (!$result) {
300
            return 0;
301
        }
302
        [$count] = $this->db->fetchRow($result);
303
304
        return (int)$count;
305
    }
306
307
    /**
308
     * delete suico_imagess matching a set of conditions
309
     *
310
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
311
     * @param bool                                 $force
312
     * @param bool                                 $asObject
313
     * @return bool FALSE if deletion failed
314
     */
315
    public function deleteAll(
316
        ?CriteriaElement $criteriaElement = null,
317
        $force = true,
318
        $asObject = false
319
    ) {
320
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_images');
321
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
322
            $sql .= ' ' . $criteriaElement->renderWhere();
323
        }
324
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
325
            return false;
326
        }
327
328
        return true;
329
    }
330
331
    /**
332
     * Render a form to send pictures
333
     *
334
     * @param int       $maxbytes the maximum size of a picture
335
     * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered
336
     * @return bool TRUE
337
     *
338
     * obs: Some functions wont work on php 4 so edit lines down under acording to your version
339
     */
340
    public function renderFormSubmit(
341
        $maxbytes,
342
        $xoopsTpl
343
    ) {
344
        $form          = new XoopsThemeForm(\_MD_SUICO_SUBMIT_PIC_TITLE, 'form_picture', 'submitImage.php', 'post', true);
345
        $field_url     = new XoopsFormFile(\_MD_SUICO_SELECT_PHOTO, 'sel_photo', 2000000);
346
        $field_title   = new XoopsFormText(\_MD_SUICO_PHOTOTITLE, 'title', 35, 55);
347
        $field_caption = new XoopsFormText(\_MD_SUICO_CAPTION, 'caption', 35, 55);
348
        $form->setExtra('enctype="multipart/form-data"');
349
        $buttonSend    = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADPICTURE, 'submit');
350
        $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024));
351
        $form->addElement($field_warning);
352
        $form->addElement($field_url, true);
353
        $form->addElement($field_title);
354
        $form->addElement($field_caption);
355
356
        $form->addElement($buttonSend);
357
        $form->assign($xoopsTpl); //If your server is php 5
358
        return true;
359
    }
360
361
    /**
362
     * Render a form to edit the description of the pictures
363
     *
364
     * @param        $title
365
     * @param string $caption  The description of the picture
366
     * @param int    $cod_img  the id of the image in database
367
     * @param string $filename the url to the thumb of the image so it can be displayed
368
     * @return bool TRUE
369
     */
370
    public function renderFormEdit(
371
        $title,
372
        $caption,
373
        $cod_img,
374
        $filename
375
    ) {
376
        $form          = new XoopsThemeForm(\_MD_SUICO_EDIT_DESC, 'form_picture', 'editpicture.php', 'post', true);
377
        $field_title   = new XoopsFormText($title, 'title', 35, 55);
378
        $field_caption = new XoopsFormText($caption, 'caption', 35, 55);
379
        $form->setExtra('enctype="multipart/form-data"');
380
        $buttonSend    = new XoopsFormButton('', 'submit_button', \_MD_SUICO_SUBMIT, 'submit');
381
        $field_warning = new XoopsFormLabel("<img src='" . $filename . "' alt='thumb'>");
382
        $field_cod_img = new XoopsFormHidden('cod_img', $cod_img);
383
        $field_marker  = new XoopsFormHidden('marker', 1);
384
        $form->addElement($field_warning);
385
        $form->addElement($field_title);
386
        $form->addElement($field_caption);
387
        $form->addElement($field_cod_img);
388
        $form->addElement($field_marker);
389
        $form->addElement($buttonSend);
390
        $form->display();
391
392
        return true;
393
    }
394
395
    /**
396
     * Upload the file and Save into database
397
     *
398
     * @param string $title         A litle title of the file
399
     * @param string $caption       A litle description of the file
400
     * @param string $pathUpload    The path to where the file should be uploaded
401
     * @param int    $thumbwidth    the width in pixels that the thumbnail will have
402
     * @param int    $thumbheight   the height in pixels that the thumbnail will have
403
     * @param int    $pictwidth     the width in pixels that the pic will have
404
     * @param int    $pictheight    the height in pixels that the pic will have
405
     * @param int    $maxfilebytes  the maximum size a file can have to be uploaded in bytes
406
     * @param int    $maxfilewidth  the maximum width in pixels that a pic can have
407
     * @param int    $maxfileheight the maximum height in pixels that a pic can have
408
     * @return bool FALSE if upload fails or database fails
409
     */
410
    public function receivePicture(
411
        $title,
412
        $caption,
413
        $pathUpload,
414
        $thumbwidth,
415
        $thumbheight,
416
        $pictwidth,
417
        $pictheight,
418
        $maxfilebytes,
419
        $maxfilewidth,
420
        $maxfileheight
421
    ) {
422
        global $xoopsUser, $xoopsDB;
423
        //busca id do user logado
424
        $uid = $xoopsUser->getVar('uid');
425
        //create a hash so it does not erase another file
426
        //$hash1 = date();
427
        //$hash = substr($hash1,0,4);
428
429
        // mimetypes and settings put this in admin part later
430
        $allowed_mimetypes = Helper::getInstance()->getConfig(
431
            'mimetypes'
432
        );
433
        $maxfilesize       = $maxfilebytes;
434
435
        //        $uploadDir = \XOOPS_UPLOAD_PATH . '/suico/images/';
436
        // create the object to upload
437
        $uploader = new XoopsMediaUploader(
438
            $pathUpload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight
439
        );
440
        // fetch the media
441
        if ($uploader->fetchMedia(Request::getArray('xoops_upload_file', '', 'POST')[0])) {
442
            //lets create a name for it
443
            $uploader->setPrefix('pic_' . $uid . '_');
444
            //now let s upload the file
445
            if (!$uploader->upload()) {
446
                // if there are errors lets return them
447
448
                echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>';
449
450
                return false;
451
            }
452
            // now let s create a new object picture and set its variables
453
            $picture = $this->create();
454
            $url     = $uploader->getSavedFileName();
455
            $picture->setVar('filename', $url);
456
            $picture->setVar('title', $title);
457
            $picture->setVar('caption', $caption);
458
            $picture->setVar('date_created', \time());
459
            $picture->setVar('date_updated', \time());
460
            $picture->setVar('private', 0);
461
            $uid = $xoopsUser->getVar('uid');
462
            $picture->setVar('uid_owner', $uid);
463
            $this->insert($picture);
464
            $saved_destination = $uploader->getSavedDestination();
465
            //print_r($_FILES);
466
            //$this->resizeImage($saved_destination,false, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload);
467
            //$this->resizeImage($saved_destination,true, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload);
468
            $this->resizeImage(
469
                $saved_destination,
470
                $thumbwidth,
471
                $thumbheight,
472
                $pictwidth,
473
                $pictheight,
474
                $pathUpload
475
            );
476
        } else {
477
            echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>';
478
479
            return false;
480
        }
481
482
        return true;
483
    }
484
485
    /**
486
     * Resize a picture and save it to $pathUpload
487
     *
488
     * @param string $img         the path to the file
489
     * @param int    $thumbwidth  the width in pixels that the thumbnail will have
490
     * @param int    $thumbheight the height in pixels that the thumbnail will have
491
     * @param int    $pictwidth   the width in pixels that the pic will have
492
     * @param int    $pictheight  the height in pixels that the pic will have
493
     * @param string $pathUpload  The path to where the files should be saved after resizing
494
     */
495
    public function resizeImage(
496
        $img,
497
        $thumbwidth,
498
        $thumbheight,
499
        $pictwidth,
500
        $pictheight,
501
        $pathUpload
502
    ) {
503
        $img2   = $img;
504
        $path   = \pathinfo($img);
505
        $img    = \imagecreatefromjpeg($img);
506
        $xratio = $thumbwidth / \imagesx($img);
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesx() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

506
        $xratio = $thumbwidth / \imagesx(/** @scrutinizer ignore-type */ $img);
Loading history...
507
        $yratio = $thumbheight / \imagesy($img);
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

507
        $yratio = $thumbheight / \imagesy(/** @scrutinizer ignore-type */ $img);
Loading history...
508
509
        if ($xratio < 1 || $yratio < 1) {
510
            if ($xratio < $yratio) {
511
                $resized = \imagecreatetruecolor($thumbwidth, (int)\floor(\imagesy($img) * $xratio));
512
            } else {
513
                $resized = \imagecreatetruecolor((int)\floor(\imagesx($img) * $yratio), $thumbheight);
514
            }
515
            \imagecopyresampled(
516
                $resized,
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

516
                /** @scrutinizer ignore-type */ $resized,
Loading history...
517
                $img,
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $src_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

517
                /** @scrutinizer ignore-type */ $img,
Loading history...
518
                0,
519
                0,
520
                0,
521
                0,
522
                \imagesx($resized) + 1,
523
                \imagesy($resized) + 1,
524
                \imagesx($img),
525
                \imagesy($img)
526
            );
527
            \imagejpeg($resized, $pathUpload . '/thumb_' . $path['basename']);
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $image of imagejpeg() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

527
            \imagejpeg(/** @scrutinizer ignore-type */ $resized, $pathUpload . '/thumb_' . $path['basename']);
Loading history...
528
            \imagedestroy($resized);
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $image of imagedestroy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

528
            \imagedestroy(/** @scrutinizer ignore-type */ $resized);
Loading history...
529
        } else {
530
            \imagejpeg($img, $pathUpload . '/thumb_' . $path['basename']);
531
        }
532
533
        \imagedestroy($img);
534
        $path2   = \pathinfo($img2);
535
        $img2    = \imagecreatefromjpeg($img2);
536
        $xratio2 = $pictwidth / \imagesx($img2);
537
        $yratio2 = $pictheight / \imagesy($img2);
538
        if ($xratio2 < 1 || $yratio2 < 1) {
539
            if ($xratio2 < $yratio2) {
540
                $resized2 = \imagecreatetruecolor($pictwidth, (int)\floor(\imagesy($img2) * $xratio2));
541
            } else {
542
                $resized2 = \imagecreatetruecolor((int)\floor(\imagesx($img2) * $yratio2), $pictheight);
543
            }
544
545
            \imagecopyresampled(
546
                $resized2,
547
                $img2,
548
                0,
549
                0,
550
                0,
551
                0,
552
                \imagesx($resized2) + 1,
553
                \imagesy($resized2) + 1,
554
                \imagesx($img2),
555
                \imagesy($img2)
556
            );
557
            \imagejpeg($resized2, $pathUpload . '/resized_' . $path2['basename']);
558
            \imagedestroy($resized2);
559
        } else {
560
            \imagejpeg($img2, $pathUpload . '/resized_' . $path2['basename']);
561
        }
562
        \imagedestroy($img2);
563
    }
564
565
    /**
566
     * @param $limit
567
     * @return array
568
     */
569
    public function getLastPictures($limit)
570
    {
571
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
572
573
        $sql = 'SELECT uname, t.uid_owner, t.filename FROM ' . $this->db->prefix(
574
                'suico_images'
575
            ) . ' AS t, ' . $this->db->prefix(
576
                'users'
577
            );
578
579
        $sql    .= ' WHERE uid_owner = uid AND private=0 ORDER BY cod_img DESC';
580
        $result = $this->db->query($sql, $limit, 0);
581
        $vetor  = [];
582
        $i      = 0;
583
        while (false !== ($myrow = $this->db->fetchArray($result))) {
584
            $vetor[$i]['uid_voted']   = $myrow['uid_owner'];
585
            $vetor[$i]['uname']       = $myrow['uname'];
586
            $vetor[$i]['user_avatar'] = $myrow['filename'];
587
            $i++;
588
        }
589
590
        return $vetor;
591
    }
592
593
    /**
594
     * @param $limit
595
     * @return array
596
     */
597
    public function getLastPicturesForBlock($limit)
598
    {
599
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
600
601
        $sql = 'SELECT uname, t.uid_owner, t.filename, t.title, t.caption  FROM ' . $this->db->prefix(
602
                'suico_images'
603
            ) . ' AS t, ' . $this->db->prefix(
604
                'users'
605
            );
606
607
        $sql    .= ' WHERE uid_owner = uid AND private=0 ORDER BY cod_img DESC';
608
        $result = $this->db->query($sql, $limit, 0);
609
        $vetor  = [];
610
        $i      = 0;
611
        while (false !== ($myrow = $this->db->fetchArray($result))) {
612
            $vetor[$i]['uid_voted']    = $myrow['uid_owner'];
613
            $vetor[$i]['uname']        = $myrow['uname'];
614
            $vetor[$i]['img_filename'] = $myrow['filename'];
615
            $vetor[$i]['title']        = $myrow['title'];
616
            $vetor[$i]['caption']      = $myrow['caption'];
617
618
            $i++;
619
        }
620
621
        return $vetor;
622
    }
623
624
    /**
625
     * Resize a picture and save it to $pathUpload
626
     *
627
     * @param string $img        the path to the file
628
     * @param        $width
629
     * @param        $height
630
     * @param string $pathUpload The path to where the files should be saved after resizing
631
     */
632
    public function makeAvatar(
633
        $img,
634
        $width,
0 ignored issues
show
Unused Code introduced by
The parameter $width is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

634
        /** @scrutinizer ignore-unused */ $width,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
635
        $height,
0 ignored issues
show
Unused Code introduced by
The parameter $height is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

635
        /** @scrutinizer ignore-unused */ $height,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
636
        $pathUpload
637
    ) {
638
        $img2   = $img;
639
        $path   = \pathinfo($img);
640
        $img    = \imagecreatefromjpeg($img);
641
        $xratio = $thumbwidth / \imagesx($img);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $thumbwidth seems to be never defined.
Loading history...
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesx() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

641
        $xratio = $thumbwidth / \imagesx(/** @scrutinizer ignore-type */ $img);
Loading history...
642
        $yratio = $thumbheight / \imagesy($img);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $thumbheight does not exist. Did you maybe mean $height?
Loading history...
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

642
        $yratio = $thumbheight / \imagesy(/** @scrutinizer ignore-type */ $img);
Loading history...
643
644
        if ($xratio < 1 || $yratio < 1) {
645
            if ($xratio < $yratio) {
646
                $resized = \imagecreatetruecolor($thumbwidth, (int)\floor(\imagesy($img) * $xratio));
647
            } else {
648
                $resized = \imagecreatetruecolor((int)\floor(\imagesx($img) * $yratio), $thumbheight);
649
            }
650
            \imagecopyresampled(
651
                $resized,
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

651
                /** @scrutinizer ignore-type */ $resized,
Loading history...
652
                $img,
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $src_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

652
                /** @scrutinizer ignore-type */ $img,
Loading history...
653
                0,
654
                0,
655
                0,
656
                0,
657
                \imagesx($resized) + 1,
658
                \imagesy($resized) + 1,
659
                \imagesx($img),
660
                \imagesy($img)
661
            );
662
            \imagejpeg($resized, $pathUpload . '/thumb_' . $path['basename']);
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $image of imagejpeg() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

662
            \imagejpeg(/** @scrutinizer ignore-type */ $resized, $pathUpload . '/thumb_' . $path['basename']);
Loading history...
663
            \imagedestroy($resized);
0 ignored issues
show
Bug introduced by
It seems like $resized can also be of type false; however, parameter $image of imagedestroy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

663
            \imagedestroy(/** @scrutinizer ignore-type */ $resized);
Loading history...
664
        } else {
665
            \imagejpeg($img, $pathUpload . '/thumb_' . $path['basename']);
666
        }
667
668
        \imagedestroy($img);
669
        $path2   = \pathinfo($img2);
670
        $img2    = \imagecreatefromjpeg($img2);
671
        $xratio2 = $pictwidth / \imagesx($img2);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pictwidth seems to be never defined.
Loading history...
672
        $yratio2 = $pictheight / \imagesy($img2);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pictheight does not exist. Did you maybe mean $height?
Loading history...
673
        if ($xratio2 < 1 || $yratio2 < 1) {
674
            if ($xratio2 < $yratio2) {
675
                $resized2 = \imagecreatetruecolor($pictwidth, (int)\floor(\imagesy($img2) * $xratio2));
676
            } else {
677
                $resized2 = \imagecreatetruecolor((int)\floor(\imagesx($img2) * $yratio2), $pictheight);
678
            }
679
680
            \imagecopyresampled(
681
                $resized2,
682
                $img2,
683
                0,
684
                0,
685
                0,
686
                0,
687
                \imagesx($resized2) + 1,
688
                \imagesy($resized2) + 1,
689
                \imagesx($img2),
690
                \imagesy($img2)
691
            );
692
            \imagejpeg($resized2, $pathUpload . '/resized_' . $path2['basename']);
693
            \imagedestroy($resized2);
694
        } else {
695
            \imagejpeg($img2, $pathUpload . '/resized_' . $path2['basename']);
696
        }
697
        \imagedestroy($img2);
698
    }
699
}
700