source: extensions/Ajax_Thumbnailer/ws_functions.inc.php @ 15683

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

Compatible with piwigo 2.2

File size: 4.2 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5$service = &$arr[0];
6$service->addMethod('pwg.images.createThumbnail', 'ws_images_createThumbnail',
7  array(
8  'picture'=>array(),
9  'width'=>array('default'=>"128"),
10  'height'=>array('default'=>"128"),
11  'ext'=>array('default'=>"jpg")
12  ),
13  'Creates a thumbnail for a given image,
14<br><b>picture</b> is the name of the picture to create thumbnail from.'
15);
16
17function ws_images_createThumbnail($params, $service)
18{
19  $picture = $params['picture'];
20  $width = (integer)$params['width'];
21  $height = (integer)$params['height'];
22  $gd_version = (integer)$params['gd_version'];         
23  $ext =$params['ext'];
24  $square = (isset($params['square']) and $params['square'] == 'true' and function_exists('process_ratio'));
25  return RatioResizeImg($picture,$width,$height,$ext,$gd_version,$square);
26}
27
28function RatioResizeImg($path, $newWidth, $newHeight, $tn_ext, $gd_version=2, $square=false)
29{
30  global $conf, $lang, $page;
31 
32  $starttime = get_moment();
33
34  if (!function_exists('gd_info'))
35    return new PwgError(WS_ERR_INVALID_PARAM,  'no gd');
36
37  if (!file_exists($path))
38    return new PwgError(WS_ERR_INVALID_PARAM,  'file not found');
39
40  $filename = basename($path);
41  $dirname = dirname($path);
42
43  // extension of the picture filename
44  $extension = get_extension($filename);
45
46  if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG'))) {
47    $srcImage = @imagecreatefromjpeg($path);
48  }
49  elseif ($extension == 'png' or $extension == 'PNG') {
50    $srcImage = @imagecreatefrompng($path);
51  } else {
52    unset($extension);
53  }
54
55  if ( isset( $srcImage ) ) {
56    // width/height
57    $srcWidth    = imagesx( $srcImage );
58    $srcHeight   = imagesy( $srcImage );
59
60    $coord = $square ? process_ratio($srcWidth, $srcHeight) : array('x'=>0, 'y'=>0);
61
62    $ratioWidth  = $srcWidth/$newWidth;
63    $ratioHeight = $srcHeight/$newHeight;
64
65    // maximal size exceeded ?
66    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) {
67      if ( $ratioWidth < $ratioHeight) { 
68        $destWidth = $srcWidth/$ratioHeight;
69        $destHeight = $newHeight; 
70      } else { 
71        $destWidth = $newWidth; 
72        $destHeight = $srcHeight/$ratioWidth;
73      }
74    } else {
75      $destWidth = $srcWidth;
76      $destHeight = $srcHeight;
77    }
78    // according to the GD version installed on the server
79    if ( $gd_version == 2 ) {
80      // GD 2.0 or more recent -> good results (but slower)
81      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
82      imagecopyresampled( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'],
83        $destWidth,$destHeight,$srcWidth,$srcHeight );
84    } else {
85      // GD prior to version  2 -> pretty bad results :-/ (but fast)
86      $destImage = imagecreate( $destWidth, $destHeight);
87      imagecopyresized( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'],
88        $destWidth,$destHeight,$srcWidth,$srcHeight );
89    }
90
91    $errors = array();
92    if (($tndir = mkget_thumbnail_dir($dirname, $errors)) == false) {
93      return new PwgError($errors[0]);
94    }
95
96    $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
97    $dest_file.= get_filename_wo_extension($filename);
98    $dest_file.= '.'.$tn_ext;
99
100    // creation and backup of final picture
101    if (!is_writable($tndir))
102      return new PwgError(WS_ERR_INVALID_PARAM,  '['.$tndir.'] : '.l10n('no_write_access'));
103     
104    imagejpeg($destImage, $dest_file, $conf['tn_compression_level']);
105    // freeing memory ressources
106    imagedestroy( $srcImage );
107    imagedestroy( $destImage );
108
109    list($tn_width, $tn_height) = getimagesize($dest_file);
110    $tn_size = floor(filesize($dest_file) / 1024).' KB';
111
112    $endtime = get_moment();
113   
114    $info = array( 'path'      => $path,
115    'tn_file'   => $dest_file,
116    'tn_width'  => $tn_width,
117    'tn_height' => $tn_height,
118    'tn_size'   => $tn_size,
119    'tn_time'   => number_format(($endtime - $starttime) * 1000, 2, '.', ' ').' ms');
120    return $info;
121  } else {
122    // error
123    $err=l10n('tn_no_support');
124    if ( isset( $extension ) )
125      $err .= l10n('tn_format').' '.$extension;
126    else
127      $err .= l10n('tn_thisformat');
128    return new PwgError(WS_ERR_INVALID_PARAM, $err);
129  }
130}
131
132?>
Note: See TracBrowser for help on using the repository browser.