source: extensions/square_thumbnails/branch/2.1/functions.inc.php @ 9755

Last change on this file since 9755 was 9755, checked in by patdenice, 13 years ago

Revert branch 2.1 (wrong ccommit)

File size: 5.8 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5// Resize function for thumbnails creation page
6function thumbnail_square_resize($info, $path, $newWidth, $newHeight, $tn_ext)
7{
8  global $conf, $lang, $page;
9
10  if ($info !== false)
11  {
12    //someone hooked us - so we skip
13    return $info;
14  }
15
16  if (!function_exists('gd_info'))
17  {
18    return;
19  }
20
21  $filename = basename($path);
22  $dirname = dirname($path);
23 
24  // extension of the picture filename
25  $extension = get_extension($filename);
26
27  if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG')))
28  {
29    $srcImage = @imagecreatefromjpeg($path);
30  }
31  else if ($extension == 'png' or $extension == 'PNG')
32  {
33    $srcImage = @imagecreatefrompng($path);
34  }
35  else
36  {
37    unset($extension);
38  }
39
40  if ( isset( $srcImage ) )
41  {
42    // width/height
43    $srcWidth    = imagesx( $srcImage ); 
44    $srcHeight   = imagesy( $srcImage ); 
45    $x = 0;
46    $y = 0;
47
48    if($srcWidth > $srcHeight)
49    {
50      $x = ceil(($srcWidth - $srcHeight) / 2 );
51      $srcWidth = $srcHeight;
52    }
53    elseif ($srcHeight > $srcWidth)
54    {
55      $y = ceil(($srcHeight - $srcWidth) / 2);
56      $srcHeight = $srcWidth;
57    }
58
59    $ratioWidth  = $srcWidth/$newWidth;
60    $ratioHeight = $srcHeight/$newHeight;
61
62    // maximal size exceeded ?
63    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
64    {
65      if ( $ratioWidth < $ratioHeight)
66      { 
67        $destWidth = $srcWidth/$ratioHeight;
68        $destHeight = $newHeight; 
69      }
70      else
71      { 
72        $destWidth = $newWidth; 
73        $destHeight = $srcHeight/$ratioWidth;
74      }
75    }
76    else
77    {
78      $destWidth = $srcWidth;
79      $destHeight = $srcHeight;
80    }
81
82    // according to the GD version installed on the server
83    if ( $_POST['gd'] == 2 )
84    {
85      // GD 2.0 or more recent -> good results (but slower)
86      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
87      imagecopyresampled( $destImage, $srcImage, 0, 0, $x, $y,
88                          $destWidth,$destHeight,$srcWidth,$srcHeight );
89    }
90    else
91    {
92      // GD prior to version  2 -> pretty bad results :-/ (but fast)
93      $destImage = imagecreate( $destWidth, $destHeight);
94      imagecopyresized( $destImage, $srcImage, 0, 0, $x, $y,
95                        $destWidth,$destHeight,$srcWidth,$srcHeight );
96    }
97
98    if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false)
99    {
100      return false;
101    }
102
103    $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
104    $dest_file.= get_filename_wo_extension($filename);
105    $dest_file.= '.'.$tn_ext;
106   
107    // creation and backup of final picture
108    if (!is_writable($tndir))
109    {
110      array_push($page['errors'], '['.$tndir.'] : '.l10n('no write access'));
111      return false;
112    }
113    imagejpeg($destImage, $dest_file, $conf['tn_compression_level']);
114    // freeing memory ressources
115    imagedestroy( $srcImage );
116    imagedestroy( $destImage );
117   
118    list($tn_width, $tn_height) = getimagesize($dest_file);
119    $tn_size = floor(filesize($dest_file) / 1024).' KB';
120   
121    $info = array( 'path'      => $path,
122                   'tn_file'   => $dest_file,
123                   'tn_width'  => $tn_width,
124                   'tn_height' => $tn_height,
125                   'tn_size'   => $tn_size );
126    return $info;
127  }
128  // error
129  else
130  {
131    echo l10n('Picture unreachable or no support')." ";
132    if ( isset( $extenstion ) )
133    {
134      echo l10n('for the file format').' '.$extension;
135    }
136    else
137    {
138      echo l10n('for this file format');
139    }
140    exit();
141  }
142}
143
144
145// Resize function for Upload Form
146function upload_square_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality)
147{
148  if ($result !== false)
149  {
150    //someone hooked us - so we skip
151    return $result;
152  }
153
154  if (!function_exists('gd_info'))
155  {
156    return false;
157  }
158
159  // extension of the picture filename
160  $extension = strtolower(get_extension($source_filepath));
161
162  $source_image = null;
163  if (in_array($extension, array('jpg', 'jpeg')))
164  {
165    $source_image = @imagecreatefromjpeg($source_filepath);
166  }
167  else if ($extension == 'png')
168  {
169    $source_image = @imagecreatefrompng($source_filepath);
170  }
171  else
172  {
173    die('unsupported file extension');
174  }
175 
176  // width/height
177  $source_width  = imagesx($source_image); 
178  $source_height = imagesy($source_image);
179  $x = 0;
180  $y = 0;
181
182  if($source_width > $source_height)
183  {
184    $x = ceil(($source_width - $source_height) / 2 );
185    $source_width = $source_height;
186  }
187  elseif ($source_height > $source_width)
188  {
189    $y = ceil(($source_height - $source_width) / 2);
190    $source_height = $source_width;
191  }
192 
193  $ratio_width  = $source_width / $max_width;
194  $ratio_height = $source_height / $max_height;
195 
196  // maximal size exceeded ?
197  if ($ratio_width > 1 or $ratio_height > 1)
198  {
199    if ($ratio_width < $ratio_height)
200    { 
201      $destination_width = ceil($source_width / $ratio_height);
202      $destination_height = $max_height; 
203    }
204    else
205    { 
206      $destination_width = $max_width; 
207      $destination_height = ceil($source_height / $ratio_width);
208    }
209  }
210  else
211  {
212    // the image doesn't need any resize! We just copy it to the destination
213    copy($source_filepath, $destination_filepath);
214    return true;
215  }
216 
217  $destination_image = imagecreatetruecolor($destination_width, $destination_height);
218 
219  imagecopyresampled(
220    $destination_image,
221    $source_image,
222    0,
223    0,
224    $x,
225    $y,
226    $destination_width,
227    $destination_height,
228    $source_width,
229    $source_height
230    );
231 
232  imagejpeg($destination_image, $destination_filepath, $quality);
233  // freeing memory ressources
234  imagedestroy($source_image);
235  imagedestroy($destination_image);
236
237  // everything should be OK if we are here!
238  return true;
239}
240
241add_event_handler('thumbnail_resize', 'thumbnail_square_resize', 40, 5);
242add_event_handler('upload_thumbnail_resize', 'upload_square_resize', 40, 6);
243
244?>
Note: See TracBrowser for help on using the repository browser.