Jump to content

[SOLVED] Watermarking Directory Of Files


fanfavorite

Recommended Posts

I have a script that resizes a directory of images to thumbnails and larger format.  The code is below:

 

$dirHandle = opendir($uploaddir);
while ($file = readdir($dirHandle)) {
    if(!is_dir($uploaddir.$file) AND $file != ".." AND $file != ".") {
        $dirfile = $uploaddir.$file;
        $ext = substr(strrchr($file, "."), 1);
        if($ext == "jpg" || $ext == "jpeg") {		
            $src = imagecreatefromjpeg($dirfile);	
        } elseif($ext == "png") {		
            $src = imagecreatefrompng($dirfile);	
        } elseif($ext == "gif") {		
            $src = imagecreatefromgif($dirfile);
        } elseif($ext == "bmp") {		
            $src = imagecreatefromwbmp($dirfile);
        }
        list($width,$height)=getimagesize($dirfile);

        if ($width >= $height AND $width > 500) {
            $newwidth=500;
            $newheight=($height/$width)*500;
        } elseif ($height > 500) {
            $newheight=500;
            $newwidth=($width/$height)*500;
        } else { 
            $newheight = $height;
            $newwidth = $width;	
        }
        $tmp=imagecreatetruecolor($newwidth,$newheight);
        imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
        
        $tmp2=imagecreatetruecolor(100,100);

            $wm = $width/100;
            $hm = $height/100;

        if ($width > $height) {
            $adjusted_width = $width / $hm;
                $half_width = $adjusted_width / 2;
                $int_width = $half_width - 50;
            imagecopyresampled($tmp2,$src,-$int_width,0,0,0,$adjusted_width,100,$width,$height);
        } else {
            $adjusted_height = $height / $wm;
                $half_height = $adjusted_height / 2;
                $int_height = $half_height - 50;
            imagecopyresampled($tmp2,$src,0,-$int_height,0,0,100,$adjusted_height,$width,$height);
        }

        $newfile = $uploaddir."larger/" . $file;
            $newfile2 = $uploaddir.$file;
        if($ext == "jpg" || $ext == "jpeg") {
            imagejpeg($tmp,$newfile,100);
            imagejpeg($tmp2,$newfile2,100);
        } elseif($ext == "png") {
            imagepng($tmp,$newfile,100);
            imagepng($tmp2,$newfile2,100);
        } elseif($ext == "gif") {		
            imagegif($tmp,$newfile,100);
            imagegif($tmp2,$newfile2,100);
        } elseif($ext == "bmp") {		
            imagewbmp($tmp,$newfile,100);
            imagewbmp($tmp2,$newfile2,100);
        }
    }
}

 

Now I need to somehow incorporate a watermark script.  Do I have to run it separately or can I incorporate it into this?  If so, how would I do that?  Most watermark scripts I have seen I can make work on a single image, but not sure how to do it with reading a directory of images. 

 

Thanks for the help! 

 

-JC

Link to comment
Share on other sites

Ive used your code to resize my images so its only sensible you use mine to watermark your images

 

 


$watermark = ImageCreateFromGif('watermark.gif'); 
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  

//create image

if (strstr($file_allocation_new,'jpg') || strstr($file_allocation_new,'JPG') || strstr($file_allocation_new,'jpeg') || strstr($file_allocation_new,'JPEG')) {

$file_type="jpg";

}

if (strstr($file_allocation_new,'png') || strstr($file_allocation_new,'PNG')) {

$file_type="png";

}

if (strstr($file_allocation_new,'gif') || strstr($file_allocation_new,'GIF')) {

$file_type="gif";

}


switch ($file_type) {

case "jpg":

$image = imagecreatefromjpeg($file_allocation_new); 

break;

case "gif":

$image = imagecreatefromGif($file_allocation_new); 

break;

case "png":

$image = imagecreatefromPng($file_allocation_new); 

break;

}


$size = getimagesize($file_allocation_new);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5; 

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100); 


switch ($file_type) {

case "jpg":

imagejpeg($image,".." . $base_watermark . "/$new_file_name." . $file_type);

break;

case "gif":

imagegif($image,".." . $base_watermark . "/$new_file_name." . $file_type);

break;

case "png":

imagepng($image,".." . $base_watermark . "/$new_file_name." . $file_type);

break;

}

  
imagedestroy($image);  
imagedestroy($watermark);  

 

$file_allocation_new is the path of your image

$base_watermark and $new file name are just parts of the path that make up the place you want to save the watermarked image too.

 

if you do not want to save the image just take out the path bit. If you want to do this for every file in a directory You would have to use this script on each individual one.

 

I have gone the long way round with determin the type of file it is. You can just use part of the getimage size to switch the type of file jpg gif or png. I haven't used bmp in my system but i believe you can use bmp too

 

Hope that helps dude.

 

 

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.