php简单实用生成缩略图代码

 更新时间:2016年11月25日 15:54  点击:1712
本程序是根据用户上传图片后再把上传的图片按比例生成缩略图
 代码如下 复制代码

<?
$w?$resizewidth=$w:$resizewidth=400;// 生成图片的宽度
$h?$resizeheight=$h:$resizeheight=400;// 生成图片的高度
function resizeimage($im,$maxwidth,$maxheight,$name){
    $width = imagesx($im);
    $height = imagesy($im);
    if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
        if($maxwidth && $width > $maxwidth){
            $widthratio = $maxwidth/$width;
            $resizewidth=true;
        }
        if($maxheight && $height > $maxheight){
            $heightratio = $maxheight/$height;
            $resizeheight=true;
        }
        if($resizewidth && $resizeheight){
            if($widthratio < $heightratio){
                $ratio = $widthratio;
            }else{
                $ratio = $heightratio;
            }
        }elseif($resizewidth){
            $ratio = $widthratio;
        }elseif($resizeheight){
            $ratio = $heightratio;
        }
        $newwidth = $width * $ratio;
        $newheight = $height * $ratio;
        if(function_exists("imagecopyresampled")){
              $newim = imagecreatetruecolor($newwidth, $newheight);
              imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }else{
            $newim = imagecreate($newwidth, $newheight);
              imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }
        imagejpeg ($newim,$name);
        imagedestroy ($newim);
    }else{
        imagejpeg ($im,$name);
    }
}

if($_files['uploadfile']['size']){
    if($_files['uploadfile']['type'] == "image/pjpeg"){
        $im = imagecreatefromjpeg($_files['uploadfile']['tmp_name']);
    }elseif($_files['uploadfile']['type'] == "image/x-png"){
        $im = imagecreatefrompng($_files['uploadfile']['tmp_name']);
    }elseif($_files['uploadfile']['type'] == "image/gif"){
        $im = imagecreatefromgif($_files['uploadfile']['tmp_name']);
    }
    if($im){
        if(file_exists('bbs.jpg')){
            unlink('bbs.jpg');
        }
        resizeimage($im,$resizewidth,$resizeheight,'bbs.jpg');
        imagedestroy ($im);
  
    }
}
//$uploadfile="www.111cn.net.jpg";
?>

date_default_timezone_set('prc');
/**
* 求取从某日起经过一定天数后的日期,
* 排除周六周日和节假日
* @param $start       开始日期
* @param $offset      经过天数
* @param $exception 例外的节假日
* @param $allow       允许的日期(预留参数)
* @return
*  examples:输入(2010-06-25,5,''),得到2010-07-02
*/
function getendday( $start='now', $offset=0, $exception='', $allow='' ){
    //先计算不排除周六周日及节假日的结果
    $starttime = strtotime($start);
    $endtime = $starttime + $offset * 24 * 3600;
    $end = date('y-m-d', $endtime);
    //然后计算周六周日引起的偏移
    $weekday = date('n', $starttime);//得到星期值:1-7
    $remain = $offset % 7;
    $newoffset = 2 * ($offset - $remain) / 7;//每一周需重新计算两天
    if( $remain > 0 ){//周余凑整
        $tmp = $weekday + $remain;
        if( $tmp >= 7 ){
            $newoffset += 2;
        }else if( $tmp == 6 ){
            $newoffset += 1;
        }
        //考虑当前为周六周日的情况
        if( $weekday == 6 ){
            $newoffset -= 1;
        }else if( $weekday == 7 ){
            $newoffset -= 2;
        }
    }
    //再计算节假日引起的偏移
    if( is_array($exception) ){//多个节假日
        foreach ($exception as $day){
            $tmp_time = strtotime($day);
            if( $tmp_time>$starttime && $tmp_time<=$endtime ){//在范围(a,b]内
                $weekday_t = date('n', $tmp_time);
                if($weekday_t <= 5){//防止节假日与周末重复
                    $newoffset += 1;
                }
            }
        }
    }else{//单个节假日
        if( $exception!='' ){
            $tmp_time = strtotime($exception);
            if( $tmp_time>$starttime && $tmp_time<=$endtime ){
                $weekday_t = date('n', $tmp_time);
                if($weekday_t <= 5){
                    $newoffset += 1;
                }
            }
        }
       
    }
    //根据偏移天数,递归做等价运算111cn.net
    if($newoffset > 0){
        #echo "[{$start} -> {$offset}] = [{$end} -> {$newoffset}]"."<br /> ";
        return getendday($end,$newoffset,$exception,$allow);
    }else{
        return $end;
    }
}
/**
* 暴力循环方法
*/
function getendday2( $start='now', $offset=0, $exception='', $allow='' ){
    $starttime = strtotime($start);
    $tmptime = $starttime + 24*3600;
   
    while( $offset > 0 ){
        $weekday = date('n', $tmptime);
        $tmpday = date('y-m-d', $tmptime);
        $bfd = false;//是否节假日
        if(is_array($exception)){
            $bfd = in_array($tmpday,$exception);
        }else{
            $bfd = ($exception==$tmpday);
        }
        if( $weekday<=5 && !$bfd){//不是周末和节假日
            $offset--;
            #echo "tmpday={$tmpday}"."<br />";
        }
        $tmptime += 24*3600;
    }
   
    return $tmpday;
}
$exception = array(
    '2010-01-01','2010-01-02','2010-01-03',
    '2010-04-03','2010-04-04','2010-04-05',
    '2010-05-01','2010-05-02','2010-05-03',
    '2010-06-14','2010-06-15','2010-06-16',
    '2010-09-22','2010-09-23','2010-09-24',
    '2010-10-01','2010-10-02','2010-10-03','2010-10-04',
    '2010-10-05','2010-10-06','2010-10-07',
   
);
//echo getendday('2010-08-27',3,'');
//echo getendday('2010-06-25',15,'2010-07-07');
$t1 = microtime();
echo getendday('2010-05-12',66,$exception)."<br />";
$t2 = microtime();echo "use ".($t2-$t1)." s <br />";
echo getendday2('2010-05-12',66,$exception)."<br />";
$t3 = microtime();echo "use ".($t3-$t2)." s <br />";

<?php教程
if($_files['file']){
 // ----------------------------------------------------------------------------------------------//
//
// 说明:文件上传   日期:2004-5-2
//
// ----------------------------------------------------------------------------------------------//

php简单实用文件上传代码
 // 上传设置
 $maxsize=10002400;            //最大允许上传的文件大小
 $alltype=array(".php",".php3");         //所有允许上传的文件类型
 $imgtype=array(".php",".php3");               //类型

 // 判断文件大小
 if($_files['file']['size']>$maxsize)  {
     echo "您上传的资料大于10000k";
     exit;
 }
 
 // 判断文件类型
 $type=strstr($_files['file']['name'],".");
 if(in_array($type,$alltype)){
     echo "不允许上传该类型的文件";
     exit;
 }
 include './uploaddir.php';
 $time=date("ymd-his",time());
 $fn=$time.$type;
 $destination=$updir."/".$fn;
 if(@move_uploaded_file($_files['file']['tmp_name'], $destination)){
         @chmod($destination, 0777);
   $fileurl=$updir."/".$destination;
         $fileurl="".$destination;
          
 }else{
    echo "上传失败!";
    echo "<script>location.href=history.back()</script>";
 }
// ----------------------------------------------------------------------------------------------//
}
if($back=="no"):
 echo "ok";
 exit;
endif;
?>

<?php教程


 $dbhost = 'localhost';    // 数据库教程服务器
 $dbuser = 'root';     // 数据库用户名
 $dbpw = 'qwaszx';             // 数据库密码
 $dbname = 'movie';  // 数据库名
 $adminemail = www.111cn.net@111cn.net; // 系统管理员 email


    $database = 'mysql教程';  // 不能修改此处
 $tplrefresh = 1;  // 模板自动刷新开关 0=关闭, 1=打开
 $pconnect = 0;   // 数据库连接方式 0=connect, 1=pconnect

 

// ============================================================================

class db_class {
 var $querynum = 0;
 //function dbstuff() { global $fp; $fp = fopen("./tempdata/dblog.txt", "w"); }

 function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
  if($pconnect) {
   if(!@mysql_pconnect($dbhost, $dbuser, $dbpw)) {
    $this->halt('can not connect to mysql server');
   }
  } else {
   if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
    $this->halt('can not connect to mysql server');
   }
  }
  mysql_query("set names 'gb2312'");
 }

 function select_db($dbname) {
  return mysql_select_db($dbname);
 }

 function fetch_array($query, $result_type = mysql_assoc){


  $query = mysql_fetch_array($query, $result_type);

  return $query;
 }

 function query($sql, $silence = 0) {
  //echo "|$sql|<br>"; //debug
  //@fwrite($globals[fp], $sql." "); //debug
  $query = mysql_query($sql);
        //echo 'query:'.$query.'<br>';
     if(!$query && !$silence)
        {
         $this->halt('mysql query error', $sql);
  }
        $this->querynum++;
  return $query;
 }

<?php教程
$bbsconn=mysql教程_connect("localhost","root","");
mysql_select_db("lsmsql",$bbsconn);
$kind="幻灯片";
$sql="select * from sa_article where kind='$kind' order by articleid desc limit 0,6";
$result=mysql_query($sql);
$pics=$links=$texts="";
    while ($rows=mysql_fetch_array($result)){

  $pics.="/attached/".$rows['source']."|";

  $links.="show.php?id=".$rows['articleid']."|";

  $texts.=$rows['title']."|";

}
$pics=substr($pics,0,-1);

$links=substr($links,0,-1);

$texts=substr($texts,0,-1);
?>


<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>幻灯片</title>
</head>
<body>
<table width="300" border="1">
  <tr>
    <td>
<script type=text/网页特效>
<!--

var focus_width=300;
var focus_height=200;
var text_height=20;
var swf_height = focus_height+text_height
var pics="<?=$pics?>"
var links="<?=$links?>"
var texts="<?=$texts?>"
document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+ focus_width +'" height="'+ swf_height +'">');
document.write('<param name="allowscriptaccess" value="samedomain"><param name="movie" value="playswf.swf"><param name=wmode value=transparent><param name="quality" value="high">');
document.write('<param name="menu" value="false"><param name=wmode value="opaque">');
document.write('<param name="flashvars" value="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'">');
document.write('<embed src="playswf.swf" wmode="opaque" flashvars="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'" menu="false" bgcolor="#dadada" quality="high" width="'+ focus_width +'" height="'+ swf_height +'" allowscriptaccess="samedomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');  document.write('</object>');
//-->
</script>

    </td>
  </tr>
</table>
</body>
</html>

[!--infotagslink--]

相关文章

  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php生成唯一数字id的方法汇总

    关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
  • jQuery为动态生成的select元素添加事件的方法

    下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
  • PHP自动生成后台导航网址的最佳方法

    经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
  • js生成随机数的方法实例

    js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
  • c#生成高清缩略图的二个示例分享

    这篇文章主要介绍了c#生成高清缩略图的二个示例,需要的朋友可以参考下...2020-06-25
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • PHP生成不同颜色、不同大小的tag标签函数

    复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
  • JS生成某个范围的随机数【四种情况详解】

    下面小编就为大家带来一篇JS生成某个范围的随机数【四种情况详解】。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧...2016-04-22
  • php中利用str_pad函数生成数字递增形式的产品编号

    解决办法:$str=”QB”.str_pad(($maxid[0]["max(id)"]+1),5,”0″,STR_PAD_LEFT ); 其中$maxid[0]["max(id)"]+1) 是利用max函数从数据库中找也ID最大的一个值, ID为主键,不会重复。 str_pad() 函数把字符串填充为指...2013-10-04
  • C#生成Word文档代码示例

    这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下...2020-06-25
  • Vue组件文档生成工具库的方法

    本文主要介绍了Vue组件文档生成工具库的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-11
  • PHP简单实现生成txt文件到指定目录的方法

    这篇文章主要介绍了PHP简单实现生成txt文件到指定目录的方法,简单对比分析了PHP中fwrite及file_put_contents等函数的使用方法,需要的朋友可以参考下...2016-04-28
  • PHP批量生成图片缩略图(1/5)

    这款批量生成缩略图代码可以生成指定大小的小图哦,并且支持文件批量上传。 这款教程会用到php文件 view.php config.php funs.php index.php 功能: -------...2016-11-25
  • C#实现为一张大尺寸图片创建缩略图的方法

    这篇文章主要介绍了C#实现为一张大尺寸图片创建缩略图的方法,涉及C#创建缩略图的相关图片操作技巧,需要的朋友可以参考下...2020-06-25
  • 史上最简洁C# 生成条形码图片思路及示例分享

    这篇文章主要介绍了史上最简洁C# 生成条形码图片思路及示例分享,需要的朋友可以参考下...2020-06-25
  • php 上传文件并生成缩略图代码

    if( isset($_FILES['upImg']) ) { if( $userGroup[$loginArr['group']]['upload'] == 0 ) { echo '{"error":"您所在的用户组无权上传图片!"}'; } else...2016-11-25
  • 简单入门级php 生成xml文档代码

    $doc = new domdocument('1.0'); // we want a nice output $doc->formatoutput = true; 代码如下 复制代码 $root = $doc->createelement('bo...2016-11-25