1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Utility functions for violet application |
---|
5 | * |
---|
6 | */ |
---|
7 | class violetUtil { |
---|
8 | |
---|
9 | public static function getSpacePath($absolute = true) { |
---|
10 | if ($absolute) { |
---|
11 | return sfConfig::get('sf_upload_dir').'/'.sfConfig::get('app_space_dir').'/'; |
---|
12 | } else { |
---|
13 | return sfConfig::get('sf_upload_dir_name').'/'.sfConfig::get('app_space_dir').'/'; |
---|
14 | } |
---|
15 | } |
---|
16 | |
---|
17 | public static function getTempPath($subDir = '', $create = false) { |
---|
18 | $tempPath = sfConfig::get('sf_upload_dir') .'/'. sfConfig::get('app_temp_dir') .'/'. $subDir; |
---|
19 | if ($create && !file_exists($tempPath)) mkdir($tempPath, 0777, true); |
---|
20 | return $tempPath; |
---|
21 | } |
---|
22 | |
---|
23 | public static function getContentType($fileType) { |
---|
24 | switch (strtolower($fileType)) { |
---|
25 | case 'xml': |
---|
26 | case 'xvl': return 'text/xml'; |
---|
27 | |
---|
28 | case 'jpe': |
---|
29 | case 'jpg': |
---|
30 | case 'jpeg': return 'image/jpeg'; |
---|
31 | case 'gif': return 'image/gif'; |
---|
32 | case 'png': return 'image/png'; |
---|
33 | |
---|
34 | case 'flv': return 'video/x-flv'; |
---|
35 | case 'mp4': return 'video/mp4'; |
---|
36 | case 'mp3': return 'audio/mpeg'; |
---|
37 | case 'swf': return 'application/x-shockwave-flash'; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | public function createThumb($pathToImage="") { |
---|
42 | |
---|
43 | if (is_file($pathToImage)) { |
---|
44 | $info = pathinfo($pathToImage); |
---|
45 | $extension = strtolower($info['extension']); |
---|
46 | error_log(date('YmdHis') .' - '.$pathToImage .' - '. $extension . "\n", 3, '/srv/www/sbg/log/thumbcreator.log'); |
---|
47 | $imagefunc = 'imagejpeg'; |
---|
48 | |
---|
49 | if (in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) { |
---|
50 | |
---|
51 | switch ($extension) { |
---|
52 | case 'png': |
---|
53 | $img = imagecreatefrompng("{$pathToImage}"); |
---|
54 | $imagefunc = 'imagepng'; |
---|
55 | break; |
---|
56 | case 'gif': |
---|
57 | $img = imagecreatefromgif("{$pathToImage}"); |
---|
58 | $imagefunc = 'imagegif'; |
---|
59 | break; |
---|
60 | case 'jpg': |
---|
61 | case 'jpeg': |
---|
62 | default: |
---|
63 | $img = imagecreatefromjpeg("{$pathToImage}"); |
---|
64 | $imagefunc = 'imagejpeg'; |
---|
65 | break; |
---|
66 | } |
---|
67 | |
---|
68 | list($width, $height) = getimagesize($pathToImage); |
---|
69 | |
---|
70 | $max_width = 64; |
---|
71 | $max_height = 50; |
---|
72 | $ratio = $max_width/$max_height; |
---|
73 | $cropped_width = $height/$ratio; |
---|
74 | $cropped_height = $width/$ratio; |
---|
75 | |
---|
76 | if ($width < $height) { |
---|
77 | $new_height = $max_height; |
---|
78 | $new_width = $width * ($max_height/$height); |
---|
79 | } |
---|
80 | else if ($width > $height) { |
---|
81 | $new_width = $max_width; |
---|
82 | $new_height = $height * ($max_width/$width); |
---|
83 | } |
---|
84 | |
---|
85 | $image_p = imagecreatetruecolor($new_width, $new_height); |
---|
86 | |
---|
87 | imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); |
---|
88 | |
---|
89 | $thumnail = $info['dirname'].DIRECTORY_SEPARATOR.$info['filename'].'_thumb.'.$extension; |
---|
90 | |
---|
91 | if ($imagefunc == 'imagegif' || $imagefunc == 'imagepng' ) { |
---|
92 | call_user_func($imagefunc, $image_p, $thumnail); |
---|
93 | } |
---|
94 | else { |
---|
95 | call_user_func($imagefunc, $image_p, $thumnail, 30); |
---|
96 | } |
---|
97 | |
---|
98 | return str_replace($_SERVER['DOCUMENT_ROOT'], "", $thumnail); |
---|
99 | } else { |
---|
100 | return ""; |
---|
101 | } |
---|
102 | } else { |
---|
103 | return ""; |
---|
104 | } |
---|
105 | |
---|
106 | return sfView::NONE; |
---|
107 | } |
---|
108 | } |
---|