mirror of
https://github.com/claradex/nativegallery.git
synced 2024-11-15 03:31:10 +03:00
compress img api
This commit is contained in:
parent
c80530a6fa
commit
c7e5f101b5
11 changed files with 114 additions and 8 deletions
90
app/Controllers/Api/Images/Compress.php
Normal file
90
app/Controllers/Api/Images/Compress.php
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controllers\Api\Images;
|
||||||
|
|
||||||
|
class Compress {
|
||||||
|
private static function compressAndResizeImage($source_url, $quality, $max_width, $max_height) {
|
||||||
|
$info = getimagesize($source_url);
|
||||||
|
|
||||||
|
if ($info === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$width = $info[0];
|
||||||
|
$height = $info[1];
|
||||||
|
$aspect_ratio = $width / $height;
|
||||||
|
|
||||||
|
// Вычисляем новые размеры, сохраняя соотношение сторон
|
||||||
|
if ($width > $height) {
|
||||||
|
$new_width = $max_width;
|
||||||
|
$new_height = $max_width / $aspect_ratio;
|
||||||
|
} else {
|
||||||
|
$new_height = $max_height;
|
||||||
|
$new_width = $max_height * $aspect_ratio;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($info['mime'] == 'image/jpeg') {
|
||||||
|
$image = imagecreatefromjpeg($source_url);
|
||||||
|
} elseif ($info['mime'] == 'image/gif') {
|
||||||
|
$image = imagecreatefromgif($source_url);
|
||||||
|
} elseif ($info['mime'] == 'image/png') {
|
||||||
|
$image = imagecreatefrompng($source_url);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$resized_image = imagecreatetruecolor($new_width, $new_height);
|
||||||
|
|
||||||
|
if ($info['mime'] == 'image/png' || $info['mime'] == 'image/gif') {
|
||||||
|
imagealphablending($resized_image, false);
|
||||||
|
imagesavealpha($resized_image, true);
|
||||||
|
$transparent = imagecolorallocatealpha($resized_image, 255, 255, 255, 127);
|
||||||
|
imagefilledrectangle($resized_image, 0, 0, $new_width, $new_height, $transparent);
|
||||||
|
}
|
||||||
|
|
||||||
|
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
imagejpeg($resized_image, null, $quality);
|
||||||
|
$compressed_image_data = ob_get_contents();
|
||||||
|
ob_end_clean();
|
||||||
|
|
||||||
|
imagedestroy($image);
|
||||||
|
imagedestroy($resized_image);
|
||||||
|
|
||||||
|
return $compressed_image_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function generateCacheFilename($source_url, $quality, $max_width, $max_height) {
|
||||||
|
return $_SERVER['DOCUMENT_ROOT'].'/cdn/imgcache/' . md5($source_url . $quality . $max_width . $max_height) . '.jpg';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$source_url = $_GET['url'];
|
||||||
|
$quality = 40;
|
||||||
|
$max_width = 400;
|
||||||
|
$max_height = 400;
|
||||||
|
|
||||||
|
$cache_filename = self::generateCacheFilename($source_url, $quality, $max_width, $max_height);
|
||||||
|
|
||||||
|
if (file_exists($cache_filename)) {
|
||||||
|
$compressed_image_data = file_get_contents($cache_filename);
|
||||||
|
} else {
|
||||||
|
$compressed_image_data = self::compressAndResizeImage($source_url, $quality, $max_width, $max_height);
|
||||||
|
|
||||||
|
if ($compressed_image_data) {
|
||||||
|
file_put_contents($cache_filename, $compressed_image_data);
|
||||||
|
} else {
|
||||||
|
echo "Произошла ошибка при сжатии изображения.";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($compressed_image_data) {
|
||||||
|
header('Content-Type: image/jpeg');
|
||||||
|
header('Content-Length: ' . strlen($compressed_image_data));
|
||||||
|
echo $compressed_image_data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
|
@ -32,7 +32,7 @@ class Upload
|
||||||
if ($_FILES['image']['error'] != 4) {
|
if ($_FILES['image']['error'] != 4) {
|
||||||
$exif = new EXIF($_FILES['image']['tmp_name']);
|
$exif = new EXIF($_FILES['image']['tmp_name']);
|
||||||
$exif = $exif->getData();
|
$exif = $exif->getData();
|
||||||
$upload = new UploadPhoto($_FILES['image'], 'cdn/img');
|
$upload = new UploadPhoto($_FILES['image'], 'cdn/img/');
|
||||||
if ($exif === null) {
|
if ($exif === null) {
|
||||||
$exif = Json::return(
|
$exif = Json::return(
|
||||||
array(
|
array(
|
||||||
|
|
|
@ -7,6 +7,7 @@ use \App\Core\Page;
|
||||||
use \App\Controllers\Api\{Login, Register};
|
use \App\Controllers\Api\{Login, Register};
|
||||||
use \App\Controllers\Api\Images\{Upload};
|
use \App\Controllers\Api\Images\{Upload};
|
||||||
use \App\Controllers\Api\Images\Rate as PhotoVote;
|
use \App\Controllers\Api\Images\Rate as PhotoVote;
|
||||||
|
use \App\Controllers\Api\Images\Compress as PhotoCompress;
|
||||||
use \App\Controllers\Api\Images\Comments\Create as PhotoComment;
|
use \App\Controllers\Api\Images\Comments\Create as PhotoComment;
|
||||||
use \App\Controllers\Api\Images\Comments\Load as PhotoCommentLoad;
|
use \App\Controllers\Api\Images\Comments\Load as PhotoCommentLoad;
|
||||||
use \App\Controllers\Api\Images\Comments\Rate as PhotoCommentVote;
|
use \App\Controllers\Api\Images\Comments\Rate as PhotoCommentVote;
|
||||||
|
@ -39,6 +40,9 @@ class ApiController
|
||||||
public static function updateprofile() {
|
public static function updateprofile() {
|
||||||
return new ProfileUpdate();
|
return new ProfileUpdate();
|
||||||
}
|
}
|
||||||
|
public static function photocompress() {
|
||||||
|
return new PhotoCompress();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -35,6 +35,7 @@ class Routes
|
||||||
Router::post('/api/upload', 'ApiController@upload');
|
Router::post('/api/upload', 'ApiController@upload');
|
||||||
Router::post('/api/profile/update', 'ApiController@updateprofile');
|
Router::post('/api/profile/update', 'ApiController@updateprofile');
|
||||||
Router::post('/api/photo/comment', 'ApiController@photocomment');
|
Router::post('/api/photo/comment', 'ApiController@photocomment');
|
||||||
|
Router::get('/api/photo/compress', 'ApiController@photocompress');
|
||||||
Router::post('/api/photo/getcomments/$id', 'ApiController@photocommentload');
|
Router::post('/api/photo/getcomments/$id', 'ApiController@photocommentload');
|
||||||
Router::get('/api/photo/vote', 'ApiController@photovote');
|
Router::get('/api/photo/vote', 'ApiController@photovote');
|
||||||
Router::get('/api/photo/comment/rate', 'ApiController@photocommentvote');
|
Router::get('/api/photo/comment/rate', 'ApiController@photocommentvote');
|
||||||
|
|
|
@ -40,7 +40,7 @@ class Upload
|
||||||
|
|
||||||
$s3->putObject([
|
$s3->putObject([
|
||||||
'Bucket' => NGALLERY['root']['storage']['s3']['credentials']['bucket'],
|
'Bucket' => NGALLERY['root']['storage']['s3']['credentials']['bucket'],
|
||||||
'Key' => 'cdn/img/'.$filecdn,
|
'Key' => $location.$filecdn,
|
||||||
'SourceFile' => $file['tmp_name']
|
'SourceFile' => $file['tmp_name']
|
||||||
]);
|
]);
|
||||||
$this->type = explode('/', $file['type'])[0];
|
$this->type = explode('/', $file['type'])[0];
|
||||||
|
|
BIN
static/PEPPERSCREEN.ttf
Normal file
BIN
static/PEPPERSCREEN.ttf
Normal file
Binary file not shown.
BIN
static/img/248.ico
Normal file
BIN
static/img/248.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
BIN
static/img/sttslogo.png
Normal file
BIN
static/img/sttslogo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
|
@ -41,6 +41,17 @@ use \App\Models\User;
|
||||||
?>
|
?>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="p20">
|
||||||
|
<h4><img src="/static/img/248.ico">Фотомодераторы</h4>
|
||||||
|
<ul style="list-style: none; margin: 0; padding: 0;">
|
||||||
|
<?php
|
||||||
|
$admins = DB::query('SELECT * FROM users WHERE admin=2');
|
||||||
|
foreach ($admins as $a) {
|
||||||
|
echo '<li><b><a href="/author/'.$a['id'].'/"><img onerror="this.src = `/static/img/avatar.png`; this.onerror = null;" src="'.$a['photourl'].'" width="32" style="border-radius: 3px; margin-right: 5px;">'.htmlspecialchars($a['username']).'</a></b></li>';
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -75,7 +75,7 @@ foreach ($photos as $pd) {
|
||||||
foreach ($photo as $p) {
|
foreach ($photo as $p) {
|
||||||
$author = new User($p['user_id']);
|
$author = new User($p['user_id']);
|
||||||
echo '<a href="/photo/'.$p['id'].'" target="_blank" class="prw pop-prw">
|
echo '<a href="/photo/'.$p['id'].'" target="_blank" class="prw pop-prw">
|
||||||
<img width="250" src="'.$p['photourl'].'">
|
<img width="250" src="/api/photo/compress?url='.$p['photourl'].'">
|
||||||
<div class="hpshade">
|
<div class="hpshade">
|
||||||
<div class="eye-icon">+'.$pd['view_count'].'</div>
|
<div class="eye-icon">+'.$pd['view_count'].'</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -100,13 +100,13 @@ foreach ($photos as $pd) {
|
||||||
<?php
|
<?php
|
||||||
$photos = DB::query('SELECT * FROM photos ORDER BY RAND() DESC LIMIT 7');
|
$photos = DB::query('SELECT * FROM photos ORDER BY RAND() DESC LIMIT 7');
|
||||||
foreach ($photos as $p) {
|
foreach ($photos as $p) {
|
||||||
$bck = 'background-image:url("' . $p['photourl'] . '")';
|
$bck = 'background-image:url("/api/photo/compress?url=' . $p['photourl'] . '")';
|
||||||
echo ' <div class="prw-grid-item">
|
echo ' <div class="prw-grid-item">
|
||||||
<div class="prw-wrapper"><span style="word-spacing:-1px"><b>' . htmlspecialchars($p['place']) . '</b></span>
|
<div class="prw-wrapper"><span style="word-spacing:-1px"><b>' . htmlspecialchars($p['place']) . '</b></span>
|
||||||
<div>' . Date::zmdate($p['posted_at']) . '</div>
|
<div>' . Date::zmdate($p['posted_at']) . '</div>
|
||||||
</div>
|
</div>
|
||||||
'; ?>
|
'; ?>
|
||||||
<a href="/photo/<?= $p['id'] ?>" target="_blank" class="prw-animate" style='background-image:url("<?= $p['photourl'] ?>")'></a>
|
<a href="/photo/<?= $p['id'] ?>" target="_blank" class="prw-animate" style='background-image:url("/api/photo/compress?url=<?= $p['photourl'] ?>")'></a>
|
||||||
<?php echo '
|
<?php echo '
|
||||||
</div>';
|
</div>';
|
||||||
}
|
}
|
||||||
|
@ -123,13 +123,13 @@ foreach ($photos as $pd) {
|
||||||
<?php
|
<?php
|
||||||
$photos = DB::query('SELECT * FROM photos ORDER BY id DESC LIMIT 30');
|
$photos = DB::query('SELECT * FROM photos ORDER BY id DESC LIMIT 30');
|
||||||
foreach ($photos as $p) {
|
foreach ($photos as $p) {
|
||||||
$bck = 'background-image:url("' . $p['photourl'] . '")';
|
$bck = 'background-image:url("/api/photo/compress?url=' . $p['photourl'] . '")';
|
||||||
echo ' <div class="prw-grid-item">
|
echo ' <div class="prw-grid-item">
|
||||||
<div class="prw-wrapper"><span style="word-spacing:-1px"><b>' . htmlspecialchars($p['place']) . '</b></span>
|
<div class="prw-wrapper"><span style="word-spacing:-1px"><b>' . htmlspecialchars($p['place']) . '</b></span>
|
||||||
<div>' . Date::zmdate($p['posted_at']) . '</div>
|
<div>' . Date::zmdate($p['posted_at']) . '</div>
|
||||||
</div>
|
</div>
|
||||||
'; ?>
|
'; ?>
|
||||||
<a href="/photo/<?= $p['id'] ?>" target="_blank" class="prw-animate" style='background-image:url("<?= $p['photourl'] ?>")'></a>
|
<a href="/photo/<?= $p['id'] ?>" target="_blank" class="prw-animate" style='background-image:url("/api/photo/compress?url=<?= $p['photourl'] ?>")'></a>
|
||||||
</div>
|
</div>
|
||||||
<?php }
|
<?php }
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -128,7 +128,7 @@ if ($photo->i('id') !== null) {
|
||||||
<tr>
|
<tr>
|
||||||
<?php
|
<?php
|
||||||
if ($photo->i('place') != null) { ?>
|
if ($photo->i('place') != null) { ?>
|
||||||
<td class="nw" valign="top" align="right"><b><?= $photo->i('place') ?></b></td>
|
<td class="nw" valign="top" align="right"><b><?= $photo->i('postbody') ?></b></td>
|
||||||
<?php } ?>
|
<?php } ?>
|
||||||
<td class="nw" align="left" valign="top"></td>
|
<td class="nw" align="left" valign="top"></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
Loading…
Reference in a new issue