Exemplo n.º 1
0
 public function load($data, $keyName = null)
 {
     $rules = $this->rules();
     if ('' === $keyName) {
         $modelData = $data;
     } else {
         $keyName = null !== $keyName ? $keyName : $this->getClassName(false);
         $modelData = isset($data[$keyName]) ? $data[$keyName] : [];
     }
     if (empty($modelData)) {
         $modelData = [];
     }
     $modelName = $this->getClassName(false);
     $file = isset($_FILES[$modelName]) ? $_FILES[$modelName] : null;
     foreach ($modelData as $prop => $value) {
         $this->{$prop} = $value;
     }
     foreach ($this->rules() as $prop => $rules) {
         if (isset($rules['type']) && BaseModel::TYPE_IMAGE === $rules['type'] && $file && isset($file['tmp_name'][$prop])) {
             if (!isset($rules['type']) || self::TYPE_IMAGE !== $rules['type'] || !preg_match("/image\\/.*/", $file['type'][$prop])) {
                 return false;
             }
             if ('image/gif' === $file['type'][$prop]) {
                 $this->{$prop} = new \models\misc\Image($file['tmp_name'][$prop], $file['type'][$prop]);
             } else {
                 $this->{$prop} = ImageHelper::loadImage($file['tmp_name'][$prop]);
             }
         }
     }
     return true;
 }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
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;
 }