WaterMarkCommand.php
3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
class WaterMarkCommand extends CConsoleCommand
{
    private  $days;
    private function mark($imagePath)
    {
        $image_info = getimagesize($imagePath);
        $imageType = $image_info[2];
        $create = null;
        $save = null;
        switch ($imageType) {
            case IMAGETYPE_JPEG:
                $create = 'imagecreatefromjpeg';
                $save = 'imagejpeg';
                break;
            case IMAGETYPE_GIF:
                $create = 'imagecreatefromgif';
                $save = 'imagegif';
                break;
            case IMAGETYPE_PNG:
                $create = 'imagecreatefrompng';
                $save = 'imagepng';
                break;
        }
        $path = realpath(Yii::getPathOfAlias('application') . '/../images/watermark.png');
        if (!isset($create)) return;
        $mark = imagecreatefrompng($path);
        $image = $create($imagePath);
        $x = 0;
        $y = 0;
        $w = round(imagesx($mark) * imagesx($image) / 800);
        $h = round(imagesy($mark) * imagesx($image) / 800);
        imagealphablending($image, true);
        imagesavealpha($image, true);
        $cut = imagecreatetruecolor($w, $h);
        imagecopy($cut, $image, 0, 0, $x, $y, $w, $h);
        imagecopyresampled($cut, $mark, 0, 0, 0, 0, $w, $h, imagesx($mark), imagesy($mark));
        // merging both of the images
        imagecopymerge($image, $cut, $x, $y, 0, 0, $w, $h, 100);
//        imagecopymerge($image, $mark, $x, $y, 0, 0, $w, $h, imagesx($mark), imagesy($mark));
        imagedestroy($mark);
        imagedestroy($cut);
        $save($image, $imagePath);
    }
    private function scanDir($dir)
    {
        $start = mktime(0, 0, 0, date('m'), date('d') - $this->days, date('Y'));
        $end = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        //echo date('Y-m-d h:i:s', $start), "\n";
        //echo date('Y-m-d h:i:s', $end), "\n";
        foreach (scandir($dir) as $file) {
            if ($file == '.' || $file == '..' || substr($file,0,1) == '.') continue;
            $filename = $dir . '/' . $file;
            if (is_dir($filename)) {
                $this->scanDir($filename);
                continue;
            }
            $lastmod = filemtime($filename);
            // echo date('Y-m-d h:i:s', $lastmod), "\n";
            if ($lastmod >= $start && $lastmod < $end) {
                Yii::log('Addding watermark to file '.$filename.', modified at '.date('Y-m-d h:i:s', $lastmod),CLogger::LEVEL_INFO,'waterMark');
                $this->mark($filename);
                $data = file_get_contents($filename);
                unlink($filename);
                file_put_contents($filename,$data);
                chmod($filename,0777);
                @touch($filename, $lastmod);
                // echo $filename, "\n";
            }
        }
    }
    public function actionAdd($days=1)
    {
        $this->days = (int)$days;
        $uploads = realpath(Yii::getPathOfAlias('application') . '/../uploads');
        Yii::log('waterMark command started, with days='.$days,CLogger::LEVEL_INFO,'waterMark');
        $start = mktime(0, 0, 0, date('m'), date('d') - $this->days, date('Y'));
        $end = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        Yii::log('Adding watermarks to files in directory: "'.$uploads.'" form '.
        date('Y-m-d h:i:s', $start). " to ". date('Y-m-d h:i:s', $end),CLogger::LEVEL_INFO,'waterMark');
        $this->scanDir($uploads);
    }
}