相关关键词
关于我们
最新文章
乐道系统增加自动缩略图功能
发布日期:2014-04-26 00:00:00
867
现有后台在添加产品图或文章图的时候,无法自动生成缩略图,上传两次,操作颇有些不方便。因此增加自动缩略图功能。
1、后台产品添加、修改模板
头部JS(产品为例)
if(i == 0){
upload += '<li><input type="text" title="" class="input rounded" name="picture_url[]" id="picture'+ i +'" size="20" value="'+ picture_url[i] +'" /></li>';
upload += '<li><iframe src="upload.php?dir=product&preview=upload_preview&return_id=picture'+ i +'" width="183" height="30" border="0" scrolling="no" frameborder="0" id="upload_'+ i +'" style="visibility:hidden"></iframe></li><li>';
}else{
upload += '<li><input type="text" title="" class="input rounded" name="picture_url[]" id="picture'+ i +'" size="20" value="'+ picture_url[i] +'" /></li>';
upload += '<li><iframe src="upload.php?dir=product&preview=upload_preview&thumb_w='+picture[0][1]+'&thumb_h='+picture[0][2]+'&return_id=picture'+ i +'&makeThumb='+ i +'" width="183" height="30" border="0" scrolling="no" frameborder="0" id="upload_'+ i +'"></iframe></li><li>';
}
if(i == 0){
upload += '<input type="checkbox" id="auto_thumb" name="auto_thumb" checked="true" value="1" onclick="handleAutoThumb(this.checked)" />自动生成商品缩略图</label> <a href="javascript:sheets(2);"><img src="../inc/templates/manage/images/operation/add_2.png" width="16" height="16" title="{:$lang['page']['add_upload']:}" /></a> ';
upload += '<a href="javascript:sheets(1);"><img src="../inc/templates/manage/images/operation/cut_2.png" width="16" height="16" title="{:$lang['page']['del_upload']:}" /></a>';
}
增加部分
function handleAutoThumb(checked)
{
if(document.getElementById('upload_0').style.visibility=='hidden'){
document.getElementById('upload_0').style.visibility='visible';
document.getElementById('upload_1').src='upload.php?dir=product&preview=upload_preview&return_id=picture1';
}else{
document.getElementById('upload_0').style.visibility='hidden';
document.getElementById('upload_1').src='upload.php?dir=product&preview=upload_preview&thumb_w='+picture[0][1]+'&thumb_h='+picture[0][2]+'&return_id=picture1&makeThumb=1';
}
}
2、上传文件
$upload->makeThumb = $_GET['makeThumb']== 1 ? true : false; //是否开启生成缩略图 乐道增加 2014.4.26
$upload->thumb_w = $_GET['thumb_w']; //缩略图宽 乐道增加 2014.4.26
$upload->thumb_h = $_GET['thumb_h']; //缩略图高 乐道增加 2014.4.26
约57行
if($upload->makeThumb){
echo 'parent.document.getElementById("picture0").value = "'.$upload->new_filename_smalls.'";'; //缩略图路径 乐道增加 2014.4.26
}
3、上传类文件
public $makeThumb = true; //是否开启生成缩略图 乐道增加 2014.4.26
public $new_filename_smalls = ''; //缩略图路径 乐道增加 2014.4.26
public $thumb_w = '150'; //缩略图宽 乐道增加 2014.4.26
public $thumb_h = '100'; //缩略图高 乐道增加 2014.4.26
增加缩略图函数
// 增加的缩略图处理函数 乐道增加 2014.4.26
// 只考虑jpg,png,gif格式
// $srcImgPath 源图象路径
// $targetImgPath 目标图象路径
// $targetW 目标图象宽度
// $targetH 目标图象高度
public function makeThumbnail($srcImgPath,$targetImgPath,$targetW,$targetH)
{
$imgSize = GetImageSize($srcImgPath);
$imgType = $imgSize[2];
//@ 使函数不向页面输出错误信息
switch ($imgType)
{
case 1:
$srcImg = @ImageCreateFromGIF($srcImgPath);
break;
case 2:
$srcImg = @ImageCreateFromJpeg($srcImgPath);
break;
case 3:
$srcImg = @ImageCreateFromPNG($srcImgPath);
break;
}
//取源图象的宽高
$srcW = ImageSX($srcImg);
$srcH = ImageSY($srcImg);
if($srcW>$targetW || $srcH>$targetH)
{
$targetX = 0;
$targetY = 0;
if ($srcW > $srcH)
{
$finaW=$targetW;
$finalH=round($srcH*$finaW/$srcW);
$targetY=floor(($targetH-$finalH)/2);
}
else
{
$finalH=$targetH;
$finaW=round($srcW*$finalH/$srcH);
$targetX=floor(($targetW-$finaW)/2);
}
//function_exists 检查函数是否已定义
//ImageCreateTrueColor 本函数需要GD2.0.1或更高版本
if(function_exists("ImageCreateTrueColor"))
{
$targetImg=ImageCreateTrueColor($targetW,$targetH);
}
else
{
$targetImg=ImageCreate($targetW,$targetH);
}
$targetX=($targetX<0)?0:$targetX;
$targetY=($targetX<0)?0:$targetY;
$targetX=($targetX>($targetW/2))?floor($targetW/2):$targetX;
$targetY=($targetY>($targetH/2))?floor($targetH/2):$targetY;
//背景白色
$white = ImageColorAllocate($targetImg, 255,255,255);
ImageFilledRectangle($targetImg,0,0,$targetW,$targetH,$white);
/*
PHP的GD扩展提供了两个函数来缩放图象:
ImageCopyResized 在所有GD版本中有效,其缩放图象的算法比较粗糙,可能会导致图象边缘的锯齿。
ImageCopyResampled 需要GD2.0.1或更高版本,其像素插值算法得到的图象边缘比较平滑,
该函数的速度比ImageCopyResized慢。
*/
if(function_exists("ImageCopyResampled"))
{
ImageCopyResampled($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
else
{
ImageCopyResized($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
}
switch ($imgType) {
case 1:
ImageGIF($targetImg,$targetImgPath);
break;
case 2:
ImageJpeg($targetImg,$targetImgPath);
break;
case 3:
ImagePNG($targetImg,$targetImgPath);
break;
}
ImageDestroy($srcImg);
ImageDestroy($targetImg);
}
else //不超出指定宽高则直接复制
{
copy($srcImgPath,$targetImgPath);
ImageDestroy($srcImg);
}
}
原来的
if(@copy($this->field['tmp_name'],$target) || (function_exists('move_uploaded_file') && @move_uploaded_file($this->field['tmp_name'],$target))){
@chmod ($target,$this->chmod_files);
后增加
if($makeThumb = $this->makeThumb){ //乐道增加 2014.4.26
$small_pic = $this -> makeThumbnail($target,$this->save_dir."small_".$this->new_filename,$this->thumb_w,$this->thumb_h);//120,90为小图宽,高
$this->new_filename_smalls = $this->save_dir."small_".$this->new_filename;
} //乐道增加 2014.4.26