addMethod('pwg.images.createThumbnail', 'ws_images_createThumbnail', array( 'picture'=>array(), 'width'=>array('default'=>"128"), 'height'=>array('default'=>"128"), 'ext'=>array('default'=>"jpg") ), 'Creates a thumbnail for a given image,
picture is the name of the picture to create thumbnail from.' ); function ws_images_createThumbnail($params, $service) { $picture = $params['picture']; $width = (integer)$params['width']; $height = (integer)$params['height']; $gd_version = (integer)$params['gd_version']; $ext =$params['ext']; $square = (isset($params['square']) and $params['square'] == 'true' and function_exists('process_ratio')); return RatioResizeImg($picture,$width,$height,$ext,$gd_version,$square); } function RatioResizeImg($path, $newWidth, $newHeight, $tn_ext, $gd_version=2, $square=false) { global $conf, $lang, $page; $starttime = get_moment(); if (!function_exists('gd_info')) return new PwgError(WS_ERR_INVALID_PARAM, 'no gd'); if (!file_exists($path)) return new PwgError(WS_ERR_INVALID_PARAM, 'file not found'); $filename = basename($path); $dirname = dirname($path); // extension of the picture filename $extension = get_extension($filename); if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG'))) { $srcImage = @imagecreatefromjpeg($path); } elseif ($extension == 'png' or $extension == 'PNG') { $srcImage = @imagecreatefrompng($path); } else { unset($extension); } if ( isset( $srcImage ) ) { // width/height $srcWidth = imagesx( $srcImage ); $srcHeight = imagesy( $srcImage ); $coord = $square ? process_ratio($srcWidth, $srcHeight) : array('x'=>0, 'y'=>0); $ratioWidth = $srcWidth/$newWidth; $ratioHeight = $srcHeight/$newHeight; // maximal size exceeded ? if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) { if ( $ratioWidth < $ratioHeight) { $destWidth = $srcWidth/$ratioHeight; $destHeight = $newHeight; } else { $destWidth = $newWidth; $destHeight = $srcHeight/$ratioWidth; } } else { $destWidth = $srcWidth; $destHeight = $srcHeight; } // according to the GD version installed on the server if ( $gd_version == 2 ) { // GD 2.0 or more recent -> good results (but slower) $destImage = imagecreatetruecolor( $destWidth, $destHeight); imagecopyresampled( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'], $destWidth,$destHeight,$srcWidth,$srcHeight ); } else { // GD prior to version 2 -> pretty bad results :-/ (but fast) $destImage = imagecreate( $destWidth, $destHeight); imagecopyresized( $destImage, $srcImage, 0, 0, $coord['x'], $coord['y'], $destWidth,$destHeight,$srcWidth,$srcHeight ); } $errors = array(); if (($tndir = mkget_thumbnail_dir($dirname, $errors)) == false) { return new PwgError($errors[0]); } $dest_file = $tndir.'/'.$conf['prefix_thumbnail']; $dest_file.= get_filename_wo_extension($filename); $dest_file.= '.'.$tn_ext; // creation and backup of final picture if (!is_writable($tndir)) return new PwgError(WS_ERR_INVALID_PARAM, '['.$tndir.'] : '.l10n('no_write_access')); imagejpeg($destImage, $dest_file, $conf['tn_compression_level']); // freeing memory ressources imagedestroy( $srcImage ); imagedestroy( $destImage ); list($tn_width, $tn_height) = getimagesize($dest_file); $tn_size = floor(filesize($dest_file) / 1024).' KB'; $endtime = get_moment(); $info = array( 'path' => $path, 'tn_file' => $dest_file, 'tn_width' => $tn_width, 'tn_height' => $tn_height, 'tn_size' => $tn_size, 'tn_time' => number_format(($endtime - $starttime) * 1000, 2, '.', ' ').' ms'); return $info; } else { // error $err=l10n('tn_no_support'); if ( isset( $extension ) ) $err .= l10n('tn_format').' '.$extension; else $err .= l10n('tn_thisformat'); return new PwgError(WS_ERR_INVALID_PARAM, $err); } } ?>