125 lines
3.5 KiB
PHP
125 lines
3.5 KiB
PHP
<?php
|
|
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
require_once(__DIR__."/../../config.php");
|
|
require_once(__DIR__ . "/SelectelApi.php");
|
|
|
|
if (php_sapi_name() !== 'cli' && php_sapi_name() !== 'phpdbg') {
|
|
// Prevent direct web access
|
|
header('HTTP/1.0 403 Forbidden');
|
|
exit('CLI access only.');
|
|
}
|
|
|
|
set_time_limit(0);
|
|
ignore_user_abort(true);
|
|
|
|
$arguments = $argv;
|
|
|
|
class ThumbnailWorker {
|
|
private $mediaId;
|
|
private $logFile;
|
|
|
|
public function __construct()
|
|
{
|
|
global $arguments;
|
|
|
|
$this->mediaId = isset($arguments[1]) ? $arguments[1] : null;
|
|
$this->logFile = isset($arguments[2]) ? $arguments[2] : null;
|
|
}
|
|
|
|
public function run() {
|
|
if ($this->logFile && !is_dir(dirname($this->logFile))) {
|
|
mkdir(dirname($this->logFile), 0777, true);
|
|
}
|
|
// Fetch pending items
|
|
$sql = "SELECT id, complex_house_id, file_path FROM complex_house_media WHERE id = {$this->mediaId}";
|
|
|
|
$q = mysql_query($sql);
|
|
$media = mysql_fetch_assoc($q);
|
|
|
|
if (!$media) {
|
|
exit("No pending thumbnails to process.\n");
|
|
}
|
|
|
|
$thumb_path = $this->generateThumbnail($media);
|
|
|
|
if ($thumb_path !== false) {
|
|
$update_sql = "UPDATE complex_house_media SET thumbnail_path = '$thumb_path' WHERE id = {$this->mediaId}";
|
|
mysql_query($update_sql);
|
|
}
|
|
}
|
|
|
|
private function generateThumbnail($media) {
|
|
try {
|
|
$mediaId = $media['id'];
|
|
$complexHouseId = $media['complex_house_id'];
|
|
$source_path = 'https://data.joywork.ru' . $media['file_path'];
|
|
|
|
$imageData = file_get_contents($source_path);
|
|
$image = new Imagick();
|
|
$image->readImageBlob($imageData);
|
|
|
|
$image->setImageFormat('jpeg');
|
|
$image->thumbnailImage(300, 300, true, true);
|
|
$image->cropThumbnailImage(300, 300);
|
|
|
|
$parts = explode('/', $media['file_path']);
|
|
$basePath = '/' . implode('/', array_slice($parts, 3, -1));
|
|
$baseName = basename($media['file_path']);
|
|
|
|
|
|
$tempName = time();
|
|
|
|
$thumbLocalPath = "medias/$complexHouseId/thumbs$basePath/" . $tempName . "_thumb.jpg";
|
|
$thumbRemotePath = "/medias/$complexHouseId/thumbs$basePath/" . $baseName;
|
|
|
|
// Ensure directory exists
|
|
$dir = dirname($thumbLocalPath);
|
|
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0777, true);
|
|
}
|
|
|
|
$image->writeImage($thumbLocalPath);
|
|
$image->clear();
|
|
$image->destroy();
|
|
|
|
$Selectel = SelectelApi::getTokenAndUrl();
|
|
$token = $Selectel[0];
|
|
$storageUrl = $Selectel[1];
|
|
$SelectelApi = new SelectelApi();
|
|
|
|
$uploadThumb = $SelectelApi->uploadFile(
|
|
$storageUrl,
|
|
$token,
|
|
$thumbLocalPath,
|
|
$thumbRemotePath,
|
|
'image/jpeg'
|
|
);
|
|
|
|
if ($uploadThumb) {
|
|
unlink($thumbLocalPath);
|
|
return $thumbRemotePath;
|
|
}
|
|
|
|
return false;
|
|
|
|
} catch (Exception $e) {
|
|
$this->logger('Thumbnail error: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
function logger($message)
|
|
{
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
print("[$timestamp] $message \n");
|
|
}
|
|
}
|
|
|
|
// Execute worker
|
|
$worker = new ThumbnailWorker();
|
|
$worker->run();
|
|
?>
|