Proyectos de Subversion LeadersLinked - Services

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace LeadersLinked\Library;
3
 
4
 
283 www 5
class Image
1 efrain 6
{
283 www 7
 
1 efrain 8
    /**
283 www 9
     *
10
     * @var \LeadersLinked\Library\Image
11
     */
12
    private static $_instance;
13
 
14
    /**
15
     *
16
     * @var array
17
     */
18
    private $config;
334 www 19
 
20
     /**
21
     *
283 www 22
     * @param array $config
23
     */
334 www 24
    private function __construct($config)
1 efrain 25
    {
334 www 26
        $this->config       = $config;
283 www 27
    }
1 efrain 28
 
29
    /**
30
     *
283 www 31
     * @param array $config
32
     * @return \LeadersLinked\Library\Image
1 efrain 33
     */
283 www 34
    public static function getInstance($config)
1 efrain 35
    {
283 www 36
        if(self::$_instance == null) {
37
            self::$_instance = new Image($config);
38
        }
1 efrain 39
 
283 www 40
        return self::$_instance;
1 efrain 41
    }
42
 
283 www 43
 
44
    /**
45
     *
1 efrain 46
     * @param string $source
47
     * @param string $target_filename
48
     * @param number $target_width
49
     * @param number $target_height
50
     * @param boolean $crop_to_dimensions
51
     * @return boolean
52
     */
334 www 53
    public function uploadProcessChangeSize($source, $target_filename, $target_width, $target_height,  $crop_to_dimensions)
1 efrain 54
    {
334 www 55
        $temp_filename = '';
1 efrain 56
 
57
 
58
        try {
334 www 59
 
1 efrain 60
            $data = file_get_contents($source);
61
            $img = imagecreatefromstring($data);
62
 
63
 
64
            if($img) {
65
                list($source_width, $source_height) = getimagesize($source);
66
 
67
                if($crop_to_dimensions) {
68
                    $width_ratio = $target_width / $source_width;
69
                    $height_ratio = $target_height / $source_height;
70
                    if($width_ratio > $height_ratio) {
71
                        $resized_width = $target_width;
72
                        $resized_height = $source_height * $width_ratio;
73
                    } else {
74
                        $resized_height = $target_height;
75
                        $resized_width = $source_width * $height_ratio;
76
                    }
77
                } else {
78
                    $width_ratio = $target_width / $source_width;
79
                    $resized_width = $target_width;
80
                    $resized_height = $source_height * $width_ratio;
81
                    if($resized_height > $target_height) {
82
                        $height_ratio = $target_height / $resized_height;
83
                        $resized_height = $target_height;
84
                        $resized_width = $resized_width * $height_ratio;
85
                    }
86
                }
283 www 87
 
1 efrain 88
 
89
                $resized_width = round($resized_width);
90
                $resized_height = round($resized_height);
91
 
92
                $offset_width = round(($target_width - $resized_width) / 2);
93
                $offset_height = round(($target_height - $resized_height) / 2);
94
 
95
 
96
                $new_image = imageCreateTrueColor($target_width, $target_height);
97
                imageAlphaBlending($new_image, False);
98
                imageSaveAlpha($new_image, True);
99
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
100
                imagefill($new_image, 0, 0, $transparent);
101
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
102
 
103
 
334 www 104
                $temp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $target_filename;
283 www 105
                imagepng($new_image, $temp_filename);
290 www 106
 
283 www 107
 
108
 
109
            } else {
334 www 110
                $temp_filename = '';
1 efrain 111
            }
112
 
113
        }
114
        catch (\Throwable $e)
115
        {
334 www 116
 
117
            $temp_filename = '';
118
 
1 efrain 119
        }
334 www 120
 
121
        @unlink($source);
122
        return $temp_filename;
1 efrain 123
    }
124
 
125
    /**
126
     *
127
     * @param string $source
128
     * @param string $target_filename
129
     * @return boolean
130
     */
334 www 131
    public function uploadProcessWithOutChangeSize($source, $target_filename)
1 efrain 132
    {
334 www 133
        $temp_filename = '';
134
 
1 efrain 135
        try {
136
            $data = file_get_contents($source);
334 www 137
 
1 efrain 138
 
283 www 139
            $img = imagecreatefromstring($data);
140
 
1 efrain 141
            if($img) {
142
                list($source_width, $source_height) = getimagesize($source);
143
 
144
                $target_width = $source_width;
145
                $target_height =  $source_height;
146
 
147
                $width_ratio    = $target_width / $source_width;
148
                $height_ratio   = $target_height / $source_height;
149
                if($width_ratio > $height_ratio) {
150
                    $resized_width = $target_width;
151
                    $resized_height = $source_height * $width_ratio;
152
                } else {
153
                    $resized_height = $target_height;
154
                    $resized_width = $source_width * $height_ratio;
155
                }
156
 
157
                $resized_width = round($resized_width);
158
                $resized_height = round($resized_height);
159
 
160
                $offset_width = round(($target_width - $resized_width) / 2);
161
                $offset_height = round(($target_height - $resized_height) / 2);
162
 
163
 
164
                $new_image = imageCreateTrueColor($target_width, $target_height);
165
                imageAlphaBlending($new_image, False);
166
                imageSaveAlpha($new_image, True);
167
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
168
                imagefill($new_image, 0, 0, $transparent);
169
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
170
 
171
 
334 www 172
 
1 efrain 173
 
334 www 174
                $temp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $target_filename;
283 www 175
                imagepng($new_image, $temp_filename);
1 efrain 176
 
283 www 177
 
178
 
334 www 179
            } else {
180
                $temp_filename = '';
1 efrain 181
            }
182
 
183
        }
184
        catch (\Throwable $e)
185
        {
334 www 186
 
187
            $temp_filename = '';
188
 
1 efrain 189
        }
334 www 190
 
191
        @unlink($source);
192
        return $temp_filename;
193
 
1 efrain 194
    }
195
}