I have some code that overlays a logo onto my images. What I want is to start with a folder full of images and then overlay the logo onto each one and save a copy. It seems to me that the code below should accomplish that. However, in my browser I am getting.
Array ( [0] => ./images/im1.jpg [1] => ./images/im2.jpg [2] => ./images/im3.jpg [3] => ./images/im4.jpg [4] => ./images/im5.jpg [5] => ./images/im6.jpg [6] => ./images/im7.jpg [7] => ./images/out0.jpg [8] => ./images/out1.jpg [9] => ./images/out2.jpg [10] => ./images/out3.jpg [11] => ./images/out4.jpg [12] => ./images/out5.jpg )
As the output of that print_r statement printing out the files it supposedly found in the folder. I only have files im<#>.jpg, 1 through 7 none of the ones called out<#>.jpg are in there before the script runs. They are however created in the bottom while loop.
I'm completely baffled on this one because I actually do get my output images... just too many of them.
The relevant code (I think) starts at the foreach loop.
$logoHeight = 60;
$overlayImage = imagecreatefrompng('./overlay.png');
list($overlayWidth, $overlayHeight) = getimagesize('./overlay.png');
$overlayAspectRatio = ($overlayWidth*1.000) /($overlayHeight*1.0000);
list($resizedOverlayWidth, $resizedOverlayHeight) = [round($logoHeight*$overlayAspectRatio), $logoHeight];
list($paddingX, $paddingY) = [20, 20];
$filesInFolder = scandir('./images');
$pictureFilesToConvert = array();
$pictureNumber = 0;
foreach($filesInFolder as $file)
{
$fullFilePath = './images/'.$file;
$extension = pathinfo($fullFilePath,PATHINFO_EXTENSION);
if ($extension == "jpg")
{
$pictureFilesToConvert[$pictureNumber] = $fullFilePath;
$pictureNumber++;
}
}
$pictureNumber--; //decrement by one because we incremented even on the last image above.
print_r($pictureFilesToConvert);
print_r($pictureNumber);
$i = 0;
while($i < $pictureNumber)
{
$backgroundImage = imagecreatefromjpeg($pictureFilesToConvert[$i]);
list($backgroundWidth, $backgroundHeight) = getimagesize($pictureFilesToConvert[$i]);
$outputImage = imagecreatetruecolor($backgroundWidth, $backgroundHeight);
imagecopy($outputImage,$backgroundImage,0,0,0,0,$backgroundWidth, $backgroundHeight);
imagecopyresampled($outputImage, $overlayImage, $backgroundWidth - $resizedOverlayWidth - $paddingX, $backgroundHeight - $resizedOverlayHeight - $paddingY, 0, 0, $resizedOverlayWidth, $resizedOverlayHeight, $overlayWidth, $overlayHeight);
imagejpeg($outputImage, './images/out'.$i.'.jpg', 100);
$i++;
}
I end up with out0.jpg through out11.jpg. All but one (im7.jpg) of my input images is actually output twice! Its like some of my output files are being used as inputs but not all of them.