【phpcms实现上传图片同时自动生成不同大小的缩略图】PHPCMS实现上传图片同时自动生成不同大小的缩略图

更新时间:2019-05-08    来源:其他相关    手机版     字体:

【www.bbyears.com--其他相关】

解决方案:

①在模型添加字段里面添加三个图片字段,分别是thumb(这个字段貌似默认就有,有就不用添加),thumb2,thumb3,第一个字段thumb设置图片宽高160*160,其他字段填不填都无所谓。

②在content模块的content控制器的add方法(modules/content/content.php)添加如下代码:

 代码如下

//手机游戏平台站点上传缩略图,自动生成75X75,48X48的缩略图
$siteid=$this->siteid;//获取当前站点siteid
if ($siteid==3) {
 $rawpic=isset($_POST["info"]["thumb"])?$_POST["info"]["thumb"]:"";
 //如果上传了第一张缩略图
 if ($rawpic) {
  $new75_75=thumb($rawpic,75,75);
  $new48_48=thumb($rawpic,48,48);
 }
 $_POST["info"]["thumb2"]=$new75_75;
 $_POST["info"]["thumb3"]=$new48_48;
}

因为PHPCMS的全局函数global.func.php包含了缩略图函数,因此上面的代码我们可以直接使用这个thumb函数:

 代码如下

function thumb($imgurl, $width = 100, $height = 100 ,$autocut = 1, $smallpic = "nopic.gif") {
 global $image;
 $upload_url = pc_base::load_config("system","upload_url");
 $upload_path = pc_base::load_config("system","upload_path");
 if(empty($imgurl)) return IMG_PATH.$smallpic;
 $imgurl_replace= str_replace($upload_url, "", $imgurl);
 if(!extension_loaded("gd") || strpos($imgurl_replace, "://")) return $imgurl;
 if(!file_exists($upload_path.$imgurl_replace)) return IMG_PATH.$smallpic;
 
 list($width_t, $height_t, $type, $attr) = getimagesize($upload_path.$imgurl_replace);
 if($width>=$width_t || $height>=$height_t) return $imgurl;
 www.111cn.net
 $newimgurl = dirname($imgurl_replace)."/thumb_".$width."_".$height."_".basename($imgurl_replace);
 
 if(file_exists($upload_path.$newimgurl)) return $upload_url.$newimgurl;
 
 if(!is_object($image)) {
  pc_base::load_sys_class("image","","0");
  $image = new image(1,0);
 }
 return $image->thumb($upload_path.$imgurl_replace, $upload_path.$newimgurl, $width, $height, "", $autocut) ? $upload_url.$newimgurl : $imgurl;
}

这样就把生成的75*75和48*48的两张图片的路径存入了数据库表中对应的字段。同理在edit方法里复制这段代码。

③另外,为了在添加内容的时候方便,不显示thumb2和thumb3的上传缩略图的标签,因为它是函数自动生成的,而且也无需编辑手动上传,因此把它给隐藏掉比较好。在content控制器对应的模板content_add.tpl.php和content_edit.tpl.php的底部,有一段jquery包含的代码中添加:

 代码如下

/*
 * 手机平台站点隐藏缩略图2和缩略图3
 */

$("#thumb2,#thumb3").parent().hide();
$("#thumb2,#thumb3").parent().prev().hide();

这样发布文章的编辑器就隐藏其他两个缩略图字段了。

本文来源:http://www.bbyears.com/asp/50008.html

热门标签

更多>>

本类排行