PHP Batch generation thumbnail code

Source: Internet
Author: User
Tags explode md5 mkdir save file

PHP Batch generation thumbnail code

Create a table of contents (directory, [mode])
function Mkdirs ($l 1, $l 2 = 0777) {
if (!is_dir ($l 1)) {
If the directory does not exist, recursion is established
Mkdirs (DirName ($l 1), $l 2);
Return @mkdir ($l 1, $l 2);
}
return true;
}
Save file (file, [content])
function SaveFile ($l 1, $l 2 = ') {
if (function_exists (file_put_contents)) {
File_put_contents ($l 1, $l 2);
} else{
$fp = @fopen ($l 1, ' WB ');
@fwrite ($FP, $l 2);
Fclose ($FP);
}
}
Get file suffix (file)
function Getfix ($l 1) {
Return End (Explode ('. ', $l 1));
}
is the allowable type (current, allowed)
function Checkfix ($l 1, $l 2) {
if (!is_array ($l 2)) {
$l 2 = Explode (', ', Str_replace (', ', $l 2));
}
Return In_array ($l 1, $l 2)? 1:0;
}
Class image{
Source Address
var $src;
New diagram path (after localization)
var $newsrc;

Allowed diagram types
var $allowtype = array ('. jpg ', '. gif ', '. png ', '. jpeg ');
Whether the thumbnail gif, 0 does not handle
var $regif = 0;
Whether to keep the source file (1 is reserved, 0 is MD5)
var $keep = 0;
If you can overwrite a picture that already exists, 0 is not covered
var $over = 0;

Picture Source Directory
var $dir;
Processed Table of Contents
var $newdir;

function __construct ($l 1=null, $l 2=null) {
$this->dir = $l 1? $l 1: "./images/temp";
$this->newdir = $l 2? $l 2: "./images/s";
}

function image ($l 1=null, $l 2=null) {
$this->__construct ($l 1, $l 2);
}

/**
* The file path after processing
* Save the source picture, MD5 file name to the new directory
*
* @ param rename ()
* The filename after the @ return process
*/
function ReName ($SRC) {
MD5 filename (for example: 3293okoe.gif)
$l 1 = substr (MD5 ($SRC), 10,10). STRRCHR ($src, ".");
File name after processing
$l 1 = $this->w. ' _ ' $this->h. ' _ '. $l 1;
Returns the address after processing
return $this->newdir. ' /'. $l 1;
}


/**
* Generate thumbnails
*
* @ param Mini (picture address, width, height, quality)
* If you want to keep the original file name, set $o->keep = 1;
* The image address after the @ return processing
*/
function Mini ($SRC, $w, $h, $q =80) {
$this->src = $src;
$this->w = $w;
$this->h = $h;

Whether to work with GIF images
if (STRRCHR ($SRC, ".") = = ". gif" && $this->regif = = 0) {
return $this->src;
}

Whether to keep the original file name, default does not hold
if (! $this->keep) {
Address of the renamed file
$NEWSRC = $this->rename ($SRC);
} else {
Keep it formerly known
$SRC = str_replace (' \ ', '/', $SRC);
$NEWSRC = $this->NEWDIR.STRRCHR ($src, "/");
}

If it already exists, return the address directly
if (file_exists ($NEWSRC) && $this->over = = 0) {
return $NEWSRC;
}

If it is a network file, save it first
if (Strstr ($src, "http://") &&!strstr ($src, $_server[' Http_host ')) {
$SRC = $this->getimg ($SRC);
}
Get Picture Properties
$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;
}
Working with thumbnails
$nim = Imagecreatetruecolor ($w, $h);
$k 1 = round ($h/$w, 2);
$k 2 = round ($oh/$ow, 2);
if ($k 1< $k 2) {
$oow = $ow;
$ooh = Round ($ow * $k 1);
$SW = 0;
$sh = ($oh-$ooh)/2;
}
else {
$oow = $oh/$k 1;
$ooh = $oh;
$SW = ($ow-$oow)/2;
$sh = 0;
}
Generate pictures
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);
}
Whether the processed directory exists
if (!is_dir ($this->newdir)) {
@mkdir ($this->newdir);
}
Save picture
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;
}
Returns the path after processing
return $NEWSRC;
}

/**
* Save the network picture
*
* @ param getimg (source map)
* This address (the MD5 file name of the local directory) after it is saved
*/
function getimg ($l 1) {
$l 2 = $this->dir. substr (MD5 ($l 1), 10,10). STRRCHR ($l 1, ".");
File exists, return address directly
if (File_exists ($l 2)) {
echo "Exits ...";
return $l 2;
}

Start getting the file and return to the new path
$img = file_get_contents ($l 1);
if ($img) {
if (!is_dir ($this->dir)) {
@mkdir ($this->dir);
}
SaveFile ($l 2, $img);
echo "File_get";
return $l 2;
}
}

/**
* Convert thumbnails (filename and structure unchanged)
*
* @ param Mini (source address, width, height, quality)
* Address generated by @ return
*/

function reimg ($SRC, $w, $h, $q =80) {
$this->keep = 1;
return $this->mini ($SRC, $w, $h, $q);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.