我正在尝试在文件夹中对我的所有图像应用水印。为了得到工作,我正在使用这行代码。
<?php
function endsWith($currentString, $target)
{
$length = strlen($target);
if ($length == 0) {
return true;
}
return (substr($currentString, -$length) === $target);
}
if ($handle = opendir('images')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (endsWith($entry,'.jpg')==0){
echo "<br/> $entry: is ignored.\n ";
continue;
}
echo "<br/> $entry\n ";
$image_path = "images/".$entry;
$watermark = imagecreatefrompng('wa/ap.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatefromjpeg($image_path);
if ($image === false) {
return false;
}
$tragetedImageSize = getImageSize($image_path);
$wmPositionX = $tragetedImageSize[0]- $watermark_width - 6;
$wmPositionY = $tragetedImageSize[1] - $watermark_height - 6;
imagealphablending($image, true);
imagealphablending($watermark, true);
imagecopy($image, $watermark, $wmPositionX, $wmPositionY, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image, "images/$entry");
imagedestroy($image);
imagedestroy($watermark);
}
}
closedir($handle);
}
?>
这段代码工作正常。但是现在如果在这个文件夹中添加一个新图像并再次运行我的PHP代码,那么上一次alredy水印的其他图像。我想跳过那些上次加水印的图像。
Extra ::如果images文件夹中有更多子文件夹,那么还需要更改哪些代码?
由于