PHP简单生成缩略图相册的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP简单生成缩略图相册的方法。分享给大家供大家参考。具体如下:

<?php
/*
 * written by mot
 * 根目录下自己新建image thumb目录
 * */
class thumb{
  private $src;
  private $source;
  private $s_width;
  private $s_height;
  private $dest;
  private $d_height;
  private $d_width;
  private $name;
  public function thumb($image_path,$rate = 0.5){
    $this->src = $image_path;
    $this->source = imagecreatefromjpeg($image_path);
    $s_size = getimagesize($image_path);
    $this->s_height = $s_size[1];
    $this->s_width = $s_size[0];
    $this->d_height = 100;
    $this->d_width = 100;
    $this->dest = imagecreate($this->d_width, $this->d_height);
    $this->name = explode('.jpg', $image_path);
    $this->name = $this->name[0];
  }
  public function make(){
    imagecopyresized($this->dest, $this->source, 0, 0, 0, 0, $this->d_width, $this->d_height,
    $this->s_width, $this->s_height);
    $thumb = str_replace('image', 'thumb', $this->name.'-thumb.jpg');
    imagejpeg($this->dest,$thumb,100);
    $img = $thumb;
    echo "<a href=$this->src><img src=$img></a>";
  }
}
$hl = opendir(".\\image\\");
while(false != $file = readdir($hl)){
  if($file == '.' || $file == '..') continue;
  $path = '.\\image\\'.$file;
  $tmp = new thumb($path,0.3);
  $tmp->make();
}

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP设计模式 注册表模式(多个类的注册)

以前我也写过一个注册表类,不过那一个不能进行多个类的注册,下面用数组对类进行了存储。 复制代码 代码如下: <?php //基础类 class webSite {//一个非常简单的...

php实现文件下载更能介绍

PHP用代码实现文件下载,阅读PHP用代码实现文件下载,我们一般实现下载都是调用url来下载,但是遇到ie能识别打开的文件就不能用这种方式了,比如下载一个图片、html网页等,这时就需要...

PHP 组件化编程技巧

PHP 组件化编程技巧

但其在UI方便却有些力不从心,不仅是PHP,任何一种Web编程语言在设计UI都有类似的问题,宿主语言与HTML混和在一个文件中,大量重复的 HTML代码,毫无任何技术含量,但又非常的费时...

PHP中使用SimpleXML检查XML文件结构实例

利用 SimpleXML 去检查 XML 结构是否符合规格,为了让这个程序可以多用途,采用了一个基准文件的作为结构准则,依据里面定义的节点跟属性,去检查文件是否符合基本要求的格式。 复制...

PHP中常用的转义函数

1. addslashesaddslashes对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如...