Would this be a good way to watermark images with PHP?

Discussion in 'PHP' started by uleesgold, Feb 23, 2012.

  1. #1
    <?
    function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
       list($width, $height) = getimagesize($SourceFile);
       $image_p = imagecreatetruecolor($width, $height);
       $image = imagecreatefromjpeg($SourceFile);
       imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
       $black = imagecolorallocate($image_p, 0, 0, 0);
       $font = 'arial.ttf';
       $font_size = 10;
       imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
       if ($DestinationFile<>'') {
          imagejpeg ($image_p, $DestinationFile, 100);
       } else {
          header('Content-Type: image/jpeg');
          imagejpeg($image_p, null, 100);
       }
       imagedestroy($image);
       imagedestroy($image_p);
    }
    
    /******** usage **********/
    $SourceFile = '/home/user/www/images/image1.jpg';
    $DestinationFile = '/home/user/www/images/image1-watermark.jpg';
    $WaterMarkText = 'Copyright phpJabbers.com';
    watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
    ?>
    PHP:
    As I look at the code, it looks like it would be a way to watermark images one at a time, as if redundant code would be needed to do it again...

    Could I replace
    $DestinationFile = '/home/user/www/images/image1-watermark.jpg';
    PHP:
    with a wildcard :
     $DestinationFile = '/home/user/www/images/*';
    PHP:
    so that anything in the images directories ends up getting converted into a watermarked image.
    (So if this worked, the same thing would need to be done with $SourceFile )

    I'm also wondering if this would be a good way to watermark videos?
     
    uleesgold, Feb 23, 2012 IP
  2. Ambler

    Ambler Active Member

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    Digital Goods:
    1
    #2
    Its a function so read the files from the folder and loop it.

    
    $files = array();
    $pathToSrcDir = "\Path\to\your\files\";
    $pathToDestDir = "\Path\to\your\Newfiles\";
    $supported_ext = array('gif', 'jpg', 'jpeg');
    $dir = @opendir($pathToSrcDir);
    
    while ($file = @readdir($dir)){
          if (!is_dir($pathToSrcDir.$file)){
                watermarkImage ($pathToSrcDir.$file, $WaterMarkText, $pathToDestDir.$file);
          }
    }
    
    @closedir($dir);
    
    PHP:
     
    Ambler, Feb 23, 2012 IP