Passed
Pull Request — master (#81)
by Michael
02:53
created

class/ImageHandler.php (1 issue)

Severity
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Yogurt;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
use CriteriaElement;
16
use XoopsDatabase;
17
use XoopsFormButton;
18
use XoopsFormFile;
19
use XoopsFormHidden;
20
use XoopsFormLabel;
21
use XoopsFormText;
22
use XoopsMediaUploader;
23
use XoopsObject;
24
use XoopsPersistableObjectHandler;
25
use XoopsThemeForm;
26
use Xmf\Request;
27
28
/**
29
 * @copyright    XOOPS Project https://xoops.org/
30
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
31
 * @author       Marcello Brandão aka  Suico
32
 * @author       XOOPS Development Team
33
 * @since
34
 */
35
36
/**
37
 * Protection against inclusion outside the site
38
 */
39
if (!\defined('XOOPS_ROOT_PATH')) {
40
    die('XOOPS root path not defined');
41
}
42
43
/**
44
 * Includes of form objects and uploader
45
 */
46
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
47
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
48
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
49
50
// -------------------------------------------------------------------------
51
// ------------------Image user handler class -------------------
52
// -------------------------------------------------------------------------
53
54
/**
55
 * yogurt_imageshandler class.
56
 * This class provides simple mechanism for Image object and generate forms for inclusion etc
57
 */
58
class ImageHandler extends XoopsPersistableObjectHandler
59
{
60
    public $helper;
61
62
    public $isAdmin;
63
64
    /**
65
     * Constructor
66
     * @param \XoopsDatabase|null              $xoopsDatabase
67
     * @param \XoopsModules\Yogurt\Helper|null $helper
68
     */
69
    public function __construct(
70
        ?XoopsDatabase $xoopsDatabase = null,
71
        $helper = null
72
    ) {
73
        /** @var \XoopsModules\Yogurt\Helper $this ->helper */
74
        if (null === $helper) {
75
            $this->helper = Helper::getInstance();
76
        } else {
77
            $this->helper = $helper;
78
        }
79
        $isAdmin = $this->helper->isUserAdmin();
80
        parent::__construct($xoopsDatabase, 'yogurt_images', Image::class, 'cod_img', 'title', 'caption');
81
    }
82
83
    /**
84
     * create a new Groups
85
     *
86
     * @param bool $isNew flag the new objects as "new"?
87
     * @return \XoopsObject Groups
88
     */
89
    public function create(
90
        $isNew = true
91
    ) {
92
        $obj = parent::create($isNew);
93
        //        if ($isNew) {
94
        //            $obj->setDefaultPermissions();
95
        //        }
96
        if ($isNew) {
97
            $obj->setNew();
98
        } else {
99
            $obj->unsetNew();
100
        }
101
        $obj->helper = $this->helper;
102
103
        return $obj;
104
    }
105
106
    /**
107
     * retrieve a Image
108
     *
109
     * @param int  $id of the Image
110
     * @param null $fields
111
     * @return mixed reference to the {@link Image} object, FALSE if failed
112
     */
113
    public function get2(
114
        $id = null,
115
        $fields = null
116
    ) {
117
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_images') . ' WHERE cod_img=' . $id;
118
        if (!$result = $this->db->query($sql)) {
119
            return false;
120
        }
121
        $numrows = $this->db->getRowsNum($result);
122
        if (1 === $numrows) {
123
            $image = new Image();
124
            $image->assignVars($this->db->fetchArray($result));
125
126
            return $image;
127
        }
128
129
        return false;
130
    }
131
132
    /**
133
     * insert a new Image in the database
134
     *
135
     * @param \XoopsObject $xoopsObject   reference to the {@link Image}
136
     *                                    object
137
     * @param bool         $force
138
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
139
     */
140
    public function insert2(
141
        XoopsObject $xoopsObject,
142
        $force = false
143
    ) {
144
        global $xoopsConfig;
145
        if (!$xoopsObject instanceof Image) {
146
            return false;
147
        }
148
        if (!$xoopsObject->isDirty()) {
149
            return true;
150
        }
151
        if (!$xoopsObject->cleanVars()) {
152
            return false;
153
        }
154
155
        $cod_img = '';
156
        foreach ($xoopsObject->cleanVars as $k => $v) {
157
            ${$k} = $v;
158
        }
159
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
160
        if ($xoopsObject->isNew()) {
161
            // ajout/modification d'un Image
162
            $xoopsObject = new Image();
163
            $format      = 'INSERT INTO %s (cod_img, title, caption, date_created, date_updated, uid_owner, filename, private)';
164
            $format      .= 'VALUES (%u, %s, %s, %s, %s, %s, %s, 0)';
165
            $sql         = \sprintf(
166
                $format,
167
                $this->db->prefix('yogurt_images'),
168
                $cod_img,
169
                $this->db->quoteString($title),
170
                $this->db->quoteString($caption),
171
                time(),//$now,
172
                time(),//$now,
173
                $this->db->quoteString($uid_owner),
174
                $this->db->quoteString($url)
175
            );
176
            $force       = true;
177
        } else {
178
            $format = 'UPDATE %s SET ';
179
            $format .= 'cod_img=%u, title=%s, caption=%s, date_created=%s, date_updated=%s, uid_owner=%s, filename=%s, private=%s';
180
            $format .= ' WHERE cod_img = %u';
181
            $sql    = \sprintf(
182
                $format,
183
                $this->db->prefix('yogurt_images'),
184
                $cod_img,
185
                $this->db->quoteString($title),
186
                $this->db->quoteString($caption),
187
                $xoopsObject->getVar('date_created'), // $now,
188
                $xoopsObject->getVar('date_updated'), // $now,
189
                $this->db->quoteString($uid_owner),
190
                $this->db->quoteString($url),
191
                $this->db->quoteString($private),
192
                $cod_img
193
            );
194
        }
195
        if ($force) {
196
            $result = $this->db->queryF($sql);
197
        } else {
198
            $result = $this->db->query($sql);
199
        }
200
        if (!$result) {
201
            return false;
202
        }
203
        if (empty($cod_img)) {
0 ignored issues
show
The condition empty($cod_img) is always true.
Loading history...
204
            $cod_img = $this->db->getInsertId();
205
        }
206
        $xoopsObject->assignVar('cod_img', $cod_img);
207
208
        return true;
209
    }
210
211
    /**
212
     * delete a Image from the database
213
     *
214
     * @param \XoopsObject $xoopsObject reference to the Image to delete
215
     * @param bool         $force
216
     * @return bool FALSE if failed.
217
     */
218
    public function delete(
219
        XoopsObject $xoopsObject,
220
        $force = false
221
    ) {
222
        if (!$xoopsObject instanceof Image) {
223
            return false;
224
        }
225
        $sql = \sprintf(
226
            'DELETE FROM %s WHERE cod_img = %u',
227
            $this->db->prefix('yogurt_images'),
228
            $xoopsObject->getVar('cod_img')
229
        );
230
        if ($force) {
231
            $result = $this->db->queryF($sql);
232
        } else {
233
            $result = $this->db->query($sql);
234
        }
235
        if (!$result) {
236
            return false;
237
        }
238
239
        return true;
240
    }
241
242
    /**
243
     * retrieve yogurt_imagess from the database
244
     *
245
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
246
     * @param bool                                 $id_as_key       use the UID as key for the array?
247
     * @param bool                                 $as_object
248
     * @return array array of {@link Image} objects
249
     */
250
    public function &getObjects(
251
        ?CriteriaElement $criteriaElement = null,
252
        $id_as_key = false,
253
        $as_object = true
254
    ) {
255
        $ret   = [];
256
        $limit = $start = 0;
257
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_images');
258
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
259
            $sql .= ' ' . $criteriaElement->renderWhere();
260
            if ('' !== $criteriaElement->getSort()) {
261
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
262
            }
263
            $limit = $criteriaElement->getLimit();
264
            $start = $criteriaElement->getStart();
265
        }
266
        $result = $this->db->query($sql, $limit, $start);
267
        if (!$result) {
268
            return $ret;
269
        }
270
        while (false !== ($myrow = $this->db->fetchArray($result))) {
271
            $image = new Image();
272
            $image->assignVars($myrow);
273
            if (!$id_as_key) {
274
                $ret[] = &$image;
275
            } else {
276
                $ret[$myrow['cod_img']] = &$image;
277
            }
278
            unset($image);
279
        }
280
281
        return $ret;
282
    }
283
284
    /**
285
     * count yogurt_imagess matching a condition
286
     *
287
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
288
     * @return int count of yogurt_imagess
289
     */
290
    public function getCount(
291
        ?CriteriaElement $criteriaElement = null
292
    ) {
293
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_images');
294
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
295
            $sql .= ' ' . $criteriaElement->renderWhere();
296
        }
297
        $result = $this->db->query($sql);
298
        if (!$result) {
299
            return 0;
300
        }
301
        [$count] = $this->db->fetchRow($result);
302
303
        return (int)$count;
304
    }
305
306
    /**
307
     * delete yogurt_imagess matching a set of conditions
308
     *
309
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
310
     * @param bool                                 $force
311
     * @param bool                                 $asObject
312
     * @return bool FALSE if deletion failed
313
     */
314
    public function deleteAll(
315
        ?CriteriaElement $criteriaElement = null,
316
        $force = true,
317
        $asObject = false
318
    ) {
319
        $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_images');
320
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
321
            $sql .= ' ' . $criteriaElement->renderWhere();
322
        }
323
        if (!$result = $this->db->query($sql)) {
324
            return false;
325
        }
326
327
        return true;
328
    }
329
330
    /**
331
     * Render a form to send pictures
332
     *
333
     * @param int       $maxbytes the maximum size of a picture
334
     * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered
335
     * @return bool TRUE
336
     *
337
     * obs: Some functions wont work on php 4 so edit lines down under acording to your version
338
     */
339
    public function renderFormSubmit(
340
        $maxbytes,
341
        $xoopsTpl
342
    ) {
343
        $form          = new XoopsThemeForm(\_MD_YOGURT_SUBMIT_PIC_TITLE, 'form_picture', 'submitImage.php', 'post', true);
344
        $field_url     = new XoopsFormFile(\_MD_YOGURT_SELECT_PHOTO, 'sel_photo', 2000000);
345
        $field_title   = new XoopsFormText(\_MD_YOGURT_PHOTOTITLE, 'title', 35, 55);
346
        $field_caption = new XoopsFormText(\_MD_YOGURT_CAPTION, 'caption', 35, 55);
347
        $form->setExtra('enctype="multipart/form-data"');
348
        $buttonSend    = new XoopsFormButton('', 'submit_button', \_MD_YOGURT_UPLOADPICTURE, 'submit');
349
        $field_warning = new XoopsFormLabel(\sprintf(\_MD_YOGURT_YOU_CAN_UPLOAD, $maxbytes / 1024));
350
        $form->addElement($field_warning);
351
        $form->addElement($field_url, true);
352
        $form->addElement($field_title);
353
        $form->addElement($field_caption);
354
355
        $form->addElement($buttonSend);
356
        $form->assign($xoopsTpl); //If your server is php 5
357
        return true;
358
    }
359
360
    /**
361
     * Render a form to edit the description of the pictures
362
     *
363
     * @param        $title
364
     * @param string $caption  The description of the picture
365
     * @param int    $cod_img  the id of the image in database
366
     * @param string $filename the url to the thumb of the image so it can be displayed
367
     * @return bool TRUE
368
     */
369
    public function renderFormEdit(
370
        $title,
371
        $caption,
372
        $cod_img,
373
        $filename
374
    ) {
375
        $form          = new XoopsThemeForm(\_MD_YOGURT_EDIT_DESC, 'form_picture', 'editpicture.php', 'post', true);
376
        $field_title   = new XoopsFormText($title, 'title', 35, 55);
377
        $field_caption = new XoopsFormText($caption, 'caption', 35, 55);
378
        $form->setExtra('enctype="multipart/form-data"');
379
        $buttonSend    = new XoopsFormButton('', 'submit_button', \_MD_YOGURT_SUBMIT, 'submit');
380
        $field_warning = new XoopsFormLabel("<img src='" . $filename . "' alt='sssss'>");
381
        $field_cod_img = new XoopsFormHidden('cod_img', $cod_img);
382
        $field_marker  = new XoopsFormHidden('marker', 1);
383
        $form->addElement($field_warning);
384
        $form->addElement($field_title);
385
        $form->addElement($field_caption);
386
        $form->addElement($field_cod_img);
387
        $form->addElement($field_marker);
388
        $form->addElement($buttonSend);
389
        $form->display();
390
391
        return true;
392
    }
393
394
    /**
395
     * Upload the file and Save into database
396
     *
397
     * @param string $title         A litle title of the file
398
     * @param string $caption       A litle description of the file
399
     * @param string $pathUpload    The path to where the file should be uploaded
400
     * @param int    $thumbwidth    the width in pixels that the thumbnail will have
401
     * @param int    $thumbheight   the height in pixels that the thumbnail will have
402
     * @param int    $pictwidth     the width in pixels that the pic will have
403
     * @param int    $pictheight    the height in pixels that the pic will have
404
     * @param int    $maxfilebytes  the maximum size a file can have to be uploaded in bytes
405
     * @param int    $maxfilewidth  the maximum width in pixels that a pic can have
406
     * @param int    $maxfileheight the maximum height in pixels that a pic can have
407
     * @return bool FALSE if upload fails or database fails
408
     */
409
    public function receivePicture(
410
        $title,
411
        $caption,
412
        $pathUpload,
413
        $thumbwidth,
414
        $thumbheight,
415
        $pictwidth,
416
        $pictheight,
417
        $maxfilebytes,
418
        $maxfilewidth,
419
        $maxfileheight
420
    ) {
421
        global $xoopsUser, $xoopsDB;
422
        //busca id do user logado
423
        $uid = $xoopsUser->getVar('uid');
424
        //create a hash so it does not erase another file
425
        //$hash1 = date();
426
        //$hash = substr($hash1,0,4);
427
428
        // mimetypes and settings put this in admin part later
429
        $allowed_mimetypes = Helper::getInstance()->getConfig(
430
            'mimetypes'
431
        );
432
        $maxfilesize       = $maxfilebytes;
433
434
        //        $uploadDir = \XOOPS_UPLOAD_PATH . '/yogurt/images/';
435
        // create the object to upload
436
        $uploader = new XoopsMediaUploader(
437
            $pathUpload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight
438
        );
439
        // fetch the media
440
        if ($uploader->fetchMedia(Request::getArray('xoops_upload_file', '', 'POST')[0])) {
441
            //lets create a name for it
442
            $uploader->setPrefix('pic_' . $uid . '_');
443
            //now let s upload the file
444
            if (!$uploader->upload()) {
445
                // if there are errors lets return them
446
447
                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>';
448
449
                return false;
450
            }
451
            // now let s create a new object picture and set its variables
452
            $picture = $this->create();
453
            $url     = $uploader->getSavedFileName();
454
            $picture->setVar('filename', $url);
455
            $picture->setVar('title', $title);
456
            $picture->setVar('caption', $caption);
457
            $picture->setVar('date_created', \time());
458
            $picture->setVar('date_updated', \time());
459
            $picture->setVar('private', 0);
460
            $uid = $xoopsUser->getVar('uid');
461
            $picture->setVar('uid_owner', $uid);
462
            $this->insert($picture);
463
            $saved_destination = $uploader->getSavedDestination();
464
            //print_r($_FILES);
465
            //$this->resizeImage($saved_destination,false, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload);
466
            //$this->resizeImage($saved_destination,true, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload);
467
            $this->resizeImage(
468
                $saved_destination,
469
                $thumbwidth,
470
                $thumbheight,
471
                $pictwidth,
472
                $pictheight,
473
                $pathUpload
474
            );
475
        } else {
476
            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>';
477
478
            return false;
479
        }
480
481
        return true;
482
    }
483
484
    /**
485
     * Resize a picture and save it to $pathUpload
486
     *
487
     * @param string $img         the path to the file
488
     * @param int    $thumbwidth  the width in pixels that the thumbnail will have
489
     * @param int    $thumbheight the height in pixels that the thumbnail will have
490
     * @param int    $pictwidth   the width in pixels that the pic will have
491
     * @param int    $pictheight  the height in pixels that the pic will have
492
     * @param string $pathUpload  The path to where the files should be saved after resizing
493
     */
494
    public function resizeImage(
495
        $img,
496
        $thumbwidth,
497
        $thumbheight,
498
        $pictwidth,
499
        $pictheight,
500
        $pathUpload
501
    ) {
502
        $img2   = $img;
503
        $path   = \pathinfo($img);
504
        $img    = \imagecreatefromjpeg($img);
505
        $xratio = $thumbwidth / \imagesx($img);
506
        $yratio = $thumbheight / \imagesy($img);
507
508
        if ($xratio < 1 || $yratio < 1) {
509
            if ($xratio < $yratio) {
510
                $resized = \imagecreatetruecolor($thumbwidth, (int)\floor(\imagesy($img) * $xratio));
511
            } else {
512
                $resized = \imagecreatetruecolor((int)\floor(\imagesx($img) * $yratio), $thumbheight);
513
            }
514
            \imagecopyresampled(
515
                $resized,
516
                $img,
517
                0,
518
                0,
519
                0,
520
                0,
521
                \imagesx($resized) + 1,
522
                \imagesy($resized) + 1,
523
                \imagesx($img),
524
                \imagesy($img)
525
            );
526
            \imagejpeg($resized, $pathUpload . '/thumb_' . $path['basename']);
527
            \imagedestroy($resized);
528
        } else {
529
            \imagejpeg($img, $pathUpload . '/thumb_' . $path['basename']);
530
        }
531
532
        \imagedestroy($img);
533
        $path2   = \pathinfo($img2);
534
        $img2    = \imagecreatefromjpeg($img2);
535
        $xratio2 = $pictwidth / \imagesx($img2);
536
        $yratio2 = $pictheight / \imagesy($img2);
537
        if ($xratio2 < 1 || $yratio2 < 1) {
538
            if ($xratio2 < $yratio2) {
539
                $resized2 = \imagecreatetruecolor($pictwidth, (int)\floor(\imagesy($img2) * $xratio2));
540
            } else {
541
                $resized2 = \imagecreatetruecolor((int)\floor(\imagesx($img2) * $yratio2), $pictheight);
542
            }
543
544
            \imagecopyresampled(
545
                $resized2,
546
                $img2,
547
                0,
548
                0,
549
                0,
550
                0,
551
                \imagesx($resized2) + 1,
552
                \imagesy($resized2) + 1,
553
                \imagesx($img2),
554
                \imagesy($img2)
555
            );
556
            \imagejpeg($resized2, $pathUpload . '/resized_' . $path2['basename']);
557
            \imagedestroy($resized2);
558
        } else {
559
            \imagejpeg($img2, $pathUpload . '/resized_' . $path2['basename']);
560
        }
561
        \imagedestroy($img2);
562
    }
563
564
    /**
565
     * @param $limit
566
     * @return array
567
     */
568
    public function getLastPictures($limit)
569
    {
570
        $ret = [];
571
572
        $sql = 'SELECT uname, t.uid_owner, t.filename FROM ' . $this->db->prefix(
573
                'yogurt_images'
574
            ) . ' AS t, ' . $this->db->prefix(
575
                'users'
576
            );
577
578
        $sql    .= ' WHERE uid_owner = uid AND private=0 ORDER BY cod_img DESC';
579
        $result = $this->db->query($sql, $limit, 0);
580
        $vetor  = [];
581
        $i      = 0;
582
        while (false !== ($myrow = $this->db->fetchArray($result))) {
583
            $vetor[$i]['uid_voted']   = $myrow['uid_owner'];
584
            $vetor[$i]['uname']       = $myrow['uname'];
585
            $vetor[$i]['user_avatar'] = $myrow['filename'];
586
            $i++;
587
        }
588
589
        return $vetor;
590
    }
591
592
    /**
593
     * @param $limit
594
     * @return array
595
     */
596
    public function getLastPicturesForBlock($limit)
597
    {
598
        $ret = [];
599
600
        $sql = 'SELECT uname, t.uid_owner, t.filename, t.title, t.caption  FROM ' . $this->db->prefix(
601
                'yogurt_images'
602
            ) . ' AS t, ' . $this->db->prefix(
603
                'users'
604
            );
605
606
        $sql    .= ' WHERE uid_owner = uid AND private=0 ORDER BY cod_img DESC';
607
        $result = $this->db->query($sql, $limit, 0);
608
        $vetor  = [];
609
        $i      = 0;
610
        while (false !== ($myrow = $this->db->fetchArray($result))) {
611
            $vetor[$i]['uid_voted']    = $myrow['uid_owner'];
612
            $vetor[$i]['uname']        = $myrow['uname'];
613
            $vetor[$i]['img_filename'] = $myrow['filename'];
614
            $vetor[$i]['title']        = $myrow['title'];
615
            $vetor[$i]['caption']      = $myrow['caption'];
616
617
            $i++;
618
        }
619
620
        return $vetor;
621
    }
622
623
    /**
624
     * Resize a picture and save it to $pathUpload
625
     *
626
     * @param string $img        the path to the file
627
     * @param        $width
628
     * @param        $height
629
     * @param string $pathUpload The path to where the files should be saved after resizing
630
     */
631
    public function makeAvatar(
632
        $img,
633
        $width,
634
        $height,
635
        $pathUpload
636
    ) {
637
        $img2   = $img;
638
        $path   = \pathinfo($img);
639
        $img    = \imagecreatefromjpeg($img);
640
        $xratio = $thumbwidth / \imagesx($img);
641
        $yratio = $thumbheight / \imagesy($img);
642
643
        if ($xratio < 1 || $yratio < 1) {
644
            if ($xratio < $yratio) {
645
                $resized = \imagecreatetruecolor($thumbwidth, (int)\floor(\imagesy($img) * $xratio));
646
            } else {
647
                $resized = \imagecreatetruecolor((int)\floor(\imagesx($img) * $yratio), $thumbheight);
648
            }
649
            \imagecopyresampled(
650
                $resized,
651
                $img,
652
                0,
653
                0,
654
                0,
655
                0,
656
                \imagesx($resized) + 1,
657
                \imagesy($resized) + 1,
658
                \imagesx($img),
659
                \imagesy($img)
660
            );
661
            \imagejpeg($resized, $pathUpload . '/thumb_' . $path['basename']);
662
            \imagedestroy($resized);
663
        } else {
664
            \imagejpeg($img, $pathUpload . '/thumb_' . $path['basename']);
665
        }
666
667
        \imagedestroy($img);
668
        $path2   = \pathinfo($img2);
669
        $img2    = \imagecreatefromjpeg($img2);
670
        $xratio2 = $pictwidth / \imagesx($img2);
671
        $yratio2 = $pictheight / \imagesy($img2);
672
        if ($xratio2 < 1 || $yratio2 < 1) {
673
            if ($xratio2 < $yratio2) {
674
                $resized2 = \imagecreatetruecolor($pictwidth, (int)\floor(\imagesy($img2) * $xratio2));
675
            } else {
676
                $resized2 = \imagecreatetruecolor((int)\floor(\imagesx($img2) * $yratio2), $pictheight);
677
            }
678
679
            \imagecopyresampled(
680
                $resized2,
681
                $img2,
682
                0,
683
                0,
684
                0,
685
                0,
686
                \imagesx($resized2) + 1,
687
                \imagesy($resized2) + 1,
688
                \imagesx($img2),
689
                \imagesy($img2)
690
            );
691
            \imagejpeg($resized2, $pathUpload . '/resized_' . $path2['basename']);
692
            \imagedestroy($resized2);
693
        } else {
694
            \imagejpeg($img2, $pathUpload . '/resized_' . $path2['basename']);
695
        }
696
        \imagedestroy($img2);
697
    }
698
}
699