< PHP /*************************************** * Author: Falling Dream Scorpio (beluckly) * Completion time: 2006-12-18 * Class Name: Creatminiature * Function: Generate multiple types of thumbnails * Basic parameters: $srcFile, $echoType * The parameters used in the method: $toFile, the resulting file $toW, the resulting width $toH, the resulting high $BK 1, the background color parameter is the highest of 255 $BK 2, background color parameters $BK 3, background color parameters Example Include ("thumb.php"); $CM =new creatminiature (); $CM->setvar ("1.jpg", "file"); $CM->distortion ("Dis_bei.jpg", 150,200); $CM->prorate ("Pro_bei.jpg", 150,200); $CM->cut ("Cut_bei.jpg", 150,200); $CM->backfill ("Fill_bei.jpg", 150,200); ***************************************/ Class Creatminiature { Public variables var $srcFile = ""; Original var $echoType; Output picture type, link--not saved as file; file--saved as File var $im = ""; Temp variable var $srcW = ""; Original width var $srcH = ""; Original height Setting variables and initialization function SetVar ($srcFile, $echoType) { $this->srcfile= $srcFile; $this->echotype= $echoType; $info = ""; $data = getimagesize ($this->srcfile, $info); Switch ($data [2]) { Case 1: if (!function_exists ("Imagecreatefromgif")) { echo "Your GD library cannot use GIF format pictures, please use JPEG or PNG format!" Return "; Exit (); } $this->im = imagecreatefromgif ($this->srcfile); Break Case 2: if (!function_exists ("Imagecreatefromjpeg")) { echo "Your GD library can not use JPEG format pictures, please use other format pictures!" Return "; Exit (); } $this->im = imagecreatefromjpeg ($this->srcfile); Break Case 3: $this->im = imagecreatefrompng ($this->srcfile); Break } $this->srcw=imagesx ($this->im); $this->srch=imagesy ($this->im); } To create a distorted shape thumbnail function Distortion ($toFile, $toW, $toH) { $CIMG = $this->creatimage ($this->im, $toW, $toH, 0,0,0,0, $this->SRCW, $this->srch); return $this->echoimage ($CIMG, $toFile); Imagedestroy ($CIMG); } To generate a proportionally scaled thumbnail function prorate ($toFile, $toW, $toH) { $toWH = $toW/$toH; $srcWH = $this->srcw/$this->srch; if ($toWH < = $srcWH) { $ftoW = $toW; $ftoH = $ftoW * ($this->srch/$this->SRCW); } else { $ftoH = $toH; $ftoW = $ftoH * ($this->srcw/$this->srch); } if ($this->srcw> $toW | | $this->srch> $toH) { $CIMG = $this->creatimage ($this->im, $ftoW, $ftoH, 0,0,0,0, $this->SRCW, $this->srch); return $this->echoimage ($CIMG, $toFile); Imagedestroy ($CIMG); } Else { $CIMG = $this->creatimage ($this->im, $this->SRCW, $this->srch,0,0,0,0, $this->SRCW, $this->srch); return $this->echoimage ($CIMG, $toFile); Imagedestroy ($CIMG); } } Generate the smallest cropped thumbnail function Cut ($toFile, $toW, $toH) { $toWH = $toW/$toH; $srcWH = $this->srcw/$this->srch; if ($toWH < = $srcWH) { $ctoH = $toH; $ctoW = $ctoH * ($this->srcw/$this->srch); } Else { $ctoW = $toW; $ctoH = $ctoW * ($this->srch/$this->SRCW); } $ALLIMG = $this->creatimage ($this->im, $ctoW, $ctoH, 0,0,0,0, $this->SRCW, $this->srch); $CIMG = $this->creatimage ($allImg, $toW, $toH, 0,0, ($ctoW-$toW)/2, ($ctoH-$toH)/2, $toW, $toH); return $this->echoimage ($CIMG, $toFile); Imagedestroy ($CIMG); Imagedestroy ($ALLIMG); } Create a thumbnail of the background fill function Backfill ($toFile, $toW, $toH, $BK 1=255, $BK 2=255, $BK 3=255) { $toWH = $toW/$toH; $srcWH = $this->srcw/$this->srch; if ($toWH < = $srcWH) { $ftoW = $toW; $ftoH = $ftoW * ($this->srch/$this->SRCW); } Else { $ftoH = $toH; $ftoW = $ftoH * ($this->srcw/$this->srch); } if (function_exists ("Imagecreatetruecolor")) { @ $cImg =imagecreatetruecolor ($toW, $toH); if (! $cImg) { $CIMG =imagecreate ($toW, $toH); } } Else { $CIMG =imagecreate ($toW, $toH); } $backcolor = Imagecolorallocate ($cImg, $BK 1, $BK 2, $BK 3); The background color of the fill Imagefilledrectangle ($CIMG, 0,0, $toW, $toH, $backcolor); if ($this->srcw> $toW | | $this->srch> $toH) { $PROIMG = $this->creatimage ($this->im, $ftoW, $ftoH, 0,0,0,0, $this->SRCW, $this->srch); /* if ($ftoW < $toW) { Imagecopymerge ($CIMG, $PROIMG, ($toW-$ftoW)/2,0,0,0, $ftoW, $ftoH, 100); } else if ($ftoH < $toH) { Imagecopymerge ($CIMG, $PROIMG, 0, ($toH-$ftoH)/2,0,0, $ftoW, $ftoH, 100); } */ if ($ftoW < $toW) { Imagecopy ($CIMG, $PROIMG, ($toW-$ftoW)/2,0,0,0, $ftoW, $ftoH); } else if ($ftoH < $toH) { Imagecopy ($CIMG, $PROIMG, 0, ($toH-$ftoH)/2,0,0, $ftoW, $ftoH); } Else { Imagecopy ($CIMG, $PROIMG, 0,0,0,0, $ftoW, $ftoH); } } Else { Imagecopymerge ($CIMG, $this->im, ($toW-$ftoW)/2, ($toH-$ftoH)/2,0,0, $ftoW, $ftoH, 100); } return $this->echoimage ($CIMG, $toFile); Imagedestroy ($CIMG); } function Creatimage ($img, $creatW, $creatH, $dstX, $dstY, $srcX, $srcY, $srcImgW, $srcImgH) { if (function_exists ("Imagecreatetruecolor")) { @ $creatImg = Imagecreatetruecolor ($creatW, $creatH); if ($CREATIMG) Imagecopyresampled ($CREATIMG, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH); Else { $CREATIMG =imagecreate ($creatW, $creatH); Imagecopyresized ($CREATIMG, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH); } } Else { $CREATIMG =imagecreate ($creatW, $creatH); Imagecopyresized ($CREATIMG, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH); } return $CREATIMG; } Output picture, link---only output, do not save the file. file--Save As File function Echoimage ($img, $to _file) { Switch ($this->echotype) { Case "link": if (function_exists (' imagejpeg ')) return imagejpeg ($IMG); else return imagepng ($img); Break Case "File": if (function_exists (' imagejpeg ')) return imagejpeg ($img, $to _file); else return imagepng ($img, $to _file); Break } } } ?> |