-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexif-remover.php
62 lines (54 loc) · 1.6 KB
/
exif-remover.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/*
* Plugin Name: Exif Remover
* Description: メディアに画像がアップロードされたときに EXIF データを自動削除する。サポート対象フォーマット: JPEG / PNG / WebP 。
* Version: 0.1.0
* Author: 後藤隼人
* Author URI: https://dyno.design/
*/
defined( 'ABSPATH' ) || exit;
/**
* 画像アップロード時のアクションフック: Exif 情報を削除する
*/
add_action('add_attachment', function ($attachment_id) {
try {
$result = exif_remover_remove_exif($attachment_id);
if ($result !== true) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log("Exif Remover Result for ID {$attachment_id}: {$result}");
}
}
} catch (Exception $e) {
error_log('Exif Remover Error: ' . $e->getMessage());
}
});
/**
* 画像の EXIF 情報を削除する
*/
function exif_remover_remove_exif($attachment_id) {
$path = get_attached_file($attachment_id);
if (!file_exists($path)) {
return 'ファイルが見つかりませんでした';
}
$type = exif_imagetype($path);
switch ($type) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($path);
imagejpeg($image, $path, 100);
imagedestroy($image);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($path);
imagepng($image, $path, 9);
imagedestroy($image);
break;
case IMAGETYPE_WEBP:
$image = imagecreatefromwebp($path);
imagewebp($image, $path, 100);
imagedestroy($image);
break;
default:
return '対応していないファイルフォーマット';
}
return true;
}