source: pro-violet-viettel/www/deploy/api/platform/lib/violetUtil.class.php

Last change on this file was 918, checked in by quyenla, 10 years ago

thumbnail for docs

File size: 3.5 KB
Line 
1<?php
2
3/**
4 * Utility functions for violet application
5 *
6 */
7class 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        if (is_file($pathToImage)) {
43            $info = pathinfo($pathToImage);
44            $extension = strtolower($info['extension']);
45                        error_log(date('YmdHis') .' - '.$pathToImage .' - '. $extension . "\n", 3, '/srv/www/sbg/log/thumbcreator.log');
46            $imagefunc = 'imagejpeg';
47
48            if (in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) {
49
50                switch ($extension) {
51                    case 'png':
52                        $img = imagecreatefrompng("{$pathToImage}");
53                        $imagefunc = 'imagepng';
54                        break;
55                    case 'gif':
56                        $img = imagecreatefromgif("{$pathToImage}");
57                        $imagefunc = 'imagegif';
58                        break;
59                    case 'jpg':
60                    case 'jpeg':
61                    default:
62                        $img = imagecreatefromjpeg("{$pathToImage}");
63                        $imagefunc = 'imagejpeg';
64                        break;
65                }
66
67                list($width, $height) = getimagesize($pathToImage);
68
69                $max_width = 52;
70                $max_height = 50;
71                $ratio = $max_width/$max_height;
72                $cropped_width = $height/$ratio;
73                $cropped_height = $width/$ratio;
74
75                if ($width < $height) {
76                        $new_height = $max_height;
77                        $new_width = $width * ($max_height/$height);
78                }
79                else if ($width > $height) {
80                        $new_width = $max_width;
81                        $new_height = $height * ($max_width/$width);
82                }
83
84                $image_p = imagecreatetruecolor($new_width, $new_height);
85
86                imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
87
88                $thumnail = $info['dirname'].DIRECTORY_SEPARATOR.$info['filename'].'_thumb.'.$extension;
89
90                if ($imagefunc == 'imagejpeg') {
91                        call_user_func($imagefunc, $image_p, $thumnail, 30);
92                }
93                else {
94                        call_user_func($imagefunc, $image_p, $thumnail);
95                }
96
97                return str_replace($_SERVER['DOCUMENT_ROOT'], "", $thumnail);
98            } else {
99                return "";
100            }
101        } else {
102            return "";
103        }
104
105        return sfView::NONE;
106    }
107}
Note: See TracBrowser for help on using the repository browser.