php 批量生成缩略图代码
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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using
