node_images使用方法
这篇文章首发与以前老的blog中,后转发到DrupalChina,现在又转了回来。
关于图片模块,我的要求是能生成缩略图并被views提取,能按照用户id分别保存图片,上传的原图可以符合页面宽度,对上传数量、大小进行限制。
于是,image.module实在太庞大,每次上传一张图片等于发布一个node;imce不能自己生成缩略图而且还要加上[img]的过滤,之前的imagecache配合imagefield的方法不错,但是遗憾是又不能自己存入到用户id下的文件夹。找啊找,突然发现了一直仍在角落里的node_images!
1.首先到这个地址下载node_images http://drupal.org/project/node_images
2.打开node_images.module,找到
$form['node_images_thumb_resolution'] = array(
'#type' => 'textfield',
'#title' => t('Resolution for thumbnails'),
'#default_value' => variable_get('node_images_thumb_resolution', '100x100'),
'#size' => 15,
'#maxlength' => 7,
'#description' => t('The thumbnail size expressed as WIDTHxHEIGHT (e.g. 100x75).'),
);在这段的后边添加下边的代码
$form['node_images_thumb_sizemethod'] = array(
'#type' => 'select',
'#title' => t('Thumbnail Generation Method'),
'#default_value' => variable_get('node_images_thumb_sizemethod', 'fitLongestSide'),
'#description' => t('This determines the way node_images calculates the size and aspect ratio of thumbnails. <strong>Fit to width</strong> or <strong>Fit to height</strong> scale the thumbnail proportionally until it is the width or height specified above, maintaining the aspect ratio of the original and disregarding the other thumbnail dimension. Useful if all thumbnails must be a certain width but height can vary, or all must be a certain height but width can vary. <strong>Fit to longest side</strong> is the TYPICAL, DEFAULT BEHAVIOR -- it scales the whole thing down so that whichever side is longer fits into the thumbnail "box." Original aspect ratio is maintained. <strong>Fit to shortest side</strong> is similar but matches whichever side is SHORTER to its corresponding thumbnail dimension. With this, there can be some spillover so the whole thing might not fit in the thumbnail "box" dimensions. Aspect ratio maintained. <strong>Crop and fit exactly</strong> is the gem; it fits the thumbnail to be EXACTLY the size specified above, cropping the longer side (centering the image in the crop rectangle).'),
'#options' => array(
'fitToWidth' => t('Fit to width'),
'fitToHeight' => t('Fit to height'),
'fitLongestSide' => t('Fit to longest side'),
'fitShortestSide' => t('Fit to shortest side'),
'fitExactSize' => t('Crop and fit exactly')
),
);再找到
/**
* Create the thumbnail for the uploaded image.
*/
function _node_images_create_thumbnail($path, $suffix='_tn') {把这个function全部替换成下边的代码
function universal_thumbnail_calculator( $originalWidth, $originalHeight, $suggestedWidth, $suggestedHeight, $resizeMethod = 'fitLongestSide' ) {
$ratioX = $suggestedWidth/$originalWidth;
$ratioY = $suggestedHeight/$originalHeight;
$crop = FALSE;
if ($resizeMethod == 'fitExactSize') {
$resizeMethod = 'fitShortestSide';
$crop = TRUE;
}
if ($resizeMethod == 'fitLongestSide') {
$resizeMethod = ($originalWidth > $originalHeight) ? 'fitToWidth' : 'fitToHeight';
}
if ($resizeMethod == 'fitShortestSide') {
$resizeMethod = ($originalWidth < $originalHeight) ? 'fitToWidth' : 'fitToHeight';
}
switch ($resizeMethod) {
case 'fitToWidth':
$thumbnailValues = array( 'scaledX' => $suggestedWidth, 'scaledY' => round($originalHeight*$ratioX) );
break;
case 'fitToHeight':
$thumbnailValues = array( 'scaledX' => round($originalWidth*$ratioY), 'scaledY' => $suggestedHeight );
break;
}
if ($crop) {
// Calculate the offset (where to begin 'drawing' the crop rectangle) as half the pixel overflow:
$thumbnailValues['offsetX'] = round($thumbnailValues['scaledX'] - $suggestedWidth)/2;
$thumbnailValues['offsetY'] = round($thumbnailValues['scaledY'] - $suggestedHeight)/2;
// Pass along the "newly cropped" dimensions to be used by later code:
$thumbnailValues['croppedX'] = $suggestedWidth;
$thumbnailValues['croppedY'] = $suggestedHeight;
}
return $thumbnailValues;
}
/**
* Create the thumbnail for the uploaded image.
*/
function _node_images_create_thumbnail($path, $suffix='_tn') {
$size = variable_get('node_images_thumb_resolution', '100x100');
list($width, $height) = explode('x', $size);
$dest_path = preg_replace('!(\.[^/.]+?)?$!', "$suffix\\1", $path, 1);
$resizeThumbMethod = variable_get('node_images_thumb_sizemethod', 'fitLongestSide');
if ($size = getimagesize($path)) {
$currentWidth = $size[0];
$currentHeight = $size[1];
$thumbnailDimensions = universal_thumbnail_calculator( $currentWidth, $currentHeight, $width, $height, $resizeThumbMethod);
image_resize($path, $dest_path, $thumbnailDimensions['scaledX'], $thumbnailDimensions['scaledY']);
if ((isset($thumbnailDimensions['offsetX']) && ($thumbnailDimensions['offsetX']>0)) || (isset($thumbnailDimensions['offsetY']) && ($thumbnailDimensions['offsetY']>0))) {
image_crop($dest_path, $dest_path, $thumbnailDimensions['offsetX'], $thumbnailDimensions['offsetY'], $thumbnailDimensions['croppedX'], $thumbnailDimensions['croppedY']);
}
$info = image_get_info($dest_path);
$thumb = new stdClass();
$thumb->filename = basename($dest_path);
$thumb->filepath = $dest_path;
$thumb->filesize = $info['file_size'];
$thumb->filemime = $info['mime_type'];
return $thumb;
}
return NULL;
}
?>
这样做的目的稍后再讲,这个patch的原文 http://drupal.org/node/147538
3.上传激活这模块(同时还需要开启upload模块)
4.在站点配置里打开nodeimage,第一项就是可以选择存储目录uid或者username;第二项是生成缩略图的大小,比如80*80(在第二步中更改的那些代码就是为了修补这个小bug,node_images在上传一个比你设定的缩略图尺寸还小的图片时,无法生成缩略图);第三项可以选择图片上传是按照什么方式放大缩小,如适合宽度,适合高度等等(这也是第二步那个patch增加的一个功能);第四项就是图片格式;第五项是规定一个node中最多可以上传多少张图片;最后一个是md5格式的文件名。
5.还是站点配置,在文件上传中,第一个,上传图像的最大分辨率,这个数值就是node-images上传图片显示的大小。例如你再次设置500*500,即时你上传一张1280*800的图片,也会被压缩成500*500的规格,好处是避免了图片撑破页面,缺点是不适合清晰大图。
6.进入内容管理-内容类型,随便挑一个类型,比如story,打开编辑,在最下边,先看到有个Position,选择一下图片发表的位置;Link to image gallery这个可有可无,无非是点图片的时候打开一个简陋的小相册;其他都按个人喜好设置吧。
7.那么怎么发表图片呢?这个有点不方便,就是说你要在发表完一个node之后才出现一个image标签让你上传。
8.创建一个views block。在field中加上Node Images: Display node images和Node: Title(nodeimage点击缩略图不是进入node而是打开原图片。)过滤器中加上Node images: Has node images。其他看个人喜好了。
好了,大概就这些了。都是一些最基本的使用方法,还有一些可以增强node_images的功能,例如:
http://drupal.org/node/150904 支持Lightbox, Thickbox
个人认为,node_images应该是非图片站点的最佳图片解决方法,缩略图、自动压缩原图、views、按uid目录存储图片……多诱人,哈哈。
评论
发表新评论