2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Entities;
|
2022-04-05 14:10:31 +03:00
|
|
|
use MessagePack\MessagePack;
|
2022-12-14 23:15:29 +03:00
|
|
|
use Nette\Utils\ImageException;
|
|
|
|
use Nette\Utils\UnknownImageFileException;
|
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";
|
2022-04-05 14:10:31 +03:00
|
|
|
|
2021-10-14 14:40:28 +03:00
|
|
|
const ALLOWED_SIDE_MULTIPLIER = 7;
|
2022-12-14 23:15:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \ImagickException
|
|
|
|
* @throws ImageException
|
|
|
|
* @throws UnknownImageFileException
|
|
|
|
*/
|
|
|
|
private function resizeImage(\Imagick $image, string $outputDir, \SimpleXMLElement $size): array
|
2022-04-05 14:10:31 +03:00
|
|
|
{
|
2022-12-14 23:15:29 +03:00
|
|
|
$res = [false];
|
2022-04-05 14:10:31 +03:00
|
|
|
$requiresProportion = ((string) $size["requireProp"]) != "none";
|
|
|
|
if($requiresProportion) {
|
2022-12-13 02:03:35 +03:00
|
|
|
$props = explode(":", (string) $size["requireProp"]);
|
2022-04-05 14:10:31 +03:00
|
|
|
$px = (int) $props[0];
|
|
|
|
$py = (int) $props[1];
|
2022-12-14 23:15:29 +03:00
|
|
|
if(($image->getImageWidth() / $image->getImageHeight()) > ($px / $py)) {
|
2022-12-21 02:26:00 +03:00
|
|
|
$height = (int) ceil(($px * $image->getImageWidth()) / $py);
|
2022-12-14 23:15:29 +03:00
|
|
|
$image->cropImage($image->getImageWidth(), $height, 0, 0);
|
2022-04-05 14:39:13 +03:00
|
|
|
$res[0] = true;
|
2022-04-05 14:10:31 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-14 23:15:29 +03:00
|
|
|
|
2022-04-05 14:10:31 +03:00
|
|
|
if(isset($size["maxSize"])) {
|
|
|
|
$maxSize = (int) $size["maxSize"];
|
2022-12-14 23:15:29 +03:00
|
|
|
$sizes = Image::calculateSize($image->getImageWidth(), $image->getImageHeight(), $maxSize, $maxSize, Image::SHRINK_ONLY | Image::FIT);
|
2022-12-15 00:09:25 +03:00
|
|
|
$image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1);
|
2022-04-05 14:10:31 +03:00
|
|
|
} else if(isset($size["maxResolution"])) {
|
|
|
|
$resolution = explode("x", (string) $size["maxResolution"]);
|
2022-12-14 23:15:29 +03:00
|
|
|
$sizes = Image::calculateSize(
|
|
|
|
$image->getImageWidth(), $image->getImageHeight(), (int) $resolution[0], (int) $resolution[1], Image::SHRINK_ONLY | Image::FIT
|
|
|
|
);
|
2022-12-15 00:09:25 +03:00
|
|
|
$image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1);
|
2022-04-05 14:10:31 +03:00
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("Malformed size description: " . (string) $size["id"]);
|
|
|
|
}
|
2022-12-14 23:15:29 +03:00
|
|
|
|
|
|
|
$res[1] = $image->getImageWidth();
|
|
|
|
$res[2] = $image->getImageHeight();
|
2022-04-05 14:10:31 +03:00
|
|
|
if($res[1] <= 300 || $res[2] <= 300)
|
2022-12-14 23:15:29 +03:00
|
|
|
$image->writeImage("$outputDir/$size[id].gif");
|
2022-04-05 14:10:31 +03:00
|
|
|
else
|
2022-12-14 23:15:29 +03:00
|
|
|
$image->writeImage("$outputDir/$size[id].jpeg");
|
|
|
|
|
|
|
|
$res[3] = true;
|
|
|
|
$image->destroy();
|
2022-04-05 14:10:31 +03:00
|
|
|
unset($image);
|
2022-12-14 23:15:29 +03:00
|
|
|
|
2022-04-05 14:10:31 +03:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2022-12-14 23:15:29 +03:00
|
|
|
private function saveImageResizedCopies(?\Imagick $image, string $filename, string $hash): void
|
2022-04-05 14:10:31 +03:00
|
|
|
{
|
2022-12-14 23:15:29 +03:00
|
|
|
if(!$image) {
|
|
|
|
$image = new \Imagick;
|
|
|
|
$image->readImage($filename);
|
|
|
|
}
|
|
|
|
|
2022-04-05 14:10:31 +03:00
|
|
|
$dir = dirname($this->pathFromHash($hash));
|
|
|
|
$dir = "$dir/$hash" . "_cropped";
|
2022-04-05 22:36:46 +03:00
|
|
|
if(!is_dir($dir)) {
|
|
|
|
@unlink($dir); # Added to transparently bypass issues with dead pesudofolders summoned by buggy SWIFT impls (selectel)
|
2022-04-05 14:10:31 +03:00
|
|
|
mkdir($dir);
|
2022-04-05 22:36:46 +03:00
|
|
|
}
|
2022-04-05 14:10:31 +03:00
|
|
|
|
|
|
|
$sizes = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml");
|
|
|
|
if(!$sizes)
|
|
|
|
throw new \RuntimeException("Could not load photosizes.xml!");
|
|
|
|
|
|
|
|
$sizesMeta = [];
|
2022-12-14 23:15:29 +03:00
|
|
|
if(OPENVK_ROOT_CONF["openvk"]["preferences"]["photos"]["photoSaving"] === "quick") {
|
|
|
|
foreach($sizes->Size as $size)
|
|
|
|
$sizesMeta[(string)$size["id"]] = [false, false, false, false];
|
|
|
|
} else {
|
|
|
|
foreach($sizes->Size as $size)
|
|
|
|
$sizesMeta[(string)$size["id"]] = $this->resizeImage(clone $image, $dir, $size);
|
|
|
|
}
|
2022-04-05 14:10:31 +03:00
|
|
|
|
|
|
|
$sizesMeta = MessagePack::pack($sizesMeta);
|
|
|
|
$this->stateChanges("sizes", $sizesMeta);
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
protected function saveFile(string $filename, string $hash): bool
|
|
|
|
{
|
2022-12-14 23:15:29 +03:00
|
|
|
$image = new \Imagick;
|
|
|
|
$image->readImage($filename);
|
|
|
|
$h = $image->getImageHeight();
|
|
|
|
$w = $image->getImageWidth();
|
|
|
|
if(($h >= ($w * Photo::ALLOWED_SIDE_MULTIPLIER)) || ($w >= ($h * Photo::ALLOWED_SIDE_MULTIPLIER)))
|
2021-10-14 14:40:28 +03:00
|
|
|
throw new ISE("Invalid layout: image is too wide/short");
|
2022-12-14 23:15:29 +03:00
|
|
|
|
|
|
|
$sizes = Image::calculateSize(
|
|
|
|
$image->getImageWidth(), $image->getImageHeight(), 8192, 4320, Image::SHRINK_ONLY | Image::FIT
|
|
|
|
);
|
2022-12-15 00:09:25 +03:00
|
|
|
$image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1);
|
2022-12-14 23:15:29 +03:00
|
|
|
$image->writeImage($this->pathFromHash($hash));
|
|
|
|
$this->saveImageResizedCopies($image, $filename, $hash);
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-04-05 15:54:53 +03:00
|
|
|
function getSizes(bool $upgrade = false, bool $forceUpdate = false): ?array
|
2022-04-05 14:10:31 +03:00
|
|
|
{
|
|
|
|
$sizes = $this->getRecord()->sizes;
|
2022-04-05 15:54:53 +03:00
|
|
|
if(!$sizes || $forceUpdate) {
|
|
|
|
if($forceUpdate || $upgrade || OPENVK_ROOT_CONF["openvk"]["preferences"]["photos"]["upgradeStructure"]) {
|
2022-12-14 23:15:29 +03:00
|
|
|
$hash = $this->getRecord()->hash;
|
|
|
|
$this->saveImageResizedCopies(NULL, $this->pathFromHash($hash), $hash);
|
2022-04-05 14:10:31 +03:00
|
|
|
$this->save();
|
|
|
|
|
|
|
|
return $this->getSizes();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = [];
|
|
|
|
$sizes = MessagePack::unpack($sizes);
|
|
|
|
foreach($sizes as $id => $meta) {
|
2022-12-14 23:15:29 +03:00
|
|
|
if(isset($meta[3]) && !$meta[3]) {
|
|
|
|
$res[$id] = (object) [
|
2022-12-21 02:20:17 +03:00
|
|
|
"url" => ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/photos/thumbnails/" . $this->getId() . "_$id.jpeg",
|
2022-12-14 23:15:29 +03:00
|
|
|
"width" => NULL,
|
|
|
|
"height" => NULL,
|
|
|
|
"crop" => NULL
|
|
|
|
];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-05 14:10:31 +03:00
|
|
|
$url = $this->getURL();
|
|
|
|
$url = str_replace(".$this->fileExtension", "_cropped/$id.", $url);
|
|
|
|
$url .= ($meta[1] <= 300 || $meta[2] <= 300) ? "gif" : "jpeg";
|
|
|
|
|
|
|
|
$res[$id] = (object) [
|
|
|
|
"url" => $url,
|
|
|
|
"width" => $meta[1],
|
|
|
|
"height" => $meta[2],
|
|
|
|
"crop" => $meta[0]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
[$x, $y] = $this->getDimensions();
|
|
|
|
$res["UPLOADED_MAXRES"] = (object) [
|
|
|
|
"url" => $this->getURL(),
|
|
|
|
"width" => $x,
|
|
|
|
"height" => $y,
|
|
|
|
"crop" => false
|
|
|
|
];
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
2022-12-14 23:15:29 +03:00
|
|
|
|
|
|
|
function forceSize(string $sizeName): bool
|
|
|
|
{
|
|
|
|
$hash = $this->getRecord()->hash;
|
|
|
|
$sizes = MessagePack::unpack($this->getRecord()->sizes);
|
|
|
|
$size = $sizes[$sizeName] ?? false;
|
|
|
|
if(!$size)
|
|
|
|
return $size;
|
|
|
|
|
|
|
|
if(!isset($size[3]) || $size[3] === true)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
$path = $this->pathFromHash($hash);
|
|
|
|
$dir = dirname($this->pathFromHash($hash));
|
|
|
|
$dir = "$dir/$hash" . "_cropped";
|
|
|
|
if(!is_dir($dir)) {
|
|
|
|
@unlink($dir);
|
|
|
|
mkdir($dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sizeMetas = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml");
|
|
|
|
if(!$sizeMetas)
|
|
|
|
throw new \RuntimeException("Could not load photosizes.xml!");
|
|
|
|
|
|
|
|
$sizeInfo = NULL;
|
|
|
|
foreach($sizeMetas->Size as $size)
|
|
|
|
if($size["id"] == $sizeName)
|
|
|
|
$sizeInfo = $size;
|
|
|
|
|
|
|
|
if(!$sizeInfo)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$pic = new \Imagick;
|
|
|
|
$pic->readImage($path);
|
|
|
|
$sizes[$sizeName] = $this->resizeImage($pic, $dir, $sizeInfo);
|
|
|
|
|
|
|
|
$this->stateChanges("sizes", MessagePack::pack($sizes));
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
return $sizes[$sizeName][3];
|
|
|
|
}
|
2022-04-05 14:10:31 +03:00
|
|
|
|
|
|
|
function getVkApiSizes(): ?array
|
|
|
|
{
|
|
|
|
$res = [];
|
|
|
|
$sizes = $this->getSizes();
|
|
|
|
if(!$sizes)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
$manifest = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml");
|
|
|
|
if(!$manifest)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
$mappings = [];
|
|
|
|
foreach($manifest->Size as $size)
|
|
|
|
$mappings[(string) $size["id"]] = (string) $size["vkId"];
|
|
|
|
|
2022-04-24 15:38:53 +03:00
|
|
|
foreach($sizes as $id => $meta) {
|
|
|
|
$type = $mappings[$id] ?? $id;
|
|
|
|
$meta->type = $type;
|
|
|
|
$res[$type] = $meta;
|
|
|
|
}
|
2022-04-05 14:10:31 +03:00
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getURLBySizeId(string $size): string
|
|
|
|
{
|
|
|
|
$sizes = $this->getSizes();
|
|
|
|
if(!$sizes)
|
|
|
|
return $this->getURL();
|
|
|
|
|
|
|
|
$size = $sizes[$size];
|
|
|
|
if(!$size)
|
|
|
|
return $this->getURL();
|
|
|
|
|
|
|
|
return $size->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-04-05 14:10:31 +03:00
|
|
|
$image = Image::fromFile($this->pathFromHash($hash));
|
2022-03-29 20:43:34 +03:00
|
|
|
|
2022-04-05 14:10:31 +03:00
|
|
|
$x = $image->getWidth();
|
|
|
|
$y = $image->getHeight();
|
2022-04-05 11:38:19 +03:00
|
|
|
$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-04-05 14:10:31 +03:00
|
|
|
}
|
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();
|
|
|
|
|
2022-04-05 14:10:31 +03:00
|
|
|
$res->sizes = $this->getVkApiSizes();
|
|
|
|
$res->src_small = $res->photo_75 = $this->getURLBySizeId("miniscule");
|
|
|
|
$res->src = $res->photo_130 = $this->getURLBySizeId("tiny");
|
|
|
|
$res->src_big = $res->photo_604 = $this->getURLBySizeId("normal");
|
|
|
|
$res->src_xbig = $res->photo_807 = $this->getURLBySizeId("large");
|
|
|
|
$res->src_xxbig = $res->photo_1280 = $this->getURLBySizeId("larger");
|
|
|
|
$res->src_xxxbig = $res->photo_2560 = $this->getURLBySizeId("original");
|
|
|
|
$res->src_original = $res->url = $this->getURLBySizeId("UPLOADED_MAXRES");
|
2022-03-29 20:43:34 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-07-29 12:41:43 +03:00
|
|
|
if(!is_null($album)) {
|
2021-10-12 14:17:21 +03:00
|
|
|
$album->addPhoto($photo);
|
2022-07-29 12:41:43 +03:00
|
|
|
$album->setEdited(time());
|
|
|
|
$album->save();
|
|
|
|
}
|
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
|
|
|
}
|