1013 lines
37 KiB
Plaintext
1013 lines
37 KiB
Plaintext
<?
|
|
namespace common;
|
|
|
|
use common\image\structure\eAutologoOptions;
|
|
use core\db\structure\Column as C;
|
|
use core\db\structure\eColumnType;
|
|
use shop\shop\structure\eAutologoPosition;
|
|
use shop\shop\structure\eAutologoType;
|
|
use themes\ManyQuests;
|
|
use ui\form\structure\eInputType;
|
|
use ui\Form;
|
|
use ui\input\Input;
|
|
use core\lang\structure\LangVariable as V;
|
|
use ms\ms\structure\msModuleTable;
|
|
use common\image\structure\imageLang as Vars;
|
|
|
|
|
|
|
|
|
|
class Image extends msModuleTable {
|
|
static $self;
|
|
static $table_name = 'image';
|
|
static $controller = 'common.image';
|
|
|
|
const PNG_1PX = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTM4IDc5LjE1OTgyNCwgMjAxNi8wOS8xNC0wMTowOTowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTcgKFdpbmRvd3MpIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOkM0MDY4MzUxM0QxNjExRTlBNjdBOTYxRTdFOUU5RDg5IiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOkM0MDY4MzUyM0QxNjExRTlBNjdBOTYxRTdFOUU5RDg5Ij4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6QzQwNjgzNEYzRDE2MTFFOUE2N0E5NjFFN0U5RTlEODkiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6QzQwNjgzNTAzRDE2MTFFOUE2N0E5NjFFN0U5RTlEODkiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz5cn6+MAAAAEElEQVR42mL6//8/A0CAAQAJBgMA+A+HZAAAAABJRU5ErkJggg==';
|
|
|
|
static $ID = 'id';
|
|
static $NAME = 'name';
|
|
static $URL = 'url';
|
|
static $ORIGINAL = 'original';
|
|
static $EXT = 'ext';
|
|
static $PROFILE = 'profile';
|
|
static $CLOUD = 'cloud';
|
|
static $ACCOUNT = 'account';
|
|
static $CREATED = 'created';
|
|
static $UPDATED = 'updated';
|
|
|
|
const UPLOAD_URL_PROFILE = '/api/common.image/uploadProfile/';
|
|
const UPLOAD_URL_ACCOUNT = '/api/common.image/upload/';
|
|
|
|
|
|
static function getTitle(){return V::get(Vars::$MODULE_NAME);}
|
|
static function getSystemLangValues():array{ return Vars::getArray(); }
|
|
public function _update(\Where $w, array $values) {
|
|
$values[self::$UPDATED] = time();
|
|
return parent::_update($w, $values);
|
|
}
|
|
|
|
public static function installUniques() {
|
|
\DB::addIndex(self::$table_name,self::$PROFILE);
|
|
\DB::addIndex(self::$table_name,self::$ACCOUNT);
|
|
}
|
|
|
|
static function calculateTextBox($text,$fontFile,$fontSize,$fontAngle=0) {
|
|
|
|
/************
|
|
|
|
simple function that calculates the *exact* bounding box (single pixel precision).
|
|
|
|
The function returns an associative array with these keys:
|
|
|
|
left, top: coordinates you will pass to imagettftext
|
|
|
|
width, height: dimension of the image you have to create
|
|
|
|
*************/
|
|
|
|
$rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text);
|
|
|
|
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
|
|
|
|
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
|
|
|
|
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
|
|
|
|
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
|
|
|
|
|
|
|
|
return array(
|
|
|
|
"left" => abs($minX) - 1,
|
|
|
|
"top" => abs($minY) - 1,
|
|
|
|
"width" => $maxX - $minX,
|
|
|
|
"height" => $maxY - $minY,
|
|
|
|
"box" => $rect
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
static function add($values){
|
|
return static::insert($values);
|
|
}
|
|
|
|
static function format($img,$cloud=null,$options = []){
|
|
if(is_numeric($img)) $img = Image::getByID($img);
|
|
$width = [
|
|
100 => Image::imageResize($img,100,0,90,['crop'=>'center','cloud'=>$cloud]),
|
|
200 => Image::imageResize($img,200,0,90,['crop'=>'center','cloud'=>$cloud]),
|
|
640 => Image::imageResize($img,640,0,90,['crop'=>'center','cloud'=>$cloud]),
|
|
800 => Image::imageResize($img,800,0,90,['crop'=>'center','cloud'=>$cloud]),
|
|
];
|
|
if($options['widths']){
|
|
foreach ($options['widths'] as $v){
|
|
$width[$v] = Image::imageResize($img,$v,0,90,['crop'=>'center','cloud'=>$cloud]);
|
|
}
|
|
}
|
|
return [
|
|
'id' => $img[Image::$ID],
|
|
'url' => $img[Image::$URL],
|
|
'thumb' => [
|
|
'square'=>[
|
|
100 => Image::imageResize($img,100,100,90,['crop'=>'center','cloud'=>$cloud]),
|
|
200 => Image::imageResize($img,200,200,90,['crop'=>'center','cloud'=>$cloud]),
|
|
640 => Image::imageResize($img,640,640,90,['crop'=>'center','cloud'=>$cloud]),
|
|
],
|
|
'width'=>$width,
|
|
],
|
|
];
|
|
}
|
|
|
|
|
|
static function upload($options=[]){
|
|
$ALLOW_IMAGE_UPLOAD = ['jpg','jpeg','png','svg'];
|
|
$needBase64 = isset($options[$c = 'base64'])?boolval($options[$c]):true;
|
|
$isFile = isset($options[$c = 'isFile'])?boolval($options[$c]):true;
|
|
$res = new \stdClass();
|
|
$res->e = 0;
|
|
$res->link = 0;
|
|
if ($res->e == 0) {
|
|
if (count($_FILES)) {
|
|
$c = 0;
|
|
foreach ($_FILES as $f) {
|
|
$uplohost = \Core::$FOLDER;
|
|
$uploaddir = '/upload/images/acc'.UID.'/';
|
|
|
|
$part = explode(".", $f['name']);
|
|
$ext = strtolower($part[count($part) - 1]);
|
|
|
|
$name = time() . '_'.rand(0,10000000).'_' . ($c++);
|
|
$fname = $name . "." . $ext;
|
|
|
|
if(!$isFile) {
|
|
if (!in_array($ext, $ALLOW_IMAGE_UPLOAD)) {
|
|
$res->e = 1;
|
|
$res->m = 'Разрешённые форматы файлов для загрузки: <b>.' . implode('</b>, <b>.', $ALLOW_IMAGE_UPLOAD) . '</b>';
|
|
break;
|
|
}
|
|
}
|
|
$file = $uplohost . $uploaddir . $fname;
|
|
|
|
if (!file_exists($uplohost . $uploaddir)) {
|
|
mkdir($uplohost . $uploaddir, 0755, true);
|
|
}
|
|
if (move_uploaded_file($f['tmp_name'], $file)) {
|
|
|
|
chmod($file,0777);
|
|
//*
|
|
if($needBase64) {
|
|
$fx = fopen($file, "r");
|
|
$s = "";
|
|
while (!feof($fx)) {
|
|
$s .= fgets($fx);
|
|
}
|
|
$s = substr($s, strpos($s, ",") + 1);
|
|
fclose($fx);
|
|
$w = fopen($file, "w");
|
|
fwrite($w, base64_decode($s));
|
|
fclose($w);
|
|
/**/
|
|
}
|
|
$tobase = $uploaddir . $fname;
|
|
$r = static::add([
|
|
static::$NAME => $f['name'],
|
|
static::$URL => $tobase,
|
|
static::$ORIGINAL => $tobase,
|
|
static::$EXT => $ext,
|
|
]);
|
|
$res->id = $r->id;
|
|
$res->link = $tobase;
|
|
$res->name = $f['name'];
|
|
$res->src = \Site::sectionUrl(substr($tobase,1),true);
|
|
//$res->thumb_src= Image::imageResize($tobase,200,200,90,['crop'=>'center']);
|
|
//$res->thumb_src= Image::imageResize($tobase,200,0,90);
|
|
$res->location = $tobase;
|
|
//$res->link = $U->imageResize($tobase,1920);
|
|
} else {
|
|
$res->e = 1;
|
|
$res->m = 'Произошла ошибка при загрузки картинки';
|
|
}
|
|
}
|
|
} else {
|
|
$res->e = 1;
|
|
$res->m = 'Прикрепите, пожалуйста, файл.';
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
|
|
static function getForm($values = [], $options = []) :array {
|
|
return [
|
|
self::getCommonFormInputs(),
|
|
[
|
|
Form::VIEW=>Form::ROW,
|
|
Form::COLS => [
|
|
[
|
|
Form::VIEW =>Form::INPUT,
|
|
Form::COL_CLASSES =>'col-12',
|
|
Form::KEY_VALUE =>self::$NAME,
|
|
Form::TYPE =>eInputType::TEXT,
|
|
Form::INPUT_OPT => [
|
|
Input::PLACEHOLDER => $c = V::get(Vars::$NAME),
|
|
Input::LABEL => $c,
|
|
],
|
|
],
|
|
]
|
|
],
|
|
self::getCommonFormSubmits($values),
|
|
];
|
|
}
|
|
|
|
static function columnInfo() : array {
|
|
return [
|
|
new C([
|
|
C::VAR_NAME =>&self::$ID,
|
|
C::TYPE =>eColumnType::INT,
|
|
C::AUTO_INCREMENT =>true,
|
|
C::PRIMARY =>true,
|
|
C::DEFAULT =>0,
|
|
C::HIDDEN =>true,
|
|
C::TH =>V::get(Vars::$ID),
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$NAME,
|
|
C::TYPE => eColumnType::VARCHAR,
|
|
C::LENGTH => 1023,
|
|
C::TH => V::get(Vars::$NAME),
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$URL,
|
|
C::TYPE => eColumnType::VARCHAR,
|
|
C::LENGTH => 255,
|
|
C::TH => V::get(Vars::$URL),
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$ORIGINAL,
|
|
C::TYPE => eColumnType::VARCHAR,
|
|
C::LENGTH => 255,
|
|
C::TH => V::get(Vars::$ORIGINAL),
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$EXT,
|
|
C::TYPE => eColumnType::VARCHAR,
|
|
C::LENGTH => 15,
|
|
C::TH => V::get(Vars::$EXT),
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$PROFILE,
|
|
C::TYPE => eColumnType::INT,
|
|
C::LENGTH => 11,
|
|
//C::TH => V::get(Vars::$PROFILE),
|
|
C::DEFAULT => intval(PID),
|
|
c::HIDDEN=>true,
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$CLOUD,
|
|
C::TYPE => eColumnType::TEXT,
|
|
C::DEFAULT => '',
|
|
c::HIDDEN => true,
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$ACCOUNT,
|
|
C::TYPE => eColumnType::INT,
|
|
C::LENGTH => 11,
|
|
C::TH => V::get(Vars::$ACCOUNT),
|
|
C::DEFAULT => intval(UID),
|
|
c::HIDDEN=>true,
|
|
c::FUNC_VALUE => function($v){
|
|
if($id = $v[self::$ACCOUNT]) {
|
|
return \Account::getName( \Account::getByID($id) );
|
|
} else return '-';
|
|
},
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$CREATED,
|
|
C::TYPE => eColumnType::INT,
|
|
C::LENGTH => 11,
|
|
C::TH => V::get(Vars::$CREATED),
|
|
C::DEFAULT => time(),
|
|
c::FUNC_VALUE => function($v){return self::formatDate($v[self::$CREATED]);},
|
|
c::HIDDEN=>true,
|
|
]),
|
|
new C([
|
|
C::VAR_NAME => &self::$UPDATED,
|
|
C::TYPE => eColumnType::INT,
|
|
C::DEFAULT => time(),
|
|
C::TH => V::get(Vars::$UPDATED),
|
|
c::FUNC_VALUE => function($v){return self::formatDate($v[self::$UPDATED]);},
|
|
c::HIDDEN=>true,
|
|
]),
|
|
];
|
|
}
|
|
|
|
|
|
|
|
public static function uploadToYandexStorage($image_id,$path,$key,$secret,$bucket){
|
|
$image_id = intval($image_id);
|
|
$image = self::getByID($image_id);
|
|
|
|
if(!$image) return false;
|
|
if(!$_SERVER['HTTPS']) return false;
|
|
|
|
|
|
$file = \CORE::$FOLDER.$path;
|
|
$sharedConfig = [
|
|
'credentials' => [
|
|
'key' => $key,
|
|
'secret' => $secret,
|
|
],
|
|
//'region' => 'us-east-1',
|
|
'region' => 'ru-central1',
|
|
'endpoint' => 'https://storage.yandexcloud.net',
|
|
'version' => 'latest',
|
|
];
|
|
$sdk = new \Aws\Sdk($sharedConfig);
|
|
$s3Client = $sdk->createS3();
|
|
|
|
|
|
|
|
$upload_path = substr($path,1);
|
|
|
|
|
|
//ManyQuests::sendToModerate($file."\n".(file_exists($file)?1:0)."\n".(file_get_contents($file)?1:0));
|
|
//exit;
|
|
try {
|
|
/** @var \Aws\Result $r */
|
|
$x = file_get_contents($file);
|
|
if($x) {
|
|
$r = $s3Client->upload($bucket, $upload_path, file_get_contents($file));
|
|
$cloud_url = $r->toArray()['ObjectURL'];
|
|
|
|
$cloud = json_decode($image[Image::$CLOUD],true);
|
|
if(!$cloud) $cloud = [];
|
|
$cloud[$path] = $cloud_url;
|
|
|
|
Image::updateById($image_id,[self::$CLOUD=>je($cloud)]);
|
|
|
|
//echo '<pre>';
|
|
//echo var_dump($r->data);
|
|
//echo '</pre>';
|
|
|
|
/*
|
|
$result = $s3Client->getObject([
|
|
'Bucket' =>$bucket,
|
|
'Key' => $upload_path
|
|
]);
|
|
//echo $result['Body'];
|
|
/**/
|
|
return $cloud_url;
|
|
} else {
|
|
return $path;
|
|
//return "https://".$bucket.".storage.yandexcloud.net/".$upload_path;
|
|
}
|
|
} catch (\Exception $exception){
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
static function correctImageOrientation($filename) {
|
|
$res = $filename;
|
|
if (function_exists('exif_read_data')) {
|
|
|
|
$f = explode(".",$filename);
|
|
$ext = array_pop($f);
|
|
$isPng = ($ext=="png");
|
|
$filenamePng = "";
|
|
if($isPng){
|
|
$f2 = $f;
|
|
$f[] = $ext;
|
|
$f2[] = "jpg";
|
|
$f1 = implode(".",$f);
|
|
$f2 = implode(".",$f2);
|
|
copy($f1,$f2);
|
|
$filenamePng = $f1;
|
|
$filename = $f2;
|
|
}
|
|
$exif = \exif_read_data($filename);
|
|
|
|
if($exif && isset($exif['Orientation'])) {
|
|
$orientation = $exif['Orientation'];
|
|
if($orientation != 1){
|
|
$img = imagecreatefromjpeg($filename);
|
|
$deg = 0;
|
|
switch ($orientation) {
|
|
case 3:
|
|
$deg = 180;
|
|
break;
|
|
case 6:
|
|
$deg = 270;
|
|
break;
|
|
case 8:
|
|
$deg = 90;
|
|
break;
|
|
}
|
|
if ($deg) {
|
|
$img = imagerotate($img, $deg, 0);
|
|
$res = $filename;
|
|
}
|
|
// then rewrite the rotated image back to the disk as $filename
|
|
if($isPng) {
|
|
imagepng($img, $filenamePng);
|
|
} else {
|
|
imagejpeg($img, $filename, 100);
|
|
}
|
|
} // if there is some rotation necessary
|
|
} // if have the exif orientation info
|
|
} // if function exists
|
|
return $res;
|
|
}
|
|
|
|
|
|
static public function imageResize($img,$width=300,$height=0,$o=100,$options=[]){
|
|
// icc profile не поддерживается в php
|
|
|
|
|
|
$res = '';
|
|
$crop = isset($options[$c = 'crop'])?$options[$c]:false;
|
|
$cloud = isset($options[$c = 'cloud'])?$options[$c]:null;
|
|
$isCloud = boolval($cloud);
|
|
$isWantCloud = boolval($cloud);
|
|
$path = '';
|
|
$cloud_full = '';
|
|
if($img) {
|
|
$isTest = $_GET['test'];
|
|
|
|
$IMAGE = null;
|
|
if(is_array($img)){
|
|
$IMAGE = $img;
|
|
$img = $IMAGE[Image::$URL];
|
|
}
|
|
if($isCloud and !$IMAGE){
|
|
$isCloud = false;
|
|
}
|
|
if(substr($img,0,4)=='http'){
|
|
//$cloud_full = $img;
|
|
$isCloud = false;
|
|
}
|
|
$isHTTPS = boolval($_SERVER['HTTPS']);
|
|
if(!$isHTTPS) {
|
|
$isCloud = false;
|
|
}
|
|
|
|
$p = explode(".", $img);
|
|
$ext = strtolower(array_pop($p));
|
|
if($ext=="jpeg") $ext = "jpg";
|
|
/*
|
|
if(in_array($ext,['jpg','jpeg'])) {
|
|
$ext = 'webp';
|
|
}
|
|
/**/
|
|
|
|
|
|
$pathName = implode(".",$p).$width.'_'.$o.'_'.$height.'_'.($crop?1:0);
|
|
|
|
$path = $pathName.".".$ext;
|
|
$pathWeb = $pathName.".".$ext;
|
|
$pathFile = $pathName."_".$ext.".".$ext;
|
|
$pathWebP = $pathName."_webp.webp";
|
|
|
|
$isWebP = function_exists('imagewebp');
|
|
|
|
$root = \Core::$FOLDER;
|
|
$full_path = $root.$path;
|
|
$file_path = $root.$IMAGE[Image::$URL];
|
|
|
|
|
|
if($ext=='svg') return $img;
|
|
|
|
$isFileExists = file_exists($full_path);
|
|
|
|
|
|
if($isCloud){
|
|
$cloud_full = $cloud['url'].$IMAGE[Image::$URL];
|
|
|
|
$cloud_paths = json_decode($IMAGE[Image::$CLOUD],true);
|
|
if(!$cloud_paths) $cloud_paths = [];
|
|
|
|
if($cloud_paths[$pathFile]){
|
|
return $cloud_paths[$pathFile];
|
|
} else {
|
|
if($isFileExists){
|
|
$upload_url = self::uploadToYandexStorage($IMAGE[Image::$ID],$pathFile,$cloud['key'],$cloud['secret'],$cloud['bucket']);
|
|
if($upload_url==false){
|
|
$isFileExists = false;
|
|
} else {
|
|
unlink($file_path);
|
|
return $upload_url;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$isCloudFileExists = false;
|
|
if(!$isFileExists and $isCloud){
|
|
$headers= get_headers($cloud_full, 1);
|
|
$isCloudFileExists = (strpos($headers[0], "200") !== false);
|
|
}
|
|
// Это нужно для того, чтобы удалять картинки по прямому пути и отдавать их по WebP
|
|
if(file_exists($root.$path)){
|
|
unlink($root.$path);
|
|
}
|
|
if( file_exists($root.$pathFile) and ($isWebP ? file_exists($root.$pathWebP) : true )) {
|
|
$res = $pathWeb;
|
|
} else if (file_exists($root.$img) || $isCloudFileExists){
|
|
chmod($root.$img,0755);
|
|
$correction = self::correctImageOrientation($root.$img);
|
|
$isRotated = $root.$img!=$correction;
|
|
|
|
if($isRotated) {
|
|
$ext = "jpg";
|
|
$path = $pathName . "." . $ext;
|
|
$pathWeb = $pathName . "." . $ext;
|
|
$pathFile = $pathName . "_" . $ext . "." . $ext;
|
|
}
|
|
$name = basename($pathFile);
|
|
$nameWebP = basename($pathWebP);
|
|
|
|
//$flink = "http://".$_SERVER['HTTP_HOST']."/".$img;
|
|
$flink = \Core::$FOLDER."/".$img;
|
|
if($isCloudFileExists) $flink = $cloud_full;
|
|
|
|
$x = \getimagesize($flink);
|
|
|
|
|
|
if($x[0]>10000 || $x[1]>10000) {
|
|
return '/upload/test/acc.png';
|
|
}
|
|
//$flink = $root.$img;
|
|
$isPng = false;
|
|
|
|
switch ($ext) {
|
|
case 'jpg':
|
|
case 'jpeg':
|
|
case 'webp':
|
|
try {
|
|
//$im1 = imagecreatefromjpeg($flink . "." . $ext);
|
|
$im1 = \imagecreatefromjpeg($flink );
|
|
/*
|
|
imagejpeg($im1, __DIR__.'/../../../test.jpg', 100);
|
|
return ;
|
|
|
|
$w = imagesx($im1);
|
|
$h = imagesy($im1);
|
|
$im = imagecreatetruecolor($w, $h);
|
|
//imagecopyresampled($im, $im1, 0, 0, 0, 0, $w, $h, $w, $h);
|
|
imagecopy($im, $im1, 0, 0, 0, 0, $w, $h);
|
|
imagejpeg($im, __DIR__.'/../../../test.jpg', 100);
|
|
*/
|
|
if(!$im1){
|
|
$im1 = \imagecreatefrompng($flink );
|
|
}
|
|
if(!$im1){
|
|
$im1 = \imagecreatefromwebp($flink);
|
|
}
|
|
} catch (\Exception $e){
|
|
echo '<pre>';
|
|
echo var_dump($e);
|
|
echo '</pre>';
|
|
}
|
|
break;
|
|
case 'gif':
|
|
case 'giff':
|
|
//$im1 = imagecreatefromgif($flink . "." . $ext);
|
|
$im1 = \imagecreatefromgif($flink );
|
|
break;
|
|
case 'png':
|
|
$isPng = true;
|
|
//$im1 = imagecreatefrompng($flink . "." . $ext);
|
|
$im1 = \imagecreatefrompng($flink );
|
|
|
|
break;
|
|
}
|
|
if ($im1) {
|
|
$w = \imagesx($im1);
|
|
$h = \imagesy($im1);
|
|
if(!$width && !$height){
|
|
$width = $w;
|
|
}
|
|
$dist_x = 0;
|
|
$dist_y = 0;
|
|
$src_x = 0;
|
|
$src_y = 0;
|
|
$src_w = $w;
|
|
$src_h = $h;
|
|
|
|
$pw = $width/$w;
|
|
$ph = $height?$height/$h:0;
|
|
|
|
$nw1 =$nw = $width;
|
|
if($width and !$height) {
|
|
$nh1 = $nh = floor($nw / $w * $h);
|
|
} else if($height and $width){
|
|
$nh1 = $nh = $height;
|
|
$nw1 = $nw = floor($nh / $h * $w);
|
|
if($nw>$width) {
|
|
$nw = $width;
|
|
$nh = floor($nw / $w * $h);
|
|
} else {
|
|
$nw1 = $width;
|
|
$nh1 = floor($nw1 / $w * $h);
|
|
}
|
|
} else if ($height && !$width) {
|
|
$nh1 = $nh = $height;
|
|
$nw1 = $nw = floor($nh / $h * $w);
|
|
}
|
|
$minSize = max($width,$height);
|
|
|
|
if($crop){
|
|
switch ($crop){
|
|
case 'center':
|
|
if($width and $height) {
|
|
$nw = $width;
|
|
$nh = $height;
|
|
$src_w = $nw/$nw1*$w;
|
|
$src_h = $nh/$nh1*$h;
|
|
$src_x = ($w-$src_w)/2;
|
|
$src_y = ($h-$src_h)/2;
|
|
}
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$im = \imagecreatetruecolor($nw, $nh);
|
|
if($isPng){
|
|
//imagealphablending($im1, true);
|
|
\imagealphablending($im, false);
|
|
\imagesavealpha ( $im ,true);
|
|
}
|
|
if(!$isPng) {
|
|
$white = \imagecolorallocate($im, 255, 255, 255);
|
|
\imagefilledrectangle($im, 0, 0, $nw, $nh, $white);
|
|
}
|
|
|
|
\imagecopyresampled($im, $im1, $dist_x, $dist_y, $src_x, $src_y, $nw, $nh, $src_w, $src_h);
|
|
$to = $root.'/'.dirname($path) .'/'. $name;
|
|
$toWebP = $root.'/'.dirname($path) .'/'. $nameWebP;
|
|
//echo $to;
|
|
if($isPng){
|
|
//imagesavealpha ( $im1 ,true);
|
|
\imagesavealpha ( $im ,true);
|
|
}
|
|
|
|
$dir = dirname($to);
|
|
if(!file_exists($dir)){
|
|
mkdir($dir,0755,true);
|
|
}
|
|
|
|
|
|
if($isPng){
|
|
if(file_exists($to)){
|
|
unlink($to);
|
|
}
|
|
if (\imagepng($im, $to)){
|
|
$res = $pathWeb;
|
|
} else $res = $img;
|
|
} else {
|
|
if(file_exists($to)){
|
|
unlink($to);
|
|
}
|
|
|
|
if ($x = \imagejpeg($im, $to, $o)) {
|
|
$res = $pathWeb;
|
|
} else $res = $img;
|
|
|
|
|
|
}
|
|
|
|
if($isWebP){
|
|
if(file_exists($toWebP)){
|
|
unlink($toWebP);
|
|
}
|
|
if (\imagewebp($im, $toWebP, $o)) {
|
|
$res = $pathWeb;
|
|
} else $res = $img;
|
|
}
|
|
chmod($to,0755);
|
|
chmod($toWebP,0755);
|
|
}
|
|
/**/
|
|
|
|
|
|
} else $res=$img;
|
|
|
|
if($isCloud and $res){
|
|
self::uploadToYandexStorage($IMAGE[Image::$ID],$IMAGE[Image::$URL],$cloud['key'],$cloud['secret'],$cloud['bucket']);
|
|
self::uploadToYandexStorage($IMAGE[Image::$ID],$pathWebP,$cloud['key'],$cloud['secret'],$cloud['bucket']);
|
|
$upload_url = self::uploadToYandexStorage($IMAGE[Image::$ID],$pathFile,$cloud['key'],$cloud['secret'],$cloud['bucket']);
|
|
|
|
if($upload_url){
|
|
$res = $upload_url;
|
|
unlink($file_path);
|
|
unlink($to);
|
|
unlink($toWebP);
|
|
}
|
|
}
|
|
}
|
|
if($isWantCloud && !$isCloud){
|
|
return \Site::sectionUrl(substr($res,1),true);
|
|
} else {
|
|
return $res;
|
|
}
|
|
}
|
|
static public function imageResize2($img='',$width=300,$height=0,$o=100,$options=[]){
|
|
$res = '';
|
|
$crop = isset($options[$c = 'crop'])?$options[$c]:false;
|
|
if($img) {
|
|
$p = explode(".", $img);
|
|
$ext = array_pop($p);
|
|
$path = implode(".",$p).$width.'_'.$o.'_'.$height.'_'.($crop?1:0).".".$ext;
|
|
$root = \Core::$FOLDER;
|
|
|
|
if(file_exists($root.$path)) {
|
|
$res = $path;
|
|
} else if (file_exists($root.$img)){
|
|
$name = basename($path);
|
|
$flink = "http://".$_SERVER['HTTP_HOST']."/".$img;
|
|
$flink = \Core::$FOLDER."/".$img;
|
|
|
|
$x = getimagesize($flink);
|
|
|
|
|
|
if($x[0]>6000 || $x[1]>6000) {
|
|
return '/upload/test/acc.png';
|
|
}
|
|
$flink = $root.$img;
|
|
$isPng = false;
|
|
|
|
$im1 = imagecreatefromstring(file_get_contents($flink));
|
|
|
|
if ($im1) {
|
|
$w = imagesx($im1);
|
|
$h = imagesy($im1);
|
|
$dist_x = 0;
|
|
$dist_y = 0;
|
|
$src_x = 0;
|
|
$src_y = 0;
|
|
$src_w = $w;
|
|
$src_h = $h;
|
|
|
|
$pw = $width/$w;
|
|
$ph = $height?$height/$h:0;
|
|
|
|
$nw1 =$nw = $width;
|
|
if($width and !$height) {
|
|
$nh1 = $nh = floor($nw / $w * $h);
|
|
} else if($height and $width){
|
|
$nh1 = $nh = $height;
|
|
$nw1 = $nw = floor($nh / $h * $w);
|
|
if($nw>$width) {
|
|
$nw = $width;
|
|
$nh = floor($nw / $w * $h);
|
|
} else {
|
|
$nw1 = $width;
|
|
$nh1 = floor($nw1 / $w * $h);
|
|
}
|
|
}
|
|
$minSize = max($width,$height);
|
|
if($crop){
|
|
switch ($crop){
|
|
case 'center':
|
|
if($width and $height) {
|
|
$nw = $width;
|
|
$nh = $height;
|
|
$src_w = $nw/$nw1*$w;
|
|
$src_h = $nh/$nh1*$h;
|
|
$src_x = ($w-$src_w)/2;
|
|
$src_y = ($h-$src_h)/2;
|
|
}
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
$im = imagecreatetruecolor($nw, $nh);
|
|
if($isPng){
|
|
//imagealphablending($im1, true);
|
|
imagealphablending($im, false);
|
|
imagesavealpha ( $im ,true);
|
|
}
|
|
if(!$isPng) {
|
|
$white = imagecolorallocate($im, 255, 255, 255);
|
|
imagefilledrectangle($im, 0, 0, $nw, $nh, $white);
|
|
}
|
|
|
|
imagecopyresampled($im, $im1, $dist_x, $dist_y, $src_x, $src_y, $nw, $nh, $src_w, $src_h);
|
|
$to = $root.'/'.dirname($path) .'/'. $name;
|
|
//echo $to;
|
|
if($isPng){
|
|
//imagesavealpha ( $im1 ,true);
|
|
imagesavealpha ( $im ,true);
|
|
}
|
|
|
|
|
|
if($isPng){
|
|
if(file_exists($to)){
|
|
unlink($to);
|
|
}
|
|
if (imagepng($im, $to)){
|
|
$res = $path;
|
|
} else $res = $img;
|
|
} else {
|
|
if(file_exists($to)){
|
|
unlink($to);
|
|
}
|
|
if (imagejpeg($im, $to, $o)) {
|
|
$res = $path;
|
|
} else $res = $img;
|
|
/**/
|
|
}
|
|
}
|
|
/**/
|
|
|
|
|
|
} else $res=$img;
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
|
|
|
|
static function autologo($image_url,$logo_url,$options = []) {
|
|
$res = $image_url;
|
|
if(!$image_url) return'';
|
|
$root = \Core::$FOLDER;
|
|
$image_path = explode(".",$image_url);
|
|
$ext = array_pop($image_path);
|
|
if(!file_exists($root.$image_url)){
|
|
$image_url = implode(".",$image_path)."_".$ext.".".$ext;
|
|
}
|
|
|
|
|
|
$ext = 'jpg';
|
|
$pathName = str_replace($root,'',dirname($root . $image_url)) . '/autologo/'.md5($image_url . $logo_url . je($options));
|
|
|
|
/*
|
|
$correction = self::correctImageOrientation($root.$img);
|
|
$isRotated = $root.$img!=$correction;
|
|
|
|
if($isRotated) {
|
|
$ext = "jpg";
|
|
$path = $pathName . "." . $ext;
|
|
$pathWeb = $pathName . "." . $ext;
|
|
$pathFile = $pathName . "_" . $ext . "." . $ext;
|
|
}
|
|
*/
|
|
|
|
$path = $pathName.".".$ext;
|
|
$pathWeb = $pathName.".".$ext;
|
|
$pathFile = $pathName."_".$ext.".".$ext;
|
|
$pathWebP = $pathName."_webp.webp";
|
|
|
|
$isWebP = function_exists('imagewebp');
|
|
|
|
|
|
|
|
if(file_exists($root.$path)){
|
|
unlink($root.$path);
|
|
}
|
|
|
|
|
|
if( file_exists($root.$pathFile) and ($isWebP ? file_exists($root.$pathWebP) : true ) ) {
|
|
return $pathWeb;
|
|
}
|
|
|
|
/*
|
|
echo '<pre>';
|
|
echo var_dump($image_url,$root,$path,$result_url);
|
|
echo '</pre>';
|
|
/**/
|
|
|
|
$img = imagecreatefromstring(file_get_contents($root . $image_url));
|
|
$logo = imagecreatefromstring(file_get_contents($root . $logo_url));
|
|
if (!($img && $logo)) return $image_url;
|
|
|
|
$position = intval($options[eAutologoOptions::POSITION]);
|
|
|
|
$img_width = imagesx($img);
|
|
$img_height = imagesy($img);
|
|
$logo_width = imagesx($logo);
|
|
$logo_height = imagesy($logo);
|
|
|
|
$xoffset = cfloatval($options[eAutologoOptions::XOFFSET]);
|
|
$xoffset_type = intval($options[eAutologoOptions::XOFFSET_TYPE]);
|
|
$yoffset = cfloatval($options[eAutologoOptions::YOFFSET]);
|
|
$yoffset_type = intval($options[eAutologoOptions::YOFFSET_TYPE]);
|
|
$width = cfloatval($options[eAutologoOptions::WIDTH]);
|
|
$width_type = intval($options[eAutologoOptions::WIDTH_TYPE]);
|
|
$quality = intval($options[eAutologoOptions::QUALITY]);
|
|
|
|
|
|
switch ($xoffset_type) {
|
|
case eAutologoType::PERCENT:
|
|
$xoffset = $img_width * $xoffset / 100;
|
|
break;
|
|
}
|
|
switch ($yoffset_type) {
|
|
case eAutologoType::PERCENT:
|
|
$yoffset = $img_height * $yoffset / 100;
|
|
break;
|
|
}
|
|
switch ($width_type) {
|
|
case eAutologoType::PERCENT:
|
|
$width = $img_width * $width / 100;
|
|
break;
|
|
}
|
|
|
|
$height = $width / $logo_width * $logo_height;
|
|
|
|
$left = $xoffset;
|
|
$top = $yoffset;
|
|
// Вычисляем координаты стартовой X позиции
|
|
switch ($position) {
|
|
case eAutologoPosition::TOP_LEFT:
|
|
case eAutologoPosition::CENTER_LEFT:
|
|
case eAutologoPosition::BOTTOM_LEFT:
|
|
$left = $xoffset;
|
|
break;
|
|
case eAutologoPosition::TOP_RIGHT:
|
|
case eAutologoPosition::CENTER_RIGHT:
|
|
case eAutologoPosition::BOTTOM_RIGHT:
|
|
$left = $img_width - $width - $xoffset;
|
|
break;
|
|
case eAutologoPosition::TOP_CENTER:
|
|
case eAutologoPosition::CENTER:
|
|
case eAutologoPosition::BOTTOM_CENTER:
|
|
$left = $xoffset + ($img_width - $width) / 2;
|
|
break;
|
|
}
|
|
// Вычисляем координаты стартовой Y позиции
|
|
switch ($position) {
|
|
case eAutologoPosition::TOP_LEFT:
|
|
case eAutologoPosition::TOP_CENTER:
|
|
case eAutologoPosition::TOP_RIGHT:
|
|
$top = $yoffset;
|
|
break;
|
|
case eAutologoPosition::CENTER_LEFT:
|
|
case eAutologoPosition::CENTER:
|
|
case eAutologoPosition::CENTER_RIGHT:
|
|
$top = $yoffset + ($img_height - $height) / 2;;
|
|
break;
|
|
case eAutologoPosition::BOTTOM_LEFT:
|
|
case eAutologoPosition::BOTTOM_CENTER:
|
|
case eAutologoPosition::BOTTOM_RIGHT:
|
|
$top = $img_height - $height - $yoffset;
|
|
break;
|
|
}
|
|
|
|
//$im3 = imagecreatetruecolor($_POST['w'],$_POST['h']);
|
|
$autologo = imagecreatetruecolor($img_width, $img_height);
|
|
$white = imagecolorallocate($autologo, 255, 255, 255);
|
|
imagefill($autologo, 0, 0, $white);
|
|
imagecopy($autologo, $img, 0, 0, 0, 0, $img_width, $img_height);
|
|
$x = imagecopyresampled($autologo, $logo, $left, $top, 0, 0, $width, $height, $logo_width, $logo_height);
|
|
|
|
|
|
$name = basename($pathFile);
|
|
$nameWebP = basename($pathWebP);
|
|
|
|
$to = $root.$pathFile;
|
|
$toWebP = $root.$pathWebP;
|
|
|
|
|
|
$result_dir = \Core::$FOLDER.dirname($pathWeb);
|
|
if (!file_exists($result_dir)) {
|
|
mkdir($result_dir, 0755, true);
|
|
}
|
|
|
|
if (imagejpeg($autologo, $to, $quality)) {
|
|
$res = $pathWeb;
|
|
}
|
|
|
|
if($isWebP){
|
|
if(file_exists($toWebP)){
|
|
unlink($toWebP);
|
|
}
|
|
if (imagewebp($autologo, $toWebP, $quality)) {
|
|
$res = $pathWeb;
|
|
}
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
}
|
|
new Image();
|
|
|
|
|