php 判断上传图大小函数

 更新时间:2016年11月25日 16:05  点击:2179

//缩略图部分------------------------------------------------------------
//判断缩略图大小函数-----
    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,$smalladdrname.$name.".jpg");
        ImageDestroy ($newim);
    }else{
        ImageJpeg ($im,$smalladdrname.$name.".jpg");
    }
    }
    //生成部分
    if($_FILES['image']['size']){
    if($_FILES['image']['type'] == "image/pjpeg"){
        $im = imagecreatefromjpeg($bigaddrname.$exname);
    }elseif($_FILES['image']['type'] == "image/x-png"){
        $im = imagecreatefrompng($bigaddrname.$exname);
    }elseif($_FILES['image']['type'] == "image/gif"){
        $im = imagecreatefromgif($bigaddrname.$exname);
    }
    if($im){
        if(file_exists($smalladdrname.".jpg")){
            unlink($smalladdrname.".jpg");
        }
        ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$smalladdrname);
        ImageDestroy ($im);
    }
    }
    echo "<div align='center'><a href='javascript:window.history.back()'>上传成功</a></div>";
}
//缩略图结束-----------------------------------------------------
  
}

<?phpphp文章_分页代码
//为了避免重复包含文件而造成错误,加了判断函数是否存在的条件:
if(!function_exists(pageft)){
/
function pageft($totle,$displaypg=20,$url=''){

//定义几个全局变量:
//$page:当前页码;
//$firstcount:(数据库)查询的起始项;
//$pagenav:页面导航条代码,函数内部并没有将它输出;
//$_SERVER:读取本页URL“$_SERVER["REQUEST_URI"]”所必须。
global $page,$firstcount,$pagenav,$_SERVER;


$GLOBALS["displaypg"]=$displaypg;

if(!$page) $page=1;

//如果$url使用默认,即空值,则赋值为本页URL:
if(!$url){ $url=$_SERVER["REQUEST_URI"];}

//URL分析:
$parse_url=parse_url($url);
$url_query=$parse_url["query"]; //单独取出URL的查询字串
if($url_query){

$url_query=ereg_replace("(^|&)page=$page","",$url_query);


$url=str_replace($parse_url["query"],$url_query,$url);

//在URL后加page查询信息,但待赋值:
if($url_query) $url.="&page"; else $url.="page";
}else {
$url.="?page";
}

//页码计算:
$lastpg=ceil($totle/$displaypg); //最后页,也是总页数
$page=min($lastpg,$page);
$prepg=$page-1; //上一页
$nextpg=($page==$lastpg ? 0 : $page+1); //下一页
$firstcount=($page-1)*$displaypg;

//开始分页导航条代码:
$pagenav="显示第 <B>".($totle?($firstcount+1):0)."</B>-<B>".min($firstcount+$displaypg,$totle)."</B> 条记录,共 $totle 条记录<BR>";

//如果只有一页则跳出函数:
if($lastpg<=1) return false;

$pagenav.=" <a href='$url=1'>首页</a> ";
if($prepg) $pagenav.=" <a href='$url=$prepg'>前页</a> "; else $pagenav.=" 前页 ";
if($nextpg) $pagenav.=" <a href='$url=$nextpg'>后页</a> "; else $pagenav.=" 后页 ";
$pagenav.=" <a href='$url=$lastpg'>尾页</a> ";

//下拉跳转列表,循环列出所有页码:
$pagenav.=" 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n";
else $pagenav.="<option value='$i'>$i</option>\n";
}
$pagenav.="</select> 页,共 $lastpg 页";
}
}
?>[/quote]


[quote]
<?

$sql = "select * from 数据库名";
$conn = mysql_query($sql);
$total = mysql_num_rows($conn);
pageft($total,7);  //一页显示的记录条数
$result = mysql_query("select * from 数据库名 order by ID编号 asc limit $firstcount,$displaypg");

echo $pagenav;   //输出分页处
?>

<?php
     
      php 留言板防刷新效果
      $ip = $_SERVER['REMOTE_ADDR'];
      $conn = mysql_connect($mysql_server_name, $mysql_username, $mysql_password);
      $sql = "SELECT * FROM `my_counter` WHERE ip = '$ip'";
      mysql_select_db($mysql_database, $conn);
      $result = mysql_query($sql);
      $row = mysql_fetch_row($result);
      print_r($row);
     
      if(!$row)   //如果IP没有出现过就新增
      {
       $sql = "INSERT INTO `my_counter` ( `new`, `ip`, `time_at`) VALUES ('1', '$ip', NOW())";
       $result = mysql_query($sql);
      }
      else        //如果IP出现过
      {
       $sql = "SELECT TIME_TO_SEC( NOW( ) ) - TIME_TO_SEC( time_at ) FROM `my_counter` WHERE `ip` = '$ip'ORDER BY `TIME_TO_SEC( NOW( ) ) - TIME_TO_SEC( time_at )` ASC ";
       $result = mysql_query($sql);
       $row = mysql_fetch_row($result);
       print_r($row);
      
       if($row[0]>5)   //如果IP出现过,则看他有没有超过5秒
       {
         $sql = "INSERT INTO `my_counter` ( `new`, `ip`, `time_at`) VALUES ('1', '$ip', NOW())";
         $result = mysql_query($sql);
       }
      }
     
       $sql = "SELECT COUNT(*) FROM `my_counter`";
       $result = mysql_query($sql);
       $row = mysql_fetch_row($result);
       print_r($row);
       mysql_free_result($result);
       mysql_close($conn);
?>

php保存远程图片,我这个系统是自己写了,所以上传图片时相当麻痹了,我还用了新云的一个系统,他那编辑器支持远程保存图片功能,但那个是asp的而我的系统只支持php我想php可能比asp更容易实现这个功能吧,开始想到用file_get_contents(),然后用fopen保存file_get_content取得的内容就OK了,下面是我实现的一个小程序.

<?php
$filed="http://111cn.net/conference_php_quebec.gif";
$datad=file_get_contents($filed);
$mydr=basename($file);
fwrite(fopen($mydr,"wb+"),$data);
?>

就这简单了,但是这样我觉得没什么实现的用途所以就写了正下面的一段代码

PHP 代码:
if($_POST['savepic']=="checked"){

$img_array = array();
$content1 = stripslashes($_POST['post_content']);
if (get_magic_quotes_gpc()) $content1 = stripslashes($content1);

preg_match_all("/(src|SRC)=\"(http:\/\/(.+).(gif|jpg|jpeg|bmp|png))/isU",$content1,$img_array);//正则开始匹配所有的图片并放入数据
$img_array = array_unique(dhtmlspecialchars($img_array[2]));
 
print_r($img_array);
set_time_limit(0);  
foreach ($img_array as $key => $value) {
$get_file = file_get_contents($value);//开始获取图片了哦
$filetime = time();
$filepath = "../wp-content/uploads/pic2/".date("Y",$filetime)."/".date("m",$filetime)."/";//图片保存的路径目录
!is_dir($filepath) ? mkdirs($filepath) : null; 
$filename = date("YmdHis",$filetime).random(1).'.'.substr($value,-3,3);
$fp = @fopen($filepath.$filename,"w");
@fwrite($fp,$get_file);
fclose($fp);//完工,哈
$content1 = preg_replace("/".addcslashes($value,"/")."/isU", "/wp-content/uploads/pic2/".date("Y",$filetime)."/".date("m",$filetime)."/".$filename, $content1);  //顺便替换一下文章里面的图片地址
 

  上面的函数就实现了你编辑器里所有图片都保存了.


<?php
printbreviaryImg("image/img.gif","ss","35","22");
function PrintBreviaryImg($spathimg,$photoname, $simgwidth, $simgheight)
{
  if(file_exists($spathimg))
  {
$size=GetImageSize($spathimg);
switch ( $size[2] ){
case 1: $origImg = @ImageCreateFromGIF($spathimg);
    break;
case 2: $origImg = @imagecreatefromjpeg($spathimg);
    break;
case 3: $origImg = @ImageCreateFromPNG($spathimg);
    break;
}
//生成缩略图
$simgsize=GetImgSize($simgwidth,$simgheight,$size[0],$size[1]);
if(function_exists("imagecopyresampled"))
{
$im = imagecreatetruecolor( $simgsize['width'],$simgsize['height'] );
imagecopyresampled( $im, $origImg, 0, 0, 0, 0,$simgsize['width'],$simgsize['height'],$size[0],$size[1] );
}
else
{
$im = imagecreate( $simgwidth,$simgheight );
imagecopyresized($im, $origImg, 0, 0, 0, 0,$simgwidth,$simgwidth,$simgsize['width'],$simgsize['height'] );
}
//添加水印
$dstX = $simgsize['width'] - 10;
$dstY = $simgsize['height'] - 10;
$string="[url=http://www.gd.com.cn]www.gd.com.cn[/url]";
$clo = imagecolorallocate($im, 255, 255, 255);
imagettftext($im, 48, 0, $dstX, $dstY, $clo, '../../fonts/arialbd.ttf', $string);
//添加水印
/*
$logoImage = ImageCreateFromGif('image/img.gif');
$logoW = ImageSX($logoImage);
    $logoH = ImageSY($logoImage);
    $dstX = -($simgsize['width']-$logoW);
$dstY = -($simgsize['height']-$logoH);
imagecopymerge($im, $logoImage, 0, 0, 0, 0, $logoW, $logoH,30);
    */
Imagejpeg($im,$photoname);
  }
  else
  {
    echo "暂缺图片";
  }
}
?>
[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • JavaScript判断浏览器及其版本信息

    本篇文章主要分享了通过window.navigator来判断浏览器及其版本信息的实例代码。具有一定的参考价值,下面跟着小编一起来看下吧...2017-01-23
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • 一个奇葩的最短的 IE 版本判断JS脚本

    使用 conditional comment 来判断 IE 的版本。嗯,是早早有人提出,但没有认真看代码。昨天刚好在看 CSS3 PIE 的时候看到,觉得是不是不靠谱。今天看到 Paul Irish 也提起,那么,推荐一下吧。这是作者博客上写的:复制代码 代码...2014-05-31
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Php文件上传类class.upload.php用法示例

    本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • c# 判断是否为空然后赋值的4种实现方法

    下面小编就为大家分享一篇c# 判断是否为空然后赋值的4种实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-25
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • jQuery实现简单的文件上传进度条效果

    本文实例讲述了jQuery实现文件上传进度条效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下:<!DOCTYPE html><html><head><meta charset="utf-8"><title>upload</title><link rel="stylesheet...2015-11-24
  • php文件上传你必须知道的几点

    本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28