PHP batch generation of picture thumbnails method _php Tips

Source: Internet
Author: User

The example in this article describes how PHP generates thumbnail images in batches. Share to everyone for your reference. as follows:

<?php//php batch generation picture thumbnail function mkdirs ($dirname, $mode =0777)//Create directory (directory, [mode]) {if (!is_dir ($dirname)) {Mkdirs ($dirname, $mode);
  If the directory does not exist, recursively establishes 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);
 The function Getsuffix ($filename)//Gets the filename suffix {return end (Explode (".", $filename)); Whether the function Checksuffix ($filename, $arr)//is an 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 diagram path (localized) var $allowtype =array (". gif", ". jpg", ". png", ". jpeg");//Allow The picture type var $regif = 0; Whether the thumbnail gif, 0 does not handle var $keep = 0; Whether to keep the source file (1 is reserved, 0 is MD5) var $over = 0; Whether you can overwrite the existing picture, 0 cannot overwrite the Var $dir; Image source directory var $newdir;
 Processed Table of Contents function __construct ($olddir =null, $newdir =null) {$this->dir= $olddir? $olddir: "./images/temp"; $this->newdir= $newdir?
  $newdir: "./images/s";
   The function renames ($SRC) {$MD 5file=substr ($SRC), 10,10). STRRCHR ($src, "."); After the MD5 file name (for example: 3293okoe.gif) $md 5file= $this->w. " _ ". $this->h."
   _ ". $md 5file; After processing the filename return $this->newdir. "
   /". $MD 5file;
   Save the source picture, MD5 filename to the new directory} function Mini ($SRC, $w, $h, $q =80)//Generate thumbnail Mini (picture address, width, height, quality) {$this->src= $src;
   $this->w= $w;
   $this->h= $h; if (STRRCHR ($SRC, ".")
   = = ". gif" && $this->regif==0)//whether to process gif {return $this->src;
   if ($this->keep==0)//whether to keep the source file, default does not keep {$newsrc = $this->renames ($SRC);
    {$src =str_replace ("\", "/", $SRC);
   $NEWSRC = $this-&GT;NEWDIR.STRRCHR ($src, "/");
   if (file_exists ($NEWSRC) && $this->over==0)///If already exists, return the address directly to the $NEWSRC; } if (Strstr ($SRC, "HTtp://") &&!strstr ($src, $_server[' http_host '))//If the network file, first save {$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;
   //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, $he
   IGHT_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 processed 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 (filename and structure invariant) {$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.

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.