nativegallery-weryskok/app/Controllers/Api/Images/Upload.php

149 lines
6.1 KiB
PHP
Raw Normal View History

2024-07-05 07:26:39 +03:00
<?php
namespace App\Controllers\Api\Images;
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF};
use App\Services\Upload as UploadPhoto;
use \Aws\S3\MultipartUploader;
use \Aws\Exception\MultipartUploadException;
class Upload
{
static $continue;
static $photourl;
2024-07-21 02:22:08 +03:00
static $vidpreview;
static $videourl;
2024-07-05 07:26:39 +03:00
public static function create($postbody, $content, $exif)
{
$user = new \App\Models\User(Auth::userid());
if (NGALLERY['root']['photo']['upload']['premoderation'] === true) {
2024-07-20 22:09:13 +03:00
if ($user->content('premoderation') === "true" || $user->i('admin') > 0) {
$moderated = 1;
} else {
$moderated = 0;
}
} else {
$moderated = 1;
}
2024-07-21 02:22:08 +03:00
DB::query('INSERT INTO photos VALUES (\'0\', :userid, :postbody, :photourl, :time, :timeup, :exif, 0, :moderated, :place, 0, :content)', array(':postbody' => $postbody, ':userid' => Auth::userid(), ':time' => mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']), ':content' => $content, ':photourl' => self::$photourl, ':exif' => $exif, ':place' => $_POST['place'], ':timeup' => time(), ':moderated' => $moderated));
2024-07-20 23:40:47 +03:00
if ($moderated === 1) {
2024-07-21 02:22:08 +03:00
$followers = DB::query('SELECT * FROM followers WHERE user_id=:uid', array(':uid' => Auth::userid()));
2024-07-20 23:40:47 +03:00
foreach ($followers as $f) {
2024-07-21 02:22:08 +03:00
DB::query('INSERT INTO followers_notifications VALUES (\'0\', :uid, :fid, :pid, 0)', array(':uid' => Auth::userid(), ':fid' => $f['follower_id'], ':pid' => DB::query('SELECT * FROM photos ORDER BY id DESC LIMIT 1')[0]['id']));
2024-07-20 23:40:47 +03:00
}
}
2024-07-05 07:26:39 +03:00
echo json_encode(
array(
2024-07-05 12:44:31 +03:00
'id' => DB::query('SELECT id FROM photos ORDER BY id DESC LIMIT 1')[0]['id'],
2024-07-05 07:26:39 +03:00
'errorcode' => 0,
'error' => 0
)
);
}
public function __construct()
2024-07-21 02:22:08 +03:00
{
2024-07-05 07:26:39 +03:00
2024-07-05 09:49:49 +03:00
if ($_FILES['image']['error'] != 4) {
2024-07-17 03:58:19 +03:00
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $_FILES['image']['tmp_name']);
if ($type === 'image/gif') {
if (NGALLERY['root']['photo']['upload']['allowgif'] === false) {
echo json_encode(
array(
'errorcode' => 'FILE_NOTSUPPORTED',
'error' => 1
)
);
die();
}
}
2024-07-21 02:22:08 +03:00
if (explode('/', $type)[0] === 'video') {
$newname = GenerateRandomStr::init(64);
$ffmpegPath = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'E:\Maksim\kandle\app\Controllers\Video\Exec\ffmpeg.exe' : 'ffmpeg';
$tempDir = $_SERVER['DOCUMENT_ROOT'] . '/cdn/temp/';
$mp4File = $tempDir . $newname . '.mp4';
$ffmpegCommand = "$ffmpegPath -i " . $_FILES['image']['tmp_name'] . " -c:v libx264 -crf 18 -fpsmax 60 -preset fast -c:a aac -ac 2 -codec:v copy -codec:a copy $mp4File";
exec($ffmpegCommand);
$thumbnailFile = $tempDir . $newname . '.jpg';
$ffmpegCommand = "$ffmpegPath -i " . $_FILES['image']['tmp_name'] . " -ss 00:00:00 -frames:v 1 -q:v 2 $thumbnailFile";
exec($ffmpegCommand);
$backgroundImagePath = $thumbnailFile;
$overlayImagePath = $_SERVER['DOCUMENT_ROOT'] . '/static/img/playic.png';
$background = imagecreatefromjpeg($backgroundImagePath);
$overlay = imagecreatefrompng($overlayImagePath);
$backgroundWidth = imagesx($background);
$backgroundHeight = imagesy($background);
$overlayWidth = imagesx($overlay);
$overlayHeight = imagesy($overlay);
$destX = ($backgroundWidth - $overlayWidth) / 2;
$destY = ($backgroundHeight - $overlayHeight) / 2;
imagecopy($background, $overlay, $destX, $destY, 0, 0, $overlayWidth, $overlayHeight);
$outputImagePath = $_SERVER['DOCUMENT_ROOT'] . '/cdn/temp/VIDPRV_' . $newname . '.jpg';
imagejpeg($background, $outputImagePath, 90);
imagedestroy($background);
imagedestroy($overlay);
$upload = new UploadPhoto($outputImagePath, 'cdn/img/');
self::$vidpreview = $upload->getSrc();
$upload = new UploadPhoto($mp4File, 'cdn/video/');
self::$videourl = $upload->getSrc();
} else if (explode($type, '/')[0] === 'image') {
$exif = new EXIF($_FILES['image']['tmp_name']);
$exif = $exif->getData();
$upload = new UploadPhoto($_FILES['image'], 'cdn/img/');
if ($exif === null) {
$exif = Json::return(
array(
'type' => 'none',
)
);
}
} else {
echo json_encode(
2024-07-05 12:44:31 +03:00
array(
2024-07-21 02:22:08 +03:00
'errorcode' => 'FILE_NOTSELECTED',
'error' => 1
2024-07-05 12:44:31 +03:00
)
);
}
2024-07-06 08:05:10 +03:00
if (isset($_POST['nomap'])) {
$_POST['lat'] = null;
$_POST['lng'] = null;
}
2024-07-05 09:49:49 +03:00
if ($upload->getType() !== null) {
2024-07-05 07:26:39 +03:00
$content = Json::return(
array(
2024-07-21 02:22:08 +03:00
'type' => explode('/', $type)[0],
'videourl' => self::$videourl,
2024-07-05 09:49:49 +03:00
'copyright' => $_POST['license'],
2024-07-05 12:58:12 +03:00
'comment' => $_POST['descr'],
2024-07-06 08:05:10 +03:00
'lat' => $_POST['lat'],
'lng' => $_POST['lng']
2024-07-05 07:26:39 +03:00
)
);
2024-07-21 02:22:08 +03:00
if (explode('/', $type)[0] === 'video') {
self::$photourl = self::$vidpreview;
} else {
self::$photourl = $upload->getSrc();
}
2024-07-05 12:44:31 +03:00
self::create($_POST['descr'], $content, $exif);
2024-07-05 07:26:39 +03:00
}
}
}
2024-07-21 02:22:08 +03:00
}