#! /usr/bin/perl -w

    BEGIN{        
        use lib '../../../lib_v7/perl';        
    }

   
    use Data::Dumper;
 
    # custom module
    use General;
        
    print "Content-type:text/html\n\n";	
	
    # local
    
    my $lv;
    
    my $param   = &General::get_param_from_query($ENV{'QUERY_STRING'});
    
    $lv->{'source'} = "source";    
    $lv->{'target'} = "scaled";
        
    # get files
    
    opendir(DIR,$lv->{'source'});
    
    @{$lv->{'files'}} = readdir(DIR);
    
    closedir(DIR);
    
    shift(@{$lv->{'files'}});
    shift(@{$lv->{'files'}});
    
    my $img_index=($param->{'start'} eq '')?1:$param->{'start'};    
    
    # get files    
    for(@{$lv->{'files'}}){   
    
		if ($_=~m/($img_index[0-9])(\.png)/ig){
              
			#300x600x900
			system("convert source/$1.png     -resize 900x900^ -gravity center -extent 900x900  scaled/temp_$1_900.png");
			system("composite -watermark 25% -gravity south misc/logo_water_mark.png   scaled/temp_$1_900.png scaled/$1_900.png");
			system("convert scaled/$1_900.png -resize 600x600^ scaled/$1_600.jpg");
			system("convert scaled/$1_600.jpg -resize 300x300^ scaled/$1_300.jpg");

			# remove temp            
			unlink("scaled/temp_$1_900.png"); 
        }
    
    } # each file
   
    print "OK";
	exit;