php 批量生成缩略图代码
//创建目录(目录, [模式])
function mkdirs($l1, $l2 = 0777){
if(!is_dir($l1)){
//如果目录不存在,递归建立
mkdirs(dirname($l1), $l2);
return @mkdir($l1, $l2);
}
return true;
}
//保存文件(文件, [内容])
function savefile($l1, $l2=''){
if(function_exists(file_put_contents)){
file_put_contents($l1, $l2);
} else{
$fp = @fopen($l1, 'wb');
@fwrite($fp, $l2);
fclose($fp);
}
}
//获取文件后缀(文件)
function getfix($l1){
return end(explode('.', $l1));
}
//是否为允许类型(当前, 允许)
function checkfix($l1, $l2){
if(!is_array($l2)){
$l2 = explode(',', str_replace(' ', '', $l2));
}
return in_array($l1, $l2) ? 1 : 0;
}
class image{
//源地址
var $src;
//新图路径(本地化后)
var $newsrc;
//允许的图类型
var $allowtype = array('.jpg','.gif','.png','.jpeg');
//是否缩略GIF, 为0不处理
var $regif = 0;
//是否保留源文件(1为保留, 0为MD5)
var $keep = 0;
//是否可以覆盖已存在的图片,为0则不可覆盖
var $over = 0;
//图片源目录
var $dir;
//处理后的目录
var $newdir;
function __construct($l1=null, $l2=null){
$this->dir = $l1 ? $l1 : "./images/temp";
$this->newdir = $l2 ? $l2 : "./images/s";
}
function image($l1=null, $l2=null){
$this->__construct($l1, $l2);
}
/**
* 处理后的文件路径
* 将源图片,MD5文件名后保存到新的目录里
*
* @ param rename()
* @ return 处理后的文件名
*/
function reName($src){
//MD5后文件名(例如:3293okoe.gif)
$l1 = substr(md5($src),10,10).strrchr($src,".");
//处理后文件名
$l1 = $this->w.'_'.$this->h.'_'.$l1;
//返回处理后的地址
return $this->newdir.'/'.$l1;
}
/**
* 生成缩略图
*
* @ param Mini(图片地址, 宽度, 高度, 质量)
* 如果要保留原来的文件名,请设置 $o->keep = 1;
* @ return 处理后的图片地址
*/
function Mini($src,$w,$h, $q=80){
$this->src = $src;
$this->w = $w;
$this->h = $h;
//是否处理GIF图
if(strrchr($src, ".") == ".gif" && $this->regif == 0){
return $this->src;
}
//是否保留原文件名,默认不保留
if(!$this->keep){
//改名后的文件地址
$newsrc = $this->reName($src);
} else {
//保持原名
$src = str_replace('\', '/', $src);
$newsrc = $this->newdir.strrchr($src, "/");
}
//如果已存在,直接返回地址
if(file_exists($newsrc) && $this->over == 0){
return $newsrc;
}
//如果是网络文件,先保存
if(strstr($src, "http://") && !strstr($src, $_SERVER['HTTP_HOST'])){
$src = $this->getimg($src);
}
//获取图片属性
$arr = getimagesize($src);
$ow = $arr[0];
$oh = $arr[1];
$ot = $arr[2];
switch($ot){
case 1:
$im = imagecreatefromgif($src);
break;
case 2:
$im = imagecreatefromjpeg($src);
break;
case 3:
$im = imagecreatefrompng($src);
break;
default:
return 0;
}
//处理缩略图
$nim = imagecreatetruecolor($w,$h);
$k1 = round($h/$w,2);
$k2 = round($oh/$ow,2);
if ($k1
$oow = $ow;
$ooh = round($ow*$k1);
$sw = 0;
$sh = ($oh-$ooh)/2;
}
else {
$oow = $oh/$k1;
$ooh = $oh;
$sw = ($ow-$oow)/2;
$sh = 0;
}
//生成图片
if(function_exists(imagecopyresampled)){
imagecopyresampled($nim,$im,0,0,$sw,$sh,$w,$h,$oow,$ooh);
}
else {
imagecopyresized($nim,$im,0,0,$sw,$sh,$w,$h,$oow,$ooh);
}
//处理后的目录是否存在
if(!is_dir($this->newdir)){
@mkdir($this->newdir);
}
//保存图片
switch($ot){
case 1:
$rs = imagegif($nim,$newsrc);
break;
case 2:
$rs = imagejpeg($nim,$newsrc,$q);
break;
case 3:
$rs = imagepng($nim,$newsrc);
break;
default:
return 0;
}
//返回处理后路径
return $newsrc;
}
/**
* 保存网络图片
*
* @ param getImg(源图)
* @ return 保存后的本地址址(本地目录的MD5文件名)
*/
function getimg($l1){
$l2 = $this->dir.'/'.substr(md5($l1),10,10).strrchr($l1,".");
//文件存在,直接返回地址
if(file_exists($l2)){
//echo "exits...";
return $l2;
}
//开始获取文件,并返回新路径
$img = file_get_contents($l1);
if($img){
if(!is_dir($this->dir)){
@mkdir($this->dir);
}
savefile($l2, $img);
//echo "file_get..";
return $l2;
}
}
/**
* 转换缩略图(文件名和结构不变)
*
* @ param mini(源地址, 宽度, 高度, 质量 )
* @ return 生成的地址
*/
function reImg($src, $w, $h, $q=80){
$this->keep = 1;
return $this->Mini($src, $w, $h, $q);
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software