Files
Admin Nasledstvo ac168868ee Initial import
2026-05-01 20:52:04 +03:00

111 lines
3.8 KiB
PHP

<?php
namespace app\widgets\services;
class Image
{
public function crateThumb($media_key, $object_key, $file_name, $file_type, $temp)
{
$file_dest = $this->getFileDest($media_key, $object_key, $file_name, 'thumb');
$this->scaleImage($temp, $file_type, $file_dest, 0.3, 50);
}
public function createCrop($crop, $file, $media_key, $object_key)
{
$file_dest = $this->getFileDest($media_key, $object_key, $file['name'], $crop);
$this->scaleImage($file['tmp_name'], $file['type'], $file_dest, 0.7, 55);
}
private function getFileDest($media_key, $object_key, $file_name, $last_dir)
{
$dir_ready = $_SERVER['DOCUMENT_ROOT'] . '/_files/ready';
$dir_media = $_SERVER['DOCUMENT_ROOT'] . '/_files/ready/' . $media_key;
$dir_object = $_SERVER['DOCUMENT_ROOT'] . '/_files/ready/' . $media_key . '/' . $object_key;
$dir_last = "$dir_object/$last_dir";
$file_dest = "$dir_last/$file_name";
if (!file_exists($dir_ready))
mkdir($dir_ready, 0777);
if (!file_exists($dir_media))
mkdir($dir_media, 0777);
if (!file_exists($dir_object))
mkdir($dir_object, 0777);
if (!file_exists($dir_last))
mkdir($dir_last, 0777);
return $file_dest;
}
private function cropImage($file, $file_type, $destination, $thumb_width = 0, $thumb_height = 0, $quality = 100)
{
switch ($file_type) {
case 'image/jpg':
case 'image/jpeg':
$image = imagecreatefromjpeg($file);
break;
case 'image/png':
$image = imagecreatefrompng($file);
$quality = 9;
break;
default:
echo json_encode(['error', 'Unsupported file format: ' . $file_type]);
exit;
}
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ($original_aspect >= $thumb_aspect) {
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
} else {
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2,
0 - ($new_height - $thumb_height) / 2,
0, 0,
$new_width, $new_height,
$width, $height);
if ($file_type == 'image/png') {
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagepng($thumb, $destination, $quality);
} else {
imagejpeg($thumb, $destination, $quality);
}
}
private function scaleImage($file, $file_type, $destination, $scale, $quality)
{
switch ($file_type) {
case 'image/jpg':
case 'image/jpeg':
$image = imagecreatefromjpeg($file);
break;
case 'image/png':
$image = imagecreatefrompng($file);
$quality = 9;
break;
default:
echo json_encode(['error', 'Unsupported file format: ' . $file_type]);
exit;
}
list($width, $height) = getimagesize($file);
$scaledImage = imagescale($image, ($width * $scale), ($height * $scale));
if ($file_type == 'image/png') {
imagealphablending($scaledImage, false);
imagesavealpha($scaledImage, true);
imagepng($scaledImage, $destination, $quality);
} else {
imagejpeg($scaledImage, $destination, $quality);
}
}
}