Development

sfDynamicCMSPlugin: ThumbnailHelper.php

You must first sign up to be able to contribute.

sfDynamicCMSPlugin: ThumbnailHelper.php

File ThumbnailHelper.php, 3.3 kB (added by Sylvio, 3 weeks ago)
Line 
1 <?php
2
3 function getThumbnail($image, $width=null, $height=null, $scale = true, $inflate = true, $quality = 75)
4 {
5     $image_dir=dirname($image);
6     $image_file=basename($image);
7     
8     $source_file=sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$image_file;
9     if(file_exists(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$image_file))
10     {
11       $thumbnail_dir='';
12       if ($width>0) $thumbnail_dir.=$width;
13       if ($height>0) $thumbnail_dir.='x'.$height;
14       if (!$scale) $thumbnail_dir.='s';
15       if (!$inflate) $thumbnail_dir.='i';
16       if ($width>0 || $height>0) $thumbnail_dir.='/';
17       
18       if (!file_exists(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$thumbnail_dir.$image_file) && ($width!=null || $height!=null))
19       {
20         if (!is_dir(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$thumbnail_dir))
21         {
22           mkdir (sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$thumbnail_dir,0777);
23         }
24         
25         $path_parts=pathinfo($source_file);
26         if(file_exists($source_file) && in_array($path_parts['extension'],array('jpg','jpeg','gif','png')))
27         {
28           $thumbnail = new sfThumbnail($width, $height,$scale,$inflate,$quality);
29           $thumbnail->loadFile(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$image_file);
30           $thumbnail->save(sfConfig::get('sf_upload_dir').'/'.$image_dir.'/'.$thumbnail_dir.$image_file);
31         }
32       }
33       return '/uploads'.'/'.$image_dir.'/'.$thumbnail_dir.$image_file;
34     }
35     else
36     {
37       return false;
38     }
39 }
40
41 /**
42  * Get the path of a generated thumbnail for any given image
43  *
44  * @param string $source
45  * @param int $width
46  * @param int $height
47  * @param boolean $absolute
48  * @return string
49  */
50 function thumbnail_path($source, $width, $height, $absolute = false)
51 {
52   $thumbnails_dir = sfConfig::get('app_sfThumbnail_thumbnails_dir', 'uploads/thumbnails');
53  
54   $width = intval($width);
55   $height = intval($height);
56  
57   if (substr($source, 0, 1) == '/') {
58     $realpath = sfConfig::get('sf_web_dir') . $source;
59   } else {
60     $realpath = sfConfig::get('sf_web_dir') . '/images/' . $source;
61   }
62  
63   $real_dir = dirname($realpath);
64   $thumb_dir = '/' . $thumbnails_dir . substr($real_dir, strlen(sfConfig::get('sf_web_dir')));
65   $thumb_name = preg_replace('/^(.*?)(\..+)?$/', '$1_' . $width . 'x' . $height . '$2', basename($source));
66  
67   $img_from = $realpath;
68   $thumb = $thumb_dir . '/' . $thumb_name;
69   $img_to = sfConfig::get('sf_web_dir') . $thumb;
70  
71   if (!is_dir(dirname($img_to))) {
72     if (!mkdir(dirname($img_to), 0777, true)) {
73       throw new Exception('Cannot create directory for thumbnail : ' . $img_to);
74     }
75   }
76  
77   if (!is_file($img_to) || filemtime($img_from) > filemtime($img_to)) {
78     $thumbnail = new sfThumbnail($width, $height);
79     $thumbnail->loadFile($img_from);
80     $thumbnail->save($img_to);
81   }
82  
83   return image_path($thumb, $absolute);
84 }
85
86 /**
87  * Get the <img> tag to include a thumbnail into your web page
88  *
89  * @param string $source
90  * @param int $width
91  * @param int $height
92  * @param mixed $options
93  * @return string
94  */
95 function thumbnail_tag($source, $width, $height, $options = array())
96 {
97   $img_src = thumbnail_path($source, $width, $height, false);
98   return image_tag($img_src, $options);
99 }
100
101
102
103
104 ?>