Example #1
0
 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;
 }