在PHP中給圖片添加圖片水印

<?php
//判斷GD庫是否存在
if (function_exists('imagepng')) {
    dir('GD庫不存在');
}

//圖片路徑
$imagePath = './img/a.jpg';
//圖片信息
$imageInfo = getimagesize($imagePath);
//圖片擴展名
$imageExtension = image_type_to_extension($imageInfo[2], false);
//獲取圖片
$func = 'imagecreatefrom' . $imageExtension;
$image = $func($imagePath);
//獲取水印圖片
$watermarkPath = './img/logo.png';
$watermarkInfo = getimagesize($watermarkPath);
$watermarkExtension = image_type_to_extension(getimagesize($watermarkPath)[2], false);
$func = 'imagecreatefrom' . $watermarkExtension;
$watermark = $func($watermarkPath);
//添加圖片水印
imagecopymerge($image, $watermark, 10, 10, 0, 0, $watermarkInfo[0], $watermarkInfo[1], 100);
//銷燬水印圖片
imagedestroy($watermark);
//輸出圖片
header('Content-Type:' . $imageInfo['mime']);
$func = 'image' . $imageExtension;
$func($image);
//銷燬圖片
imagedestroy($image);
相關文章
相關標籤/搜索