157157 $ com = str_replace ('"\n" ' ,"<br> " ,$ com ); //改行文字の前に HTMLの改行タグ
158158 $ email ='' ;
159159 $ now_time = substr ($ time ,0 ,-3 );
160- $ now =now_date ($ now_time );
160+ $ now =now_date (( int ) $ now_time );
161161 $ now .= $ userid ? " ID: " . $ userid : "" ;
162162 $ tool =switch_tool ($ tool );
163163 $ newlog []="$ __no, $ now, $ name, $ email, $ sub, $ com, $ url, $ host, $ hash, $ ext, $ w, $ h, $ time, $ log_img_hash, $ painttime,, $ pchext, $ thumbnail, $ tool,6 \n" ;
174174unset($ oya );
175175
176176//ツリーログ
177+ $ _treeline =[];
177178foreach ($ treeline as $ val ){
178179 list ($ _oya ,)=explode (', ' ,rtrim ($ val ));
179180 $ _treeline [$ _oya ]=$ val ;
@@ -215,7 +216,7 @@ function lang_en(){//言語が日本語以外ならtrue。
215216}
216217
217218// 日付
218- function now_date ($ time ){
219+ function now_date (int $ time ){
219220 $ youbi = array ('日 ' ,'月 ' ,'火 ' ,'水 ' ,'木 ' ,'金 ' ,'土 ' );
220221 $ yd = $ youbi [(int )date ("w " , $ time )] ;
221222 $ date = date (DATE_FORMAT , $ time );
@@ -225,26 +226,26 @@ function now_date($time){
225226}
226227
227228//タブ除去
228- function t ($ str ){
229+ function t (? string $ str ){
229230 return str_replace ("\t" ,"" ,$ str );
230231}
231232
232- function check_dir ($ path ) {
233+ function check_dir (? string $ path ) {
233234
234235 if (!is_dir ($ path )) {
235236 mkdir ($ path , PERMISSION_FOR_DIR ,true );
236237 chmod ($ path , PERMISSION_FOR_DIR );
237238 }
238239}
239- function check_petit ($ path ) {
240+ function check_petit (? string $ path ) {
240241
241242 if (!is_dir ($ path )) {
242243 mkdir ($ path , PERMISSION_FOR_POTI ,true );
243244 chmod ($ path , PERMISSION_FOR_POTI );
244245 }
245246}
246247
247- function switch_tool ($ tool ){
248+ function switch_tool (? string $ tool ){
248249 switch ($ tool ){
249250 case 'neo ' :
250251 $ tool ='PaintBBS NEO ' ;
@@ -281,7 +282,7 @@ function switch_tool($tool){
281282 * @param $filepath
282283 * @return string
283284 */
284- function check_pch_ext ($ filepath ) {
285+ function check_pch_ext (? string $ filepath ) {
285286
286287 $ exts =[".pch " ,".spch " ,".tgkr " ,".chi " ,".psd " ];
287288
@@ -297,40 +298,50 @@ function check_pch_ext ($filepath) {
297298 return '' ;
298299}
299300
300- // thumbnail_gd.inc.php for PetitNote (C)さとぴあ @satopian 2021 - 2025
301+ // thumbnail_gd.inc.php for PetitNote (C)さとぴあ @satopian 2021-2026 MIT License
301302// https://paintbbs.sakura.ne.jp/
302303// originalscript (C)SakaQ 2005 http://www.punyu.net/php/
303304
304- $ thumbnail_gd_ver =20250707 ;
305- // defined('PERMISSION_FOR_DEST') or define('PERMISSION_FOR_DEST', 0606); //config.phpで未定義なら0606
305+ $ thumbnail_gd_ver =20260501 ;
306306class thumbnail_gd {
307307
308- public static function thumb ($ path ,$ fname ,$ time ,$ max_w ,$ max_h ,$ options =[]): ?string {
309- // $path=basename($path).'/';
308+ /**
309+ * @param int|string|null $max_w
310+ * @param int|string|null $max_h
311+ */
312+
313+ public static function thumb (?string $ path ,?string $ fname ,?string $ time ,$ max_w ,$ max_h ,array $ options =[]): string {
314+ $ path =basename ($ path ).'/ ' ;
310315 $ fname =basename ($ fname );
311316 $ time =basename ($ time );
312317 if (!ctype_digit ($ time )) {
313- return null ;
318+ return '' ;
314319 }
315320 $ fname =$ path .$ fname ;
316321 if (!is_file ($ fname )){
317- return null ;
322+ return '' ;
318323 }
319324 if (!self ::gd_check ()||!function_exists ("ImageCreate " )||!function_exists ("ImageCreateFromJPEG " )){
320- return null ;
325+ return '' ;
326+ }
327+ if (isset ($ options ['png2webp ' ])||isset ($ options ['png2jpeg ' ])){
328+ $ options ['2webp ' ]=true ;//互換処理
329+ }
330+ if ((isset ($ options ['webp ' ])||isset ($ options ['2webp ' ])||isset ($ options ['thumbnail_webp ' ])) && !function_exists ("ImageWEBP " )){
331+ return '' ;
321332 }
322- if ((isset ($ options ['webp ' ])||isset ($ options ['thumbnail_webp ' ])) && !function_exists ("ImageWEBP " )){
323- return null ;
333+ if ((isset ($ options ['avif ' ])||isset ($ options ['2avif ' ])|| isset ( $ options [ ' thumbnail_avif ' ])) && !function_exists ("ImageAVIF " )){
334+ return '' ;
324335 }
325336
326337 $ fsize = filesize ($ fname ); // ファイルサイズを取得
327338 list ($ w ,$ h ) = GetImageSize ($ fname ); // 画像の幅と高さを取得
328339 $ w_h_size_over = $ max_w && $ max_h && ($ w > $ max_w || $ h > $ max_h );
329340 $ f_size_over = !isset ($ options ['toolarge ' ]) ? ($ fsize >1024 *1024 ) : false ;
330- if (!$ w_h_size_over && !$ f_size_over && !isset ($ options ['webp ' ]) && !isset ($ options ['png2webp ' ]) && !isset ($ options ['png2jpeg ' ])){
331- return null ;
341+ if (!$ w_h_size_over && !$ f_size_over && !isset ($ options ['webp ' ]) && !isset ($ options ['2webp ' ]) && !isset ($ options ['2png ' ]) && ! isset ( $ options [ ' 2jpeg ' ])){ //リサイズも変換もしない
342+ return '' ;
332343 }
333- if (!$ w_h_size_over || isset ($ options ['png2jpeg ' ]) || isset ($ options ['png2webp ' ]) || !$ max_w || !$ max_h ){//リサイズしない
344+ if (!$ w_h_size_over || isset ($ options ['2webp ' ]) || isset ($ options ['2png ' ]) || !$ max_w || !$ max_h ){//リサイズしない
334345 $ out_w = $ w ;
335346 $ out_h = $ h ;
336347 }else {// リサイズ
@@ -343,7 +354,7 @@ public static function thumb($path,$fname,$time,$max_w,$max_h,$options=[]): ?str
343354
344355 $ mime_type = mime_content_type ($ fname );
345356 if (!$ im_in = self ::createImageResource ($ fname ,$ mime_type )){
346- return null ;
357+ return '' ;
347358 };
348359 // 出力画像(サムネイル)のイメージを作成
349360 if (function_exists ("ImageCreateTrueColor " )){
@@ -370,8 +381,8 @@ public static function thumb($path,$fname,$time,$max_w,$max_h,$options=[]): ?str
370381 ImageCopyResized ($ im_out , $ im_in , 0 , 0 , 0 , 0 , $ out_w , $ out_h , $ w , $ h );//"ImageCopyResampled"が無効の時
371382 }
372383
373- if (isset ($ options ['toolarge ' ])){
374- $ outfile = self ::overwriteResizedImage ($ im_out , $ fname, $ mime_type );
384+ if (isset ($ options ['toolarge ' ])){//元画像を縮小してPNGで上書き
385+ $ outfile = self ::overwriteResizedImageWithPNG ($ im_out , $ fname );
375386 }else {
376387 $ outfile = self ::createThumbnailImage ($ im_out , $ time , $ options );
377388 }
@@ -380,17 +391,17 @@ public static function thumb($path,$fname,$time,$max_w,$max_h,$options=[]): ?str
380391 self ::safeImageDestroy ($ im_out );
381392
382393 if (!$ outfile ){
383- return null ;
394+ return '' ;
384395 }
385396
386397 if (!chmod ($ outfile ,PERMISSION_FOR_DEST )){
387- return null ;
398+ return '' ;
388399 }
389400
390401 if (is_file ($ outfile )){
391402 return $ outfile ;
392403 }
393- return null ;
404+ return '' ;
394405
395406 }
396407 //GD版が使えるかチェック
@@ -414,31 +425,37 @@ private static function gd_check(): bool {
414425 return true ;
415426 }
416427
417- //GDのイメージを破棄
428+ /**
429+ * GDのイメージを破棄
430+ * @param resource|\GdImage|null $gdImage
431+ */
418432 private static function safeImageDestroy ($ gdImage ): void {
419433 if (PHP_VERSION_ID < 80000 ) {//PHP8.0未満の時は
420434 imagedestroy ($ gdImage );
421435 }
422436 }
423437
424438 // 透明度の処理を行う必要があるかを判断
425- private static function isTransparencyEnabled ($ options , $ mime_type ): bool {
439+ private static function isTransparencyEnabled (array $ options ,? string $ mime_type ): bool {
426440 // 透明度を扱うオプションが設定されているか確認
427- $ transparencyOptionsSet = isset ($ options ['toolarge ' ]) || isset ($ options ['webp ' ]) || isset ($ options ['thumbnail_webp ' ]) || isset ($ options ['png2webp ' ]);
441+ $ transparencyOptionsSet = isset ($ options ['toolarge ' ]) || isset ($ options ['webp ' ]) || isset ($ options ['thumbnail_webp ' ]) || isset ($ options ['2webp ' ]) || isset ( $ options [ ' 2png ' ]);
428442
429443 // 対象の画像形式で透明度がサポートされているか確認
430- $ transparencySupportedFormats = ["image/png " , "image/gif " , "image/webp " ];
444+ $ transparencySupportedFormats = ["image/png " , "image/gif " , "image/webp " , " image/avif " ];
431445
432446 // 透明度を扱うための関数が存在するか確認
433447 $ transparencyFunctionsAvailable = function_exists ("imagealphablending " ) && function_exists ("imagesavealpha " );
434448
435449 return $ transparencyOptionsSet && in_array ($ mime_type , $ transparencySupportedFormats ) && $ transparencyFunctionsAvailable ;
436450 }
437- //各画像フォーマットのリソースを作成
438- private static function createImageResource ($ fname ,$ mime_type ) {
451+ /**
452+ *各画像フォーマットのリソースを作成
453+ * @param string|bool $mime_type
454+ */
455+ private static function createImageResource (?string $ fname ,$ mime_type ) {
439456 switch ($ mime_type ) {
440457 case "image/gif " :
441- if (!function_exists ("ImageCreateFromGIF " )){//gif
458+ if (!function_exists ("ImageCreateFromGIF " )) {//gif
442459 return null ;
443460 }
444461 $ im_in = @ImageCreateFromGIF ($ fname );
@@ -449,92 +466,95 @@ private static function createImageResource($fname,$mime_type) {
449466 if (!$ im_in )return null ;
450467 break ;
451468 case "image/png " :
452- if (!function_exists ("ImageCreateFromPNG " )){//png
469+ if (!function_exists ("ImageCreateFromPNG " )) {//png
453470 return null ;
454471 }
455472 $ im_in = @ImageCreateFromPNG ($ fname );
456473 if (!$ im_in )return null ;
457474 break ;
458475 case "image/webp " :
459- if (!function_exists ("ImageCreateFromWEBP " )){//webp
476+ if (!function_exists ("ImageCreateFromWEBP " )) {//webp
460477 return null ;
461478 }
462479 $ im_in = @ImageCreateFromWEBP ($ fname );
463480 if (!$ im_in )return null ;
464481 break ;
482+ case "image/avif " :
483+ if (!function_exists ("ImageCreateFromAVIF " )) {//avif
484+ return null ;
485+ }
486+ $ im_in = @ImageCreateFromAVIF ($ fname );
487+ if (!$ im_in )return null ;
488+ break ;
465489
466490 default : return null ;
467491 }
468492 return $ im_in ;
469493 }
470494
471- //縮小した画像で上書き
472- private static function overwriteResizedImage ($ im_out , $ fname , $ mime_type ): ?string {
495+ /**
496+ * 縮小してPNGで上書き
497+ * @param resource|\GdImage|null $im_out
498+ */
499+ private static function overwriteResizedImageWithPNG ($ im_out , ?string $ fname ): ?string {
473500 $ outfile =(string )$ fname ;
474501 //本体画像を縮小
475- switch ($ mime_type ) {
476- case "image/gif " :
477- if (function_exists ("ImagePNG " )){
478- ImagePNG ($ im_out , $ outfile ,3 );
479- }else {
480- ImageJPEG ($ im_out , $ outfile ,98 );
481- }
482- return $ outfile ;
483- case "image/jpeg " :
502+ if (function_exists ("ImagePNG " )) {
503+ ImagePNG ($ im_out , $ outfile ,3 );
504+ } else {
484505 ImageJPEG ($ im_out , $ outfile ,98 );
485- return $ outfile ;
486- case "image/png " :
487- if (function_exists ("ImagePNG " )){
488- ImagePNG ($ im_out , $ outfile ,3 );
489- }else {
490- ImageJPEG ($ im_out , $ outfile ,98 );
491- }
492- return $ outfile ;
493- case "image/webp " :
494- if (function_exists ("ImageWEBP " )){
495- ImageWEBP ($ im_out , $ outfile ,98 );
496- }else {
497- ImageJPEG ($ im_out , $ outfile ,98 );
498- }
499- return $ outfile ;
500-
501- default : return null ;
502-
503- }
506+ }
507+ return $ outfile ;
504508 }
505- //サムネイル作成
506- private static function createThumbnailImage ($ im_out , $ time , $ options ): ?string {
509+ /**
510+ * サムネイル作成
511+ * @param resource|\GdImage|null $im_out
512+ */
513+ private static function createThumbnailImage ($ im_out ,?string $ time ,array $ options ): ?string {
507514
508- if (isset ($ options ['png2jpeg ' ])){
515+ if (isset ($ options ['2png ' ])) {
509516
510- $ outfile =TEMP_DIR .$ time .'.jpg.tmp ' ;//一時ファイル
511- ImageJPEG ($ im_out , $ outfile ,98 );
517+ $ outfile =TEMP_DIR .$ time .'.png.tmp ' ;//一時ファイル
518+ ImagePNG ($ im_out , $ outfile ,3 );
519+
520+ } elseif (isset ($ options ['2jpeg ' ])) {
512521
513- } elseif (isset ($ options ['png2webp ' ])){
522+ $ outfile =TEMP_DIR .$ time .'.jpeg.tmp ' ;//一時ファイル
523+ imagejpeg ($ im_out , $ outfile ,98 );
514524
515- if (function_exists ("ImageWEBP " )){
516- $ outfile =TEMP_DIR .$ time .'.webp.tmp ' ;//一時ファイル
517- ImageWEBP ($ im_out , $ outfile ,98 );
525+ } elseif (isset ($ options ['2webp ' ])) {
518526
519- }else {
520- $ outfile =TEMP_DIR .$ time .'.jpg.tmp ' ;//一時ファイル
521- ImageJPEG ($ im_out , $ outfile ,98 );
527+ $ outfile =TEMP_DIR .$ time .'.webp.tmp ' ;//一時ファイル
528+ ImageWEBP ($ im_out , $ outfile ,98 );
529+
530+ } elseif (isset ($ options ['2avif ' ])) {
522531
523- }
532+ $ outfile =TEMP_DIR .$ time .'.avif.tmp ' ;//一時ファイル
533+ imageavif ($ im_out , $ outfile ,90 );
524534
525- } elseif (isset ($ options ['webp ' ])){
535+ } elseif (isset ($ options ['webp ' ])) {
526536
527- $ outfile =" poti/ webp/{ $ time} t.webp " ;
537+ $ outfile =' webp/ ' . $ time. ' t.webp ' ;
528538 ImageWEBP ($ im_out , $ outfile ,90 );
529539
530- }elseif (isset ($ options ['thumbnail_webp ' ])){
540+ } elseif (isset ($ options ['avif ' ])) {
541+
542+ $ outfile ='avif/ ' .$ time .'t.avif ' ;
543+ imageavif ($ im_out , $ outfile ,80 );
544+
545+ } elseif (isset ($ options ['thumbnail_webp ' ])) {
531546
532- $ outfile =" poti/thumb/ { $ time} s.webp " ;
547+ $ outfile =THUMB_DIR . $ time. ' s.webp ' ;
533548 ImageWEBP ($ im_out , $ outfile ,90 );
534549
535- }else {
550+ } elseif (isset ($ options ['thumbnail_avif ' ])) {
551+
552+ $ outfile =THUMB_DIR .$ time .'s.avif ' ;
553+ imageavif ($ im_out , $ outfile ,80 );
554+
555+ } else {
536556
537- $ outfile =" poti/thumb/ { $ time} s.jpg " ;
557+ $ outfile =THUMB_DIR . $ time. ' s.jpg ' ;
538558 // サムネイル画像を保存
539559 ImageJPEG ($ im_out , $ outfile ,90 );
540560
0 commit comments