2namespace dbx\dbxContent_admin;
4Class dbxContent_images {
6 private function convert_image_to_webp($file) {
7 if (!extension_loaded(
'gd') || !function_exists(
'imagewebp'))
return '';
8 if ($file ===
'' || !is_file($file) || !is_readable($file))
return '';
10 $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
11 if (!in_array($ext, array(
'jpg',
'jpeg',
'png'),
true))
return '';
13 $image = $ext ===
'png'
14 ? (function_exists(
'imagecreatefrompng') ? @imagecreatefrompng($file) : false)
15 : (function_exists(
'imagecreatefromjpeg') ? @imagecreatefromjpeg($file) : false);
16 if (!$image)
return '';
18 imagepalettetotruecolor($image);
19 imagealphablending($image,
true);
20 imagesavealpha($image,
true);
22 $webp = preg_replace(
'/\.[^.]+$/',
'.webp', $file);
23 if ($webp === $file || $webp ===
null) {
28 $ok = @imagewebp($image, $webp, 86);
30 if (!$ok || !is_file($webp))
return '';
33 return basename($webp);
36 public function check_data($iid,$data) {
37 if (is_array($data)) {
39 $img_key=
'upload'.$iid;
43 if (isset($data[$img_key])) $img_src=$data[$img_key];
44 if (!$img_src) $img_src=
'content.gif';
46 $path_img_ext=$path.$img_src;
47 if (!file_exists($path_img_ext)) $img_src=
'content.gif';
49 $data[$img_key]=$img_src;
55 public function run() {
56 $content=
''; $ok=
false;
58 $iid=dbx()->get_modul_var(
'iid',1,
'int');
60 $lng=dbx()->get_system_var(
'dbx_lng',
'de');
62 $rid=dbx()->get_modul_var(
'rid',0);
63 $obs=dbx()->get_modul_var(
'dbx_obs_fld');
64 $obv=dbx()->get_modul_var(
'dbx_obs_val');
67 if ($obs && $obv) $rid=$obv;
71 $oForm = dbx()->get_system_obj(
'dbxForm');
72 $db = dbx()->get_system_obj(
'dbxDB');
76 $url = dbx()->get_base_url().
'files/dbxContent/img/';
78 $target= dbx()->get_query_var(
'dbx_target');
80 $tab_content = dbx_lng_name(
'content', $lng);
81 $img_key =
'upload'.$iid;
82 $obs_key =
'obs_'.$img_key;
85 $data=
$db->select1($tab_content,$rid);
86 if (!is_array($data)) {
return "Content ID ($rid) nicht gefunden"; }
89 $oForm->init(
'dbxContent_image_'.$iid,
'form-images');
91 $oForm->_action=
"?dbx_modul=dbxContent_admin&dbx_run1=content&dbx_run2=images&rid=$rid&iid=$iid";
92 if (!$rid) $oForm->_tpl=
'form-images_wait';
95 $oForm->add_fld(
'img_alt_'.$iid,
'text-label' ,rules:
'words',label:
'Kurtz Beschreibung');
96 $oForm->add_fld(
'img_des_'.$iid,
'textarea-label',rules:
'words',label:
'Beschreibung');
100 $submit=$oForm->submit();
102 $img_file=dbx()->get_modul_var($obs_key,
'no-img.png',
'filename-img');
104 dbx()->debug(
"##GEt val=($obs_key) for=($img_key) val=($img_file)");
107 $oForm->_post[$img_key]=$img_file;
108 $ok=$oForm->save_post($tab_content,$rid);
109 if ($ok) $oForm->_msg_success=
"Bild: ($img_file)";
112 $oForm->add_fld_error($obs_key,
'x');
113 $imgag_file=dbx()->get_request_var($obs_key,0,
'*');
114 $oForm->_msg_error=
"Kein Bild: ($img_file)";
119 if (!empty($_FILES) ) {
120 $oUpload=dbx()->get_system_obj(
'dbxUpload');
121 $oUpload->upload($_FILES[
'upload_file']);
122 $oUpload->allowed = array(
'image/*');
123 $oUpload->file_overwrite =
true;
125 $oUpload->image_resize =
true;
126 $oUpload->image_ratio_crop =
true;
127 $oUpload->image_x = 1280;
128 $oUpload->image_y = 800;
131 $oUpload->process($path);
132 if ($oUpload->processed) {
133 $img=$oUpload->file_dst_pathname;
134 $uploaded_name = $oUpload->file_dst_name;
135 $webp_name = $this->convert_image_to_webp($img);
136 if ($webp_name !==
'') {
137 $uploaded_name = $webp_name;
139 $field_values[
'id']=$rid;
140 $field_values[$img_key]=$uploaded_name;
142 $ok=$oForm->save_post($tab_content,$rid,$field_values);
146 $data[$img_key]=$uploaded_name;
147 $url_img_ext=$url.$uploaded_name;
148 $img_src =$uploaded_name;
149 $oForm->set_msg_ok(
"Bild Datei erfolgreich hochgeladen.");
152 $content=
'error : ' . $oUpload->error;
153 $oForm->set_msg_error(
"Bild Datei nicht hochgeladen.");
161 $oForm->_msg_info=
'Bild ('.$iid.
')';
165 $oForm->_data=$this->check_data($iid,$oForm->_data);
166 $img_src=$oForm->_data[$img_key];
167 $url_img_ext=$url.$img_src;
171 $modal1[
'title'] =
'Bild Auswahl';
172 $modal1[
'label'] =
'Filebrauser';
173 $modal1[
'dbx_get'] =
'?dbx_modul=dbxContent_admin&dbx_run1=content&dbx_run2=ibrowser&dbx_caller='.$obs_key;
177 $img_data[
'dbx_get'] =
'?dbx_modul=dbxContent_admin&dbx_run1=set_observer';
178 $img_data[
'dbx_target'] = $img_key.
'_{i}';
179 $img_data[
'id'] =
'selectimg_{i}';
180 $img_data[
'name'] = $img_key;
181 $img_data[
'src'] = $url_img_ext;
182 $img_data[
'class'] =
'dbxImg';
186 $oForm->add_rep(
'rid',$rid);
187 $oForm->add_rep(
'iid',$iid);
192 $observer=
'obs_content_rid';
193 $observ[
'name'] = $observer;
194 $observ[
'form'] =
'dbx_form_{i}';
195 $observ[
'observ'] =
'content_rid';
196 $observ[
'value'] = $rid;
197 $observ[
'old'] = $rid;
199 $oForm->add_obj(
'img_wait',
'modul|img_wait',
"src=$url_img_ext");
200 $oForm->add_obj(
'msg',
'alert-warning',
'msg=Ein Content Bild kann erst nach dem Speichern der Systemdaten vom Content gewählt werden.');
201 $oForm->add_obj($observer,
'observer',$observ);
203 $oForm->add_js_observe($observer,11500);
207 $observer=
'obs_'.$img_key;
208 $observ[
'name'] = $observer;
209 $observ[
'form'] =
'dbx_form_{i}';
210 $observ[
'observ'] = $observer;
211 $observ[
'value'] = $img_src;
212 $observ[
'old'] = $img_src;
214 dbx()->debug(
"## images.class add_observer=($observer)",$observ);
216 $oForm->add_obj($observer,
'dbx|observer',$observ);
218 $oForm->add_obj($img_key,
'image-upload',$img_data);
220 $oForm->add_obj(
'button-modal' ,
'button-modal1b',$modal1);
221 $oForm->add_obj(
'button-submit',
'button-submit',
'label=Save');
223 $oForm->add_js_observe($observer,1500);
226 $oForm->add_js_call(
"uploader_img_{i}_$iid",
'upload');
242 $content=$oForm->run();