0

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.

Kyle Preiksa
  • 516
  • 2
  • 7
  • 23
  • Please extract the relevant code and be more specific about where you think there's something wrong. – simeg Jul 26 '14 at 19:07
  • Well, first of all, this is the relevant code. I can't just post one loop because looking at just one loop would you think there is anything wrong with this? I have some programming experience and LOGICALLY my code makes sense. I don't see anywhere there could be something wrong. I do not, however, have much PHP experience so I'm wondering if there is something specific about this language where things are done a little differently. If I suspected something I would try to figure it out on my own rather than coming here just to have people tell me I need to bow down to their requests. – Kyle Preiksa Jul 26 '14 at 19:10
  • do you mean too few of them since there are 7 input and 5 output? – Fabricator Jul 26 '14 at 19:18
  • @Fabricator nope I am actually getting too many. I get out0 thorough out11, with all but one (the last) of the input images actually being "output" twice. In other words, it is using SOME of the output images as inputs (but not all of them) I will add this information to my original question – Kyle Preiksa Jul 26 '14 at 19:21

2 Answers2

1

The code missed the last image because after the line $pictureNumber--;, $pictureNumber = 6 (if there are 7 input images). However, in the final while loop, it only ran 6 times (0 to 5). Images being ouput twice is probably from rerunning the script without deleting the earlier outputs. Here is a version that uses a more conventional syntax:

foreach($filesInFolder as $file)                                                                                                                      
{                                                                                                                                                     
    $fullFilePath = './images/'.$file;                                                                                                                
    $extension = pathinfo($fullFilePath,PATHINFO_EXTENSION);                                                                                          

    if ($extension == "jpg")                                                                                                                          
    {                                                                                                                                                 
        $pictureFilesToConvert[] = $fullFilePath;                                                                                                     
    }                                                                                                                                                 
}                                                                                                                                                     

print_r($pictureFilesToConvert);                                                                                                                      

foreach ($pictureFilesToConvert as $i => $pic)                                                                                                        
{                                                                                                                                                     
    $backgroundImage = imagecreatefromjpeg($pic);                                                                                                     
    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);                                                                                                             
}
Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • This part makes sense doing it a more conventional way and all, but I still get two copies of all outputs when running through Google Chrome. Interestingly, I did try running through a command line and the script works as expected with only one set of outputs. I definitely start with a clean folder each time I run the script, so I wonder what Chrome is doing that is causing this problem? – Kyle Preiksa Jul 27 '14 at 02:20
  • check this out: http://stackoverflow.com/questions/5948063/rewriterule-causes-page-to-reload-twice/5948301#5948301 – Fabricator Jul 27 '14 at 02:23
  • That is excellent to know, didn't know about that feature or possible consequences. I suspected the script was running twice but I sprinkled some basic echo's around and they only ran once, which is why I was so confused. As it stands now, the script runs properly through MAMP but not through phpstorm's debugger. Must be something different about the server configs. – Kyle Preiksa Jul 27 '14 at 02:28
0

I feel stupid now... I think something is wrong with the way the phpstorm debugger is running my page. I downloaded MAMP, copied my project into that folder and ran it directly and it works (except for the off by one error pointed out by @Fabricator) I guess I just assumed a debugger would work properly :(

Kyle Preiksa
  • 516
  • 2
  • 7
  • 23