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