PHP Batch generation image thumbnail method, _php tutorial

Source: Internet
Author: User

PHP Batch generation image thumbnail method,


In this paper, we describe the method of batch generating picture thumbnails in PHP. Share to everyone for your reference. Specific as follows:

<?php//Batch generated image thumbnail function mkdirs ($dirname, $mode =0777)//Create directory (directory, [mode]) {if (!is_dir ($dirname)) {mkdirs ($dirnam E, $mode);  If the directory does not exist, recursively establish return mkdir ($dirname, $mode); } return true; } function SaveFile ($filename, $content = ")//Save file (file, [content]) {if (function_exists (file_put_contents)) {File_put_conten  TS ($filename, $content);   } else {$fp =fopen ($filename, "WB");   Fwrite ($fp, $content);  Fclose ($FP);  }} function Getsuffix ($filename)//Gets the file name suffix {return end (Explode (".", $filename)),} function Checksuffix ($filename, $arr)  is the allowable type (currently, 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 the existing picture can be overwritten, the Var $dir is not covered by 0; Image source directory var $newdir; Directory function __construct after processing ($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, "."); After MD5 file name (for example: 3293okoe.gif) $md 5file= $this->w. " _ ". $this->h."   _ ". $md 5file; After processing the file name return $this->newdir.   /". $MD 5file;   Save the source picture, MD5 file name to the 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 graph {return $this->src;    if ($this->keep==0)//whether to keep the source file, the default is not reserved {$newsrc = $this->renames ($SRC);//The file address after renaming} else//Keep the original name {    $src =str_replace ("\ \", "/", $SRC);   $NEWSRC = $this-&GT;NEWDIR.STRRCHR ($src, "/");   if (file_exists ($NEWSRC) && $this->over==0)//If it already exists, return the address {return $NEWSRC directly; } if (Strstr ($SRC, "/http") &&!strstr ($src, $_server[' http_host '))//If it is a network file, save {$src = $thi FirstS->getimg ($SRC); } $arr =getimagesize ($SRC);   Gets the Picture property $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;   }//Processing thumbnails $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; }//Generate 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; Returns the 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 thumbnail (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 "
"; echo $image->reimg ("Images/m8920.jpg", 75,75,80); echo "
"; echo $image->getimg ("./images/s/zht.jpg");? >

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1019063.html www.bkjia.com true http://www.bkjia.com/PHPjc/1019063.html techarticle php Batch generation image thumbnail method, this article describes the PHP batch generation image thumbnail method. Share to everyone for your reference. As follows: php//batch generation of images with PHP ...

  • Related Article

    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.