2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Entities;
|
2022-03-25 14:05:44 +03:00
|
|
|
use openvk\Web\Models\Entities\Album;
|
|
|
|
use openvk\Web\Models\Repositories\Albums;
|
2020-06-07 19:04:43 +03:00
|
|
|
use Chandler\Database\DatabaseConnection as DB;
|
|
|
|
use Nette\InvalidStateException as ISE;
|
|
|
|
use Nette\Utils\Image;
|
|
|
|
|
|
|
|
class Photo extends Media
|
|
|
|
{
|
|
|
|
protected $tableName = "photos";
|
|
|
|
protected $fileExtension = "jpeg";
|
|
|
|
|
2021-10-14 14:40:28 +03:00
|
|
|
const ALLOWED_SIDE_MULTIPLIER = 7;
|
2022-03-29 20:43:34 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
protected function saveFile(string $filename, string $hash): bool
|
|
|
|
{
|
|
|
|
$image = Image::fromFile($filename);
|
2021-10-14 14:40:28 +03:00
|
|
|
if(($image->height >= ($image->width * Photo::ALLOWED_SIDE_MULTIPLIER)) || ($image->width >= ($image->height * Photo::ALLOWED_SIDE_MULTIPLIER)))
|
|
|
|
throw new ISE("Invalid layout: image is too wide/short");
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
$image->save($this->pathFromHash($hash), 92, Image::JPEG);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:43:34 +03:00
|
|
|
function crop(real $left, real $top, real $width, real $height): void
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
if(isset($this->changes["hash"]))
|
|
|
|
$hash = $this->changes["hash"];
|
|
|
|
else if(!is_null($this->getRecord()))
|
|
|
|
$hash = $this->getRecord()->hash;
|
|
|
|
else
|
|
|
|
throw new ISE("Cannot crop uninitialized image. Please call setFile(\$_FILES[...]) first.");
|
|
|
|
|
|
|
|
$image = Image::fromFile($this->pathFromHash($hash));
|
|
|
|
$image->crop($left, $top, $width, $height);
|
2022-03-29 20:43:34 +03:00
|
|
|
$image->save($this->pathFromHash($hash));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function isolate(): void
|
|
|
|
{
|
|
|
|
if(is_null($this->getRecord()))
|
|
|
|
throw new ISE("Cannot isolate unpresisted image. Please save() it first.");
|
|
|
|
|
2021-01-27 20:39:15 +03:00
|
|
|
DB::i()->getContext()->table("album_relations")->where("media", $this->getRecord()->id)->delete();
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2022-03-29 20:43:34 +03:00
|
|
|
|
|
|
|
function getDimensions(): array
|
|
|
|
{
|
2022-04-05 11:38:19 +03:00
|
|
|
$x = $this->getRecord()->width;
|
|
|
|
$y = $this->getRecord()->height;
|
|
|
|
if(!$x) { # no sizes in database
|
|
|
|
$hash = $this->getRecord()->hash;
|
|
|
|
$image = new \Imagick($this->pathFromHash($hash));
|
2022-03-29 20:43:34 +03:00
|
|
|
|
2022-04-05 11:38:19 +03:00
|
|
|
$x = $image->getImageWidth();
|
|
|
|
$y = $image->getImageHeight();
|
|
|
|
$this->stateChanges("width", $x);
|
|
|
|
$this->stateChanges("height", $y);
|
|
|
|
$this->save();
|
|
|
|
}
|
2022-03-29 20:43:34 +03:00
|
|
|
|
2022-04-05 11:38:19 +03:00
|
|
|
return [$x, $y];
|
|
|
|
}
|
2022-03-29 20:43:34 +03:00
|
|
|
|
|
|
|
function getAlbum(): ?Album
|
|
|
|
{
|
|
|
|
return (new Albums)->getAlbumByPhotoId($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
function toVkApiStruct(): object
|
|
|
|
{
|
|
|
|
$res = (object) [];
|
|
|
|
|
|
|
|
$res->id = $res->pid = $this->getId();
|
|
|
|
$res->owner_id = $res->user_id = $this->getOwner()->getId()->getId();
|
|
|
|
$res->aid = $res->album_id = NULL;
|
|
|
|
$res->width = $this->getDimensions()[0];
|
|
|
|
$res->height = $this->getDimensions()[1];
|
|
|
|
$res->date = $res->created = $this->getPublicationTime()->timestamp();
|
|
|
|
|
|
|
|
$res->src =
|
|
|
|
$res->src_small = $res->src_big = $res->src_xbig = $res->src_xxbig =
|
|
|
|
$res->src_xxxbig = $res->photo_75 = $res->photo_130 = $res->photo_604 =
|
|
|
|
$res->photo_807 = $res->photo_1280 = $res->photo_2560 = $this->getURL();
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2022-03-30 16:52:14 +03:00
|
|
|
static function fastMake(int $owner, string $description = "", array $file, ?Album $album = NULL, bool $anon = false): Photo
|
2021-10-12 14:17:21 +03:00
|
|
|
{
|
|
|
|
$photo = new static;
|
|
|
|
$photo->setOwner($owner);
|
|
|
|
$photo->setDescription(iconv_substr($description, 0, 36) . "...");
|
2021-11-15 22:45:48 +03:00
|
|
|
$photo->setAnonymous($anon);
|
2021-10-12 14:17:21 +03:00
|
|
|
$photo->setCreated(time());
|
|
|
|
$photo->setFile($file);
|
|
|
|
$photo->save();
|
2022-03-29 20:43:34 +03:00
|
|
|
|
2021-10-12 14:17:21 +03:00
|
|
|
if(!is_null($album))
|
|
|
|
$album->addPhoto($photo);
|
2022-03-25 14:05:44 +03:00
|
|
|
|
2022-03-29 20:43:34 +03:00
|
|
|
return $photo;
|
2022-03-25 14:05:44 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|