-
- function Mkdirs ($dirname, $mode =0777)//Create directory (directory, [mode])
- {
- if (!is_dir ($dirname))
- {
- Mkdirs ($dirname, $mode); If the directory does not exist, recursive builds
- return mkdir ($dirname, $mode);
- }
- return true;
- }
- function SaveFile ($filename, $content = ")//Save 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 file name suffix
- {
- Return End (Explode (".", $filename));
- }
- function Checksuffix ($filename, $arr)//is the allowable 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 picture types
- var $regif = 0; Whether the thumbnail gif, for 0 does not handle
- var $keep = 0; Whether to keep the source file (1 is reserved, 0 is MD5)
- var $over = 0; Whether an existing picture can be overwritten, 0 is not overwritten
- var $dir; Image source Directory
- var $newdir; The directory after processing
- function __construct ($olddir =null, $newdir =null)
- {
- $this->dir= $olddir? $olddir: "./images/temp";
- $this->newdir= $newdir? $newdir: "./images/s";
- }
- function renames ($SRC)
- {
- $MD 5FILE=SUBSTR (MD5 ($SRC), 10,10). STRRCHR ($src, "."); MD5 file name (for example: 3293okoe.gif)
- $MD 5file= $this->w. " _ ". $this->h." _ ". $md 5file; File name after processing
- return $this->newdir. " /". $MD 5file; Save the source picture, 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 graphs
- {
- return $this->src;
- }
- if ($this->keep==0)//whether to keep the source file, default does not retain
- {
- $NEWSRC = $this->renames ($SRC); The file address after renaming
- }
- else//Maintain original
- {
- $src =str_replace ("\ \", "/", $SRC);
- $NEWSRC = $this->NEWDIR.STRRCHR ($src, "/");
- }
- if (file_exists ($NEWSRC) && $this->over==0)//If already present, return address directly
- {
- return $NEWSRC;
- }
- if (Strstr ($SRC, "/http") &&!strstr ($src, $_server[' http_host '))//If it is a network file, save it first
- {
- $SRC = $this->getimg ($SRC);
- }
- $arr =getimagesize ($SRC); Get Picture Properties
- $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;
- }
- Working with thumbnail images
- $nim =imagecreatetruecolor ($w, $h);
- $k 1=round ($h/$w, 2);
- $k 2=round ($height/$width, 2);
- if ($k 1< $k 2)
- {
- $width _a= $width;
- $height _a=round ($width * $k 1);
- $SW = 0;
- $sh = ($height-$height _a)/2;
- }
- Else
- {
- $width _a= $height/$k 1;
- $height _a= $height;
- $SW = ($width-$width _a)/2;
- $sh = 0;
- }
- Create a picture
- 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 Picture
- {
- 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 to post-processing path
- }
- function Getimg ($filename)
- {
- $MD 5file= $this->dir. " /". SUBSTR (MD5 ($filename), 10,10). STRRCHR ($filename,". ");
- if (file_exists ($MD 5file))
- {
- return $MD 5file;
- }
- Start getting the file and return to the new path
- $img =file_get_contents ($filename);
- if ($img)
- {
- if (!is_dir ($this->dir))
- {
- mkdir ($this->dir);
- }
- SaveFile ($md 5file, $img);
- return $MD 5file;
- }
- }
- function reimg ($SRC, $w, $h, $q)//Convert thumbnails (file name and structure unchanged)
- {
- $this->keep=1;
- return $this->mini ($SRC, $w, $h, $q); Address generated by return
- }
- }
- $image =new image ();
- echo $image->reimg ("Images/zht.jpg", 75,75,80);
- echo "
";
- echo $image->reimg ("Images/m8920.jpg", 75,75,80);
- echo "
";
- echo $image->getimg ("./images/s/zht.jpg");
- ?>
Copy Code
|