This article illustrates how to generate thumbnail images in batches in PHP. Share to everyone for your reference. as follows:
<?php
/ / Use PHP to generate image thumbnails in batches
Function mkdirs($dirname,$mode=0777)
/ / Create a directory (directory, [mode])
{
If(!is_dir($dirname))
{
Mkdirs($dirname,$mode); //If the directory does not exist, recursively build
Return mkdir($dirname,$mode);
}
Return true;
}
Function savefile($filename,$content='')
/ / Save the file (file, [content])
{
If(function_exists(file_put_contents))
{
File_put_contents($filename,$content);
}
Else
{
$fp=fopen($filename,"wb");
Fwrite($fp,$content);
Fclose($fp);
}
}
Function getsuffix($filename) //Get the file name suffix
{
Return end(explode(".",$filename));
}
Function checksuffix($filename,$arr) //whether it is allowed type (current, allowed)
{
If(!is_array($arr))
{
$arr=explode(",",str_replace(" ","",$arr));
}
Return in_array($filename,$arr) ? 1 : 0;
}
Class image
{
Var $src; //source address
Var $newsrc; / / new map path (after localization)
Var $allowtype=array(".gif",".jpg",".png",".jpeg"); //Allowed image types
Var $regif=0; //whether abbreviated GIF, 0 is not processed
Var $keep=0; // Whether to keep the source file (1 is reserved, 0 is MD5)
Var $over=0; //Can you overwrite an existing image, 0 is not overwritten
Var $dir; //image source directory
Var $newdir; // processed directory
Function __construct($olddir=null,$newdir=null)
{
$this->dir=$olddir ? $olddir : "./images/temp";
$this->newdir=$newdir ? $newdir : "./images/s";
}
Function reNames($src)
{
$md5file=substr(md5($src),10,10).strrchr($src,".");
/ / MD5 file name (for example: 3293okoe.gif)
$md5file=$this->w."_".$this->h."_".$md5file;
/ / File name after processing
Return $this->newdir."/".$md5file;
/ / Save the source image, MD5 file name to a new directory
}
Function Mini($src,$w,$h,$q=80)
//Generate thumbnail Mini (image address, width, height, quality)
{
$this->src=$src;
$this->w=$w;
$this->h=$h;
If(strrchr($src,".")==".gif" && $this->regif==0)
/ / Whether to process GIF map
{
Return $this->src;
}
If($this->keep==0) // Whether to keep the source file, it is not retained by default
{
$newsrc=$this->reNames($src); //Renamed file address
}
Else // keep the original name
{
$src=str_replace("\\","/",$src);
$newsrc=$this->newdir.strrchr($src,"/");
}
If(file_exists($newsrc) && $this->over==0)
/ / If it already exists, return directly to the address
{
Return $newsrc;
}
If(strstr($src,"http://") && !strstr($src,$_SERVER['HTTP_HOST']))
/ / If it is a network file, save first
{
$src=$this->getimg($src);
}
$arr=getimagesize($src); //Get image attributes
$width=$arr[0];
$height=$arr[1];
$type=$arr[2];
Switch($type)
{
Case 1: //1 = GIF,
$im=imagecreatefromgif($src);
Break;
Case 2: //2 = JPG
$im=imagecreatefromjpeg($src);
Break;
Case 3: //3 = PNG
$im=imagecreatefrompng($src);
Break;
Default:
Return 0;
}
/ / Handle thumbnails
$nim=imagecreatetruecolor($w,$h);
$k1=round($h/$w,2);
$k2=round($height/$width,2);
If($k1<$k2)
{
$width_a=$width;
$height_a=round($width*$k1);
$sw=0;
$sh=($height-$height_a)/2;
}
Else
{
$width_a=$height/$k1;
$height_a=$height;
$sw=($width-$width_a)/2;
$sh = 0;
}
//Generate image
If(function_exists(imagecopyresampled))
{
Imagecopyresampled($nim,$im,0,0,$sw,$sh,$w,$h,$width_a,$height_a);
}
Else
{
Imagecopyresized($nim,$im,0,0,$sw,$sh,$w,$h,$width_a,$height_a);
}
If(!is_dir($this->newdir))
{
Mkdir($this->newdir);
}
Switch($type) //Save the image
{
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; / / return the processed path
}
Function getimg($filename)
{
$md5file=$this->dir."/".substr(md5($filename),10,10).strrchr($filename,".");
If(file_exists($md5file))
{
Return $md5file;
}
/ / Start to get the file and return a new path
$img=file_get_contents($filename);
If($img)
{
If(!is_dir($this->dir))
{
Mkdir($this->dir);
}
Savefile($md5file,$img);
Return $md5file;
}
}
Function reImg($src,$w,$h,$q)
/ / Convert thumbnails (file name and structure unchanged)
{
$this->keep=1;
Return $this->Mini($src,$w,$h,$q);
//return generated address
}
}
$image=new image();
Echo $image->reImg("images/zht.jpg",75,75,80);
Echo "<br/>";
Echo $image->reImg("images/m8920.jpg",75,75,80);
Echo "<br/>";
Echo $image->getimg("./images/s/zht.jpg");
?>
I hope this article will help you with your PHP programming.