script PHPスクリプトで画像にウォーターマークを付ける 作成日: 2025年7月12日
画像ギャラリーなどで画像にウォーターマークを付けるPHPスクリプト。
JPEG・PNG・GIFに対して、指定のPNG画像をウォーターマークとして付けて表示させるもの。
マーク用PNG画像のアルファ値調整をPHP上で出来るようにと試したが、エラーが多く難しいのでこれは予め画像で透明度設定する。
マーク位置は調整可能。
<?php
ini_set('default_charset', 'UTF-8');
mb_internal_encoding('UTF-8');
$source_image_path = '<MTEntryAssets limit="1"><$mt:AssetURL$></MTEntryAssets>';
$watermark_image_path = '/home/kir877430/public_html/surf-plus.com/object/sp_02-1.png';
$output_image_path = 'output_<MTEntryID>.jpg';
$position = 'bottom-right-percent'; // ウォーターマークの位置を 'bottom-right-percent' に設定
error_log('Debug: Source image path - ' . ($source_image_path ?: 'empty'));
error_log('Debug: Watermark image path - ' . $watermark_image_path);
if (!$source_image_path) {
error_log('Error: Source image path is empty');
echo 'default.jpg';
} elseif (!($info = @getimagesize($source_image_path))) {
error_log('Error: Failed to get image info - ' . $source_image_path);
echo $source_image_path;
} else {
$mime = $info['mime'];
error_log('Debug: Source image MIME - ' . $mime);
switch ($mime) {
case 'image/jpeg': $src = @imagecreatefromjpeg($source_image_path); break;
case 'image/png': $src = @imagecreatefrompng($source_image_path); break;
case 'image/gif': $src = @imagecreatefromgif($source_image_path); break;
default:
error_log('Error: Unsupported image format - ' . $mime);
echo $source_image_path;
break;
}
if (!$src) {
error_log('Error: Failed to load source image - ' . $source_image_path);
echo $source_image_path;
} elseif (!file_exists($watermark_image_path) || !($wm_info = @getimagesize($watermark_image_path))) {
error_log('Error: Watermark image not found or invalid - ' . $watermark_image_path);
echo $source_image_path;
} else {
$wm = @imagecreatefrompng($watermark_image_path);
if (!$wm) {
error_log('Error: Failed to load watermark image - ' . $watermark_image_path);
echo $source_image_path;
} else {
error_log('Debug: Watermark image MIME - ' . $wm_info['mime']);
$w = imagesx($src);
$h = imagesy($src);
$wm_w = imagesx($wm);
$wm_h = imagesy($wm);
$m = 10; // 既存のマージン値(ピクセル)
error_log('Debug: Source dimensions - ' . $w . 'x' . $h);
error_log('Debug: Watermark dimensions - ' . $wm_w . 'x' . $wm_h);
// 位置計算
switch ($position) {
case 'center':
$x = floor(($w - $wm_w) / 2);
$y = floor(($h - $wm_h) / 2);
break;
case 'top-left':
$x = $m;
$y = $m;
break;
case 'top-right':
$x = $w - $wm_w - $m;
$y = $m;
break;
case 'bottom-left':
$x = $m;
$y = $h - $wm_h - $m;
break;
case 'bottom-right':
$x = $w - $wm_w - $m;
$y = $h - $wm_h - $m;
break;
case 'bottom-right-percent': // 新しい位置の計算
// 小数点以下を切り捨てて整数に変換します
$x = (int)($w - $wm_w - ($w * 0.05));//右から5%
$y = (int)($h - $wm_h - ($h * 0.1));//下から10%
break;
default:
$x = floor(($w - $wm_w) / 2);
$y = floor(($h - $wm_h) / 2);
}
error_log('Debug: Watermark position - x:' . $x . ', y:' . $y);
// imagecopy でアルファチャンネルを保持
imagecopy($src, $wm, $x, $y, 0, 0, $wm_w, $wm_h);
$output_dir = dirname($output_image_path);
if (!is_writable($output_dir)) {
error_log('Error: Output directory not writable - ' . $output_dir);
echo $source_image_path;
} else {
switch ($mime) {
case 'image/jpeg': imagejpeg($src, $output_image_path, 90); break;
case 'image/png': imagepng($src, $output_image_path, 9); break;
case 'image/gif': imagegif($src, $output_image_path); break;
}
error_log('Debug: Output image saved - ' . $output_image_path);
echo $output_image_path;
}
imagedestroy($src);
imagedestroy($wm);
}
}
}
?>