125 lines
4.3 KiB
PHP

<?php
class attr_image extends attr {
function getDBType() {
return "int(12)";
}
function render_input() {
$html = '';
if ( $this->getCurVal() ) {
$html .= '<a href="img.php?id='.$this->getCurVal().'" data-fancybox><img class="mx-1" src="img.php?id='.$this->getCurVal().'" style="height: 30px; vertical-align: middle;"></a>';
}
$html .= '<label class="btn btn-gray"><input class="d-none" type="file" id="img_' . $this->attr_name . '" name="' . $this->attr_name . '"> Выбрать файл</label>';
$html .= '<input class="input" type="hidden" name="' . $this->attr_name . '" value="'.$this->getCurVal().'">';
if ( $this->getCurVal() ) {
$html .= '
<div class="custom-control custom-checkbox">
<input class="checkbox custom-control-input" type="checkbox" id="' . $this->attr_name . '_del" name="' . $this->attr_name . '_del" value="1">
<label for="' . $this->attr_name . '_del" class="custom-control-label"> Удалить</label>
</div>';
}
return $html;
}
function lPost() {
if ( substr( $this->attr_name, -8 ) == '_preview' ) {
return;
}
if ($_POST[$this->attr_name . '_del']) {
$this->obj->set( $this->attr_name, 0 );
}
if ($_POST[$this->attr_name]) {
$this->obj->set( $this->attr_name, $_POST[$this->attr_name] );
}
$file = $_FILES[$this->attr_name];
if (isset($file['error']) && !$file['error']) {
require_once (_CORE_DIR . 'image.transform.core.php');
$tmpObj = new cobject( '_sys_files' );
$file = $_FILES[$this->attr_name];
$fileContent = file_get_contents($file['tmp_name']);
// Имя файла основано на содержимом
$fname = md5($fileContent);
$fpath = substr($fname,0,2);
$fpath2 = substr($fname,2,2);
@mkdir( _FILES_DIR_ . $fpath . '/' . $fpath2, 0777, true );
$fname = $fpath .'/' . $fpath2 . '/' . $fname;
$toSize = $this->obj->attrs[$this->attr_name]['templ'];
if (!file_exists(_FILES_DIR_ . $fname .'_'. $toSize) ) {
$im = imagecreatefromstring($fileContent);
// Сохраняем картинку
$res = imagejpeg($im, _FILES_DIR_ . $fname.'_' , 95 );
//Ресайзим если нужно
if ($toSize != "") {
$p = explode('x', $toSize);
if (count($p) == 2) {
$tr = new imageTransform(_FILES_DIR_ . $fname .'_', 'image/jpeg');
$tr->resize($p[0], $p[1], true);
$tr->save(_FILES_DIR_ .$fname .'_'. $toSize);
}
}
}
// Сохраняем в базу
$fobj = new cobject('_sys_files');
$fobj->set('filename', $fname . '_'. $toSize);
$fobj->set('ext','image/jpeg');
$fobj->set('realname', $file['name']);
$fobj->save();
// Сохраняем в объект
$this->obj->set( $this->attr_name, $fobj->id );
// Автопревью
if ( isset($this->obj->attrs[$this->attr_name.'_preview']) && $this->obj->attrs[$this->attr_name.'_preview']['templ'] ) {
$pre_size = $this->obj->attrs[$this->attr_name.'_preview']['templ'];
if ($pre_size != "" ) {
// Сохраняем в базу
$fobj = new cobject('_sys_files');
$fobj->set('filename', $fname . '_' . $pre_size );
$fobj->set('ext','image/jpeg');
$fobj->set('realname', 'preview_'.$file['name']);
$fobj->save();
$this->obj->set( $this->attr_name.'_preview', $fobj->id );
copy(_FILES_DIR_.$fname . '_'. $toSize, _FILES_DIR_.$fname.'_' . $pre_size );
$p = explode('x',$pre_size);
$tr = new imageTransform(_FILES_DIR_ . $fname.'_' . $pre_size, 'image/jpeg');
$tr->resize($p[0], $p[1], true);
$tr->save(_FILES_DIR_ .$fname.'_' . $pre_size );
}
}
}
}
function render_text( $crop = false ) {
if ($crop) {
$val = '<a href="img.php?id='.$this->getCurVal().'"><img src="img.php?id='.$this->getCurVal().'" height="48"></a>';
} else {
$val = $this->getCurVal();
$val = getImagePath($val);
}
return $val;
}
}
?>