public function save()
 {
     $imageDg = $this->image->getImage();
     $ext = "jpeg";
     $usersDir = CW::$app->params['sitePath'] . 'public_html/images/users';
     $userDir = "{$usersDir}/{$this->userId}";
     if (!is_dir($usersDir)) {
         mkdir($usersDir);
     }
     if (!is_dir($userDir)) {
         mkdir($userDir);
     }
     $imageMedium = ImageHelper::scaleAndCrop(0, 0, User::IMAGE_MEDIUM_SIZE, User::IMAGE_MEDIUM_SIZE, $imageDg, User::IMAGE_MEDIUM_SIZE, User::IMAGE_MEDIUM_SIZE);
     $imageSmall = ImageHelper::scaleAndCrop(0, 0, User::IMAGE_SMALL_SIZE, User::IMAGE_SMALL_SIZE, $imageDg, User::IMAGE_SMALL_SIZE, User::IMAGE_SMALL_SIZE);
     imagejpeg($imageMedium, sprintf("%s/%dx%d.%s", $userDir, User::IMAGE_MEDIUM_SIZE, User::IMAGE_MEDIUM_SIZE, $ext));
     imagejpeg($imageSmall, sprintf("%s/%dx%d.%s", $userDir, User::IMAGE_SMALL_SIZE, User::IMAGE_SMALL_SIZE, $ext));
     imagedestroy($imageMedium);
     imagedestroy($imageSmall);
     $imageId = Image::create(['rel_id' => CW::$app->user->identity->id, 'rel_type' => Image::REL_TYPE_USER, 'type' => Image::TYPE_PROFILE_PIC, 'image_type' => Image::IMAGE_TYPE_NORMAL]);
     if (null !== $imageId) {
         CW::$app->db->executeUpdate("UPDATE `users` SET `profile_img_id` = {$imageId} WHERE id = " . CW::$app->user->identity->id);
         $_SESSION['user']->profile_img_id = $imageId;
     }
     return true;
 }
 public function save()
 {
     $imageDg = $this->image->getImage();
     $ext = "jpeg";
     $stmt = CW::$app->db->prepare("INSERT INTO `updates` (`user_id`, `description`, `is_gif`, `created_at`) VALUES (:userId, :description, " . ($this->image->isGif() ? 1 : 0) . ", :created_at)");
     if (0 >= $stmt->execute([':userId' => \CW::$app->user->identity->id, ':description' => $this->title, ':created_at' => time()])) {
         return false;
     }
     $this->newUpdateId = CW::$app->db->getLastInsertedId();
     if (!Update::addActivity($this->newUpdateId, CW::$app->user->identity->id, Update::ACTIVITY_TYPE_POST)) {
         return false;
     }
     $this->addTags();
     $a = [];
     foreach ($this->categories as $category) {
         $c = (int) $category;
         $a[] = "({$this->newUpdateId}, {$c})";
     }
     $categoryInsert = 'INSERT INTO `update_categories` (`update_id`, `category_id`) VALUES ' . implode(',', $a);
     if (0 >= CW::$app->db->executeUpdate($categoryInsert)) {
         return false;
     }
     $updateDir = CW::$app->params['sitePath'] . 'public_html/images/updates/' . $this->newUpdateId;
     if (!file_exists(CW::$app->params['sitePath'] . 'public_html/images/updates')) {
         mkdir(CW::$app->params['sitePath'] . 'public_html/images/updates');
     }
     mkdir($updateDir);
     if ('image/gif' === $this->image->getExt()) {
         if (!file_exists(CW::$app->params['sitePath'] . 'public_html/images/tmp')) {
             mkdir(CW::$app->params['sitePath'] . 'public_html/images/tmp');
         }
         $i = ImageHelper::loadImage($this->image->getImage());
         if (!$i) {
             $this->addError('image', 'Invalid gif.');
             return false;
         }
         imagejpeg(ImageHelper::scaleImage($i->getImage(), 500), CW::$app->params['sitePath'] . "public_html/images/updates/{$this->newUpdateId}/poster.jpeg");
         $inpFile = CW::$app->params['sitePath'] . "public_html/images/tmp/{$this->newUpdateId}_medium.gif";
         $outFileMedium = "{$updateDir}/medium";
         foreach (['mp4', 'webm'] as $ext) {
             $mediumBytes = ImageHelper::resizeGif($this->image->getImage(), 500);
             file_put_contents($inpFile, $mediumBytes);
             ImageHelper::gifToVideo($inpFile, "{$outFileMedium}.{$ext}");
             unlink($inpFile);
         }
         $imageType = Image::IMAGE_TYPE_VIDEO;
         $type = Image::TYPE_IMAGE;
     } else {
         $imageBig = ImageHelper::scaleImage($imageDg, Update::IMAGE_BIG_WIDTH, PHP_INT_MAX);
         $imageMedium = ImageHelper::scaleImage($imageDg, Update::IMAGE_MEDIUM_WIDTH, PHP_INT_MAX);
         $highImage = imagesy($imageMedium) > imagesx($imageMedium) + 150;
         if ($highImage) {
             $imageMedium = ImageHelper::cropImage(0, 0, imagesx($imageMedium), 300, $imageMedium);
         }
         $imageSmall = ImageHelper::scaleImage($imageDg, Update::IMAGE_SMALL_WIDTH, PHP_INT_MAX);
         if ($highImage) {
             $imageSmall = ImageHelper::cropImage(0, 0, imagesx($imageSmall), 150, $imageSmall);
         }
         imagejpeg($imageBig, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_BIG_WIDTH, $ext));
         imagejpeg($imageMedium, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_MEDIUM_WIDTH, $ext));
         imagejpeg($imageSmall, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_SMALL_WIDTH, $ext));
         imagedestroy($imageBig);
         imagedestroy($imageMedium);
         imagedestroy($imageSmall);
         $imageType = Image::IMAGE_TYPE_NORMAL;
         $type = $highImage ? Image::TYPE_HIGH_IMAGE : Image::TYPE_IMAGE;
     }
     Image::create(['rel_id' => $this->newUpdateId, 'rel_type' => Image::REL_TYPE_UPDATE, 'type' => $type, 'image_type' => $imageType]);
     return true;
 }