我會做這樣的:
function add_png_logo(){
$dir = APPPATH.'assets/img/'; # Where your PNG photos are
$dirUpload = APPPATH.'assets/uploads/'; # Where they will be saved
$baseUrl = 'http://127.0.0.1/tests/assets/uploads/'; # Base to link merged images
# Where the work begins
$iteractor = new DirectoryIterator($dir);
$ext = ['png'];
$i = 0;
# Function to save individual files
function save_png($getFilename,$getFilePath){
$medida = array('width'=>'1024','height'=>'1024',);
// Creates a temp image
$TempPngFile = imagecreatetruecolor($medida['width'], $medida['height']);
# Defines a transparent color to fill all image
$TransparentColor = imagecolorallocatealpha($TempPngFile, 0, 0, 0, 127);
imagefill($TempPngFile, 0, 0, $TransparentColor);
# Forces transparency definition
imagealphablending($TempPngFile, true);
imagesavealpha($TempPngFile, true);
# Open image
$logo = imageCreateFromPng(APPPATH.'cache/mark.png');
# Fix transparency definitions
imageAlphaBlending($logo, true);
imageSaveAlpha($logo, true);
# Open image
$img2 = imageCreateFromPng($getFilename);
# Forces transparency definition
imageAlphaBlending($img2, true);
imageSaveAlpha($img2, true);
# insert $logo and $img2 in $TempPngFile and setting positioning
imagecopy($TempPngFile, $img2, 0, 0, 0, 0, imagesx($img2), imagesy($img2));
imagecopy($TempPngFile, $logo, 25, 25, 0, 0, imagesx($logo), imagesy($logo));
# Save final image to $getFilePath
imagepng($TempPngFile, $getFilePath);
// Destroy images
imageDestroy($TempPngFile);
imageDestroy($logo);
imageDestroy($img2);
}
# Loop in $dir, get all PNG files to overlap $logo on left top
foreach ($iteractor as $entry) {
if ($entry->isFile()) {
if (in_array($entry->getExtension(), $ext)) {
$getFilename = $dir.$entry->getFilename();
$getImageName = $entry->getFilename().'_'.$i++.'_.png';
$getFilePath = $dirUpload.$getImageName;
save_png($getFilename, $getFilePath);
echo 'Created image: <a href="'.$baseUrl.$getImageName.'" target="_blank">'.$getImageName.'</a><br>';
}
}
}
}
OBS:它使用php-gd
擴展。並且肯定有一種方法可以在重疊文件之前將JPG轉換爲PNG(或者您應該先將3000張照片轉換爲PNG),但現在我太懶了,而且它很有用!現在它在你的手中!
Adobe Lightroom和Photoshop讓你可以輕鬆地做到這一點 – JimL
你如何有任何toturial或任何演示 – und3rc00d3
Photoshop在文件 - 自動化批處理。在Lightroom中,只是導出帶有水印的文件,這是一個導出選項 – JimL