Watermark images using GD

Discussion in 'PHP' started by izlik, Aug 9, 2014.

  1. #1
    Hey

    How can i watermark images in a folder using GD? all images that is and not only 1
     
    izlik, Aug 9, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    PoPSiCLe, Aug 9, 2014 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    That's for just one single images, i have seen loads of them but not for doing batch watermarking :/
     
    izlik, Aug 10, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Do a loop through each file?
    Read in the files, one by one, apply watermark, done.
     
    PoPSiCLe, Aug 10, 2014 IP
  5. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #5
    I tried the following code with the following errors, can you se what im doing wrong? :/

    Notice: Undefined variable: image_in_folder in /home/mvafoto/gallery/test/test.php on line 20

    Warning: Invalid argument supplied for foreach() in /home/mvafoto/gallery/test/test.php on line 20

    <?
    error_reporting(E_ALL);
    ini_set('display_errors', 'on');
    $dir_to_img='/';
        $watermark = "watermark.png";
        $dir_to_new_image='/watered/';
    
        if(is_dir($dir_to_img)){
            if($dir = opendir($dir_to_img)){
                while(($archivo = readdir($dir)) !== false){
                    if($archivo != '.' && $archivo != '..' && $archivo != '.htaccess'){
                        if(substr($archivo, -4) == '.jpg' || substr($archivo, -4) == '.JPG') //only jpg files
                           $image_in_folder[] = $archivo;
                    }
                }
            closedir($dir);
            }
        }
    
        foreach ($image_in_folder as $imagen) {
            $im = imagecreatefrompng($watermark);
            $im2 = imagecreatefromjpeg($dir_to_img."/".$imagen);
    
                imagecopy($im2, $im, (imagesx($im2)/2)-(imagesx($im)/2), (imagesy($im2)/2)-(imagesy($im)/2), 0, 0, imagesx($im), imagesy($im));
    
            if(!is_dir($dir_to_new_image)){   //save to another dir
                mkdir($dir_to_new_image, 0777, true);
            }
    
            imagejpeg($im2,"$dir_to_new_image/$imagen",90);
            imagedestroy($im);
            imagedestroy($im2);
         }
    ?>
    PHP:
     
    izlik, Aug 10, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Well, two questions, really. Are you trying to read root? Because that's what you're trying to open.
    Second, you should assign $image_in_folder to an array outside the loop. As I can see now, I'm guessing there are no images in the root folder, hence the $image_in_folder-array is never created, and the for each fails
     
    PoPSiCLe, Aug 10, 2014 IP
  7. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #7
    Got it to work, was just thinking of / as the root of the current folder!: P
     
    Last edited: Aug 10, 2014
    izlik, Aug 10, 2014 IP