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 \LeadersLinked\Library\Storage
17
     */
18
    private $storage;
19
 
20
    /**
1 efrain 21
     *
283 www 22
     * @var array
23
     */
24
    private $config;
25
 
26
    /**
27
     *
28
     * @param array $config
29
     */
30
    private function __construct($config)
1 efrain 31
    {
283 www 32
        $this->config = $config;
33
        $this->storage = Storage::getInstance($config);
34
    }
1 efrain 35
 
36
    /**
37
     *
283 www 38
     * @param array $config
39
     * @return \LeadersLinked\Library\Image
1 efrain 40
     */
283 www 41
    public static function getInstance($config)
1 efrain 42
    {
283 www 43
        if(self::$_instance == null) {
44
            self::$_instance = new Image($config);
45
        }
1 efrain 46
 
283 www 47
        return self::$_instance;
1 efrain 48
    }
49
 
50
    /**
51
     *
283 www 52
     * @return \LeadersLinked\Library\Storage
53
     */
54
    public function getStorage()
55
    {
56
        return $this->storage;
57
    }
58
 
59
 
60
 
61
    /**
62
     *
1 efrain 63
     * @param string $source
64
     * @param string $target_path
283 www 65
     * @param string $target_code
1 efrain 66
     * @param string $target_filename
67
     * @param number $target_width
68
     * @param number $target_height
69
     * @param boolean $crop_to_dimensions
283 www 70
     * @param boolean $unlink_source
1 efrain 71
     * @return boolean
72
     */
283 www 73
    public function uploadImageChangeSize($source, $target_path, $target_code, $target_filename, $target_width, $target_height,  $crop_to_dimensions, $unlink_source)
1 efrain 74
    {
75
 
76
 
77
 
78
        try {
79
            $data = file_get_contents($source);
283 www 80
            if($unlink_source) {
81
                unlink($source);
82
            }
83
 
1 efrain 84
            $img = imagecreatefromstring($data);
85
 
86
 
87
            if($img) {
88
                list($source_width, $source_height) = getimagesize($source);
89
 
90
                if($crop_to_dimensions) {
91
                    $width_ratio = $target_width / $source_width;
92
                    $height_ratio = $target_height / $source_height;
93
                    if($width_ratio > $height_ratio) {
94
                        $resized_width = $target_width;
95
                        $resized_height = $source_height * $width_ratio;
96
                    } else {
97
                        $resized_height = $target_height;
98
                        $resized_width = $source_width * $height_ratio;
99
                    }
100
                } else {
101
                    $width_ratio = $target_width / $source_width;
102
                    $resized_width = $target_width;
103
                    $resized_height = $source_height * $width_ratio;
104
                    if($resized_height > $target_height) {
105
                        $height_ratio = $target_height / $resized_height;
106
                        $resized_height = $target_height;
107
                        $resized_width = $resized_width * $height_ratio;
108
                    }
109
                }
283 www 110
 
1 efrain 111
 
112
                $resized_width = round($resized_width);
113
                $resized_height = round($resized_height);
114
 
115
                $offset_width = round(($target_width - $resized_width) / 2);
116
                $offset_height = round(($target_height - $resized_height) / 2);
117
 
118
 
119
                $new_image = imageCreateTrueColor($target_width, $target_height);
120
                imageAlphaBlending($new_image, False);
121
                imageSaveAlpha($new_image, True);
122
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
123
                imagefill($new_image, 0, 0, $transparent);
124
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
125
 
126
 
283 www 127
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
128
                imagepng($new_image, $temp_filename);
129
 
130
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
131
                unlink($temp_filename);
132
 
133
 
134
                return $result;
135
            } else {
136
 
137
                return true;
1 efrain 138
            }
139
 
140
        }
141
        catch (\Throwable $e)
142
        {
143
            error_log($e->getTraceAsString());
144
            return false;
145
        }
146
    }
147
 
148
    /**
149
     *
150
     * @param string $source
151
     * @param string $target_path
283 www 152
     * @param string $target_code
1 efrain 153
     * @param string $target_filename
283 www 154
     * @param boolean $unlink_source
1 efrain 155
     * @return boolean
156
     */
283 www 157
    public function uploadImageRaw($source, $target_path, $target_code, $target_filename, $unlink_source)
1 efrain 158
    {
159
 
160
 
161
 
162
        try {
163
            $data = file_get_contents($source);
283 www 164
            if($unlink_source) {
165
                unlink($source);
1 efrain 166
            }
167
 
283 www 168
            $img = imagecreatefromstring($data);
169
 
1 efrain 170
            if($img) {
171
                list($source_width, $source_height) = getimagesize($source);
172
 
173
                $target_width = $source_width;
174
                $target_height =  $source_height;
175
 
176
                $width_ratio    = $target_width / $source_width;
177
                $height_ratio   = $target_height / $source_height;
178
                if($width_ratio > $height_ratio) {
179
                    $resized_width = $target_width;
180
                    $resized_height = $source_height * $width_ratio;
181
                } else {
182
                    $resized_height = $target_height;
183
                    $resized_width = $source_width * $height_ratio;
184
                }
185
 
186
                $resized_width = round($resized_width);
187
                $resized_height = round($resized_height);
188
 
189
                $offset_width = round(($target_width - $resized_width) / 2);
190
                $offset_height = round(($target_height - $resized_height) / 2);
191
 
192
 
193
                $new_image = imageCreateTrueColor($target_width, $target_height);
194
                imageAlphaBlending($new_image, False);
195
                imageSaveAlpha($new_image, True);
196
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
197
                imagefill($new_image, 0, 0, $transparent);
198
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
199
 
200
 
201
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
202
                if(file_exists($target)) {
203
                    @unlink($target);
204
                }
205
 
283 www 206
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
207
                imagepng($new_image, $temp_filename);
1 efrain 208
 
283 www 209
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
210
                unlink($temp_filename);
211
 
212
 
213
                return $result;
1 efrain 214
            }
215
 
216
 
217
            return true;
218
 
219
        }
220
        catch (\Throwable $e)
221
        {
222
            error_log($e->getTraceAsString());
223
            return false;
224
        }
225
    }
226
}