php生成缩略图实用程序

 更新时间:2016年11月25日 15:57  点击:1742

<?php教程
class resizeimage
{
    //图片类型
    var $type;
    //实际宽度
    var $width;
    //实际高度
    var $height;
    //改变后的宽度
    var $resize_width;
    //改变后的高度
    var $resize_height;
    //是否裁图
    var $cut;
    //源图象
    var $srcimg;
    //目标图象地址
    var $dstimg;
    //临时创建的图象
    var $im;
    function resizeimage($img, $wid, $hei,$c,$dstpath)
    {
        $this->srcimg = $img;
        $this->resize_width = $wid;
        $this->resize_height = $hei;
        $this->cut = $c;
        //图片的类型
        $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
        //初始化图象
        $this->initi_img();
        //目标图象地址
        $this -> dst_img($dstpath);
        //--
        $this->width = imagesx($this->im);
        $this->height = imagesy($this->im);
        //生成图象
        $this->newimg();
        ImageDestroy ($this->im);
    }
    function newimg()
    {
        //改变后的图象的比例
        $resize_ratio = ($this->resize_width)/($this->resize_height);
        //实际图象的比例
        $ratio = ($this->width)/($this->height);
        if(($this->cut)=="1")
        //裁图
        {
            if($ratio>=$resize_ratio)
            //高度优先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            //宽度优先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
        else
        //不裁图
        {
            if($ratio>=$resize_ratio)
            {
                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_ratio)
            {
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
    }
    //初始化图象
    function initi_img()
    {
        if($this->type=="jpg")
        {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if($this->type=="gif")
        {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if($this->type=="png")
        {
            $this->im = imagecreatefrompng($this->srcimg);
        }
    }
    //图象目标地址
    function dst_img($dstpath)
    {
        $full_length  = strlen($this->srcimg);
        $type_length  = strlen($this->type);
        $name_length  = $full_length-$type_length;
        $name         = substr($this->srcimg,0,$name_length-1);
        $this->dstimg = $dstpath;
//echo $this->dstimg;
    }
}
$resizeimage = new resizeimage("1.jpg", "200", "150", "1","2.jpg");
?>

修改dedecms 织梦系统 生成静态页面栏目缓存文件路径
由于dedecms 生成频道栏目,要生成一个临时mkall_cache_{adminid}.php教程文件,这对网站做安全会有一定的影响,特别我现在是把我整个网站限制不能上传php,js文件,为了方便起见,今天我就来拿我修改过程记录下来与各位分享吧。
首页我们找到dedecms  的后台管理上当默认是在dede/目录。

找到文件:
makehtml_all.php
找到73行,

//$mkcachefile = DEDEROOT."/data/mkall_cache_{$adminID}.php";
改成你的站外目录如
$mkcachefile = "s:/s/data/mkall_cache_{$adminID}.php";

这是要写缓存文件的,就是把你更新的目录ID保存到一个数组里

再找到147行

//$mkcachefile = DEDEDATA."/mkall_cache_{$adminID}.php";
$mkcachefile = "s:/s/data/mkall_cache_{$adminID}.php";

好了,保存文件,我们再在当前上当找到

makehtml_list_action.php

找到第37行
//$mkcachefile = DEDEROOT."/data/mkall_cache_{$adminID}.php";
$mkcachefile = "s:/s/data/mkall_cache_{$adminID}.php";

保存上传就OK了。

本文章原创于www.111cn.net 转载注明出处

传值方法很多,
参数传值: 可以是urs.php?id=1带参数形式,这是页面之间比较主要的传值方式 ,用request,get 接收值.
from表单传值 : 主要接收request传值 post, get  来接收,from标签可以选择get或者post 通过url传值的是get 还可以利用ajax传值可以选择post或者get.

session传值 : 这个一般是做用户登陆时用的,服务器全局变量,一般不用在页面之前的传值
cookie传值 :把内容保存在客户端,

我们常用的页面传值主要是参数传值  from表单传值传值。

现在来做几个实例
*/
//先用页面参数传值实例

?>

<a href="b.php?action=1">点击我</a>
//输出值为 1

用form传值

<form action="b.php" method="get"> 

<input name="action" type="text" value="5">

<input type="submit" value="submit" /> 

</form>

得出值 5

<form action="b.php" method="POST"> 

<input name="action" type="text" value="5">

<input type="submit" value="submit" /> 

</form>


<?
if( $_GET )
{
 echo 'get传值',$_GET['action'];
}
else
{
 echo 'post传值', $_POST['action'];
}
//本文章原创于www.111cn.net 中国WEB第一站,,转载注明出处

if( $_POST )
{

 $str = '23=12,34  78=1,3 45=12,46,78,89=33'; 

 $content=nl2br($str); 

 $content=str_replace(" ","",$content); 

 $arr=explode("<br/>",$content); 

 $result=array(); 

 foreach ($arr as $value) 

 { 

$k=explode("=",$value); 

 $result[]=array($k[0]=>$k[1]); 

 } 

 //数组转换成字串 

 function arrayeval($array, $level = 0) { 

  $space = ''; 

  for($i = 0; $i <= $level; $i++) { 

  $space .= " "; 

  } 

 $evaluate = "Array $space( "; 

  $comma = $space; 

  foreach($array as $key => $val) { 

   $key = is_string($key) ? '''.addcslashes($key, ''\').''' : $key; 

   $val = !is_array($val) && (!preg_match("/^-?d+$/", $val) || strlen($val) > 12 || substr($val, 0, 1)=='0') ? '''.addcslashes($val, ''\').''' : $val; 

   if(is_array($val)) { 

    $evaluate .= "$comma$key => ".arrayeval($val, $level + 1); 

  } else { 

    $evaluate .= "$comma$key => $val"; 

   } 

   $comma = ", $space"; 

  } 

 $evaluate .= " $space)"; 

  return $evaluate; 

 } 

//把结果写到文件 

 $config=arrayeval($result); 

 $strwrite="<?php ".'$'.'shuzu'.'='.$config." ?>"; 

 $fp=fopen('config.php','w'); 

 fwrite($fp,$strwrite); 

 fclose($fp); 
 }

 ?>

<form action="b.php?action=5" method="get"> 

<textarea name="content"></textarea> 

<input type="submit" value="submit" /> 

</form>

缓存的工作原理其实并不复杂。它的核心思想是:首先,我们将需要显示的内容存储在一个文本文件(即缓存文件)之中。然后,如果有用户请求某个页面的内容,我们首先检查此页对应的缓存(即那个文本文件)是否存在——如果存在且为最新的缓存文件,那么直接将这个文本文件中的内容输出到客户端供用户查看;如果此页对应的缓存文件不存在或缓存生成的时间不符合要求(太旧),那么直接执行一次此页对应的PHP文件,并将显示内容存储在缓存文件中。重复上述流程。这样一来,虽然增加了PHP代码,但我们最大程度的节省了PHP链接到数据库教程再提取数据的时间。

*/

   //导入缓存类
   require_once('cache.class.php教程');
   //创建一个缓存类对象CacheManager
   $CacheManager = new Cache();
   //调用startCache方法,表示开始缓存
   $CacheManager->startCache();
   //以下区块所有echo内容都将作为缓存写入缓存文件中
   echo "Start Cache example at: ".date('H:i:s')."<br/>";
   sleep(2);
   echo "End Cache example at: ".date('H:i:s')."<br/>";
  
   echo "<br/><a href='clear.php'>Clean the cache</a><br/>";
   //以上区块所有echo内容都将作为缓存写入缓存文件中
   $CacheManager->endCache();


//cache.class.php代码

class Cache {
   var $status    = true;     // 值为True表示开启缓存;值False表示关闭缓存功能。
   var $cacheDir  = 'cache/'; //存放缓存文件的默认目录
   var $cacheFile = '';       //缓存文件的真实文件名
   var $timeOut   = 1000;     // 内容被重复使用的最长时限
   var $startTime = 0;        // 程序执行的开始时间
   var $caching     = true;     // 是否需要对内容进行缓存;值为False表示直接读取缓存文件内容
  
   function getMicroTime() {
           list($usec, $sec) = explode(" ",microtime());
          return ((float)$usec + (float)$sec);
    }

function startCache(){
      $this->startTime = $this->getMicroTime();
      ob_start();
      if ($this->status){
         $this->cacheFile = $this->cacheDir.urlencode( $_SERVER['REQUEST_URI'] );
         if ( (file_exists($this->cacheFile)) &&
              ((fileatime($this->cacheFile) + $this->timeOut) > time()) )
         {
            //从缓存文件中读取内容
            $handle = fopen($this->cacheFile , "r");
            $html   = fread($handle,filesize($this->cacheFile));
            fclose($handle);
         
            // 显示内容
            echo $html;

            // 显示程序执行时间          
           echo '<p>Total time: '
                 .round(($this->getMicroTime())-($this->startTime),4).'</p>';
           
            //退出程序
            exit();
          }
          else
          {
             //置缓存标志caching为true
             $this->caching = true;
          }
      }
    }

function endCache(){
      if ($this->status){
         if ( $this->caching )
         {
            //首先输出页面内容,然后将输出内容保存在缓存文件中
            $html = ob_get_clean();
            echo $html;
            $handle = fopen($this->cacheFile, 'w' );
            fwrite ($handle, $html );
            fclose ($handle);

            //显示页面执行时间           
            echo '<p>Total time: '.round(($this->getMicroTime()-$this->startTime),4).'</p>';
         }
      }      
    }

function cleanCache(){
       if ($handle = opendir($this->cacheDir)) {
          while (false !== ($file = readdir($handle))) {
             if (is_file($this->cacheDir.$file)) unlink($this->cacheDir.$file);
          }

          closedir($handle);      
       }
    }

}


// 再一个简单的php  缓存文件实例代码


if ($content = $cache->start($cache_id))
{
 echo $content;
 exit();
}

require_once 'Cache/Function.php';
 

 $cacheDir = './pear_cache/';
 $cache = new Cache_Function('file',array('cache_dir' => $cacheDir));
 $arr = array('东方', '南方','西方');
 $cache->call('slowFunction', $arr);
 echo '<BR>';

 $arr = array('东方', '南方','西方');

 slowFunction($arr);

  function slowFunction($arr = null)
 {
  echo "一个执行起来很慢的函数 :( <br>";
  echo "当前时间是 " . date('M-d-Y H:i:s A', time()) . '<br>';
  foreach ($arr as $fruit)
  {
   echo "太阳此时正在 $fruit <br>";
  }
 )

[!--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