123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- /*
- * Image handle class
- * by lijg 20181031
- */
- class ImageLib{
- public function __construct(){
- //TODO
- }
-
- public function setImageType($imgPath='', $type=''){
- if(empty($imgPath) || empty($type)){
- return false;
- }
-
- $im = null;
- switch($type){
- case 1:
- $im = imagecreatefromgif($imgPath);
- break;
- case 2:
- $im = imagecreatefromjpeg($imgPath);
- break;
- case 3:
- $im = imagecreatefrompng($imgPath);
- break;
- default:
- return false;
- }
-
- return $im;
- }
-
- public function exportImage($imgPath='', $dim, $suf='thumb'){
- if(empty($imgPath) || empty($dim)){
- return false;
- }
-
- $imgInfo = getimagesize($imgPath);
- $path = pathinfo($imgPath);
- $imgName = "{$path['dirname']}/{$suf}/{$path['filename']}_{$suf}.{$path['extension']}";
- switch($imgInfo[2]){
- case 1:
- imagegif($dim, $imgName);
- break;
- case 2:
- imagejpeg($dim, $imgName);
- break;
- case 3:
- imagepng($dim, $imgName);
- break;
- default:
- return false;
- }
- return $imgName;
- }
-
- public function scaleImage($imgPath='', $setWidth='100', $setHeight='100', $suf ='thumb'){
- if(empty($imgPath)){
- return false;
- }
-
- $imgInfo = getimagesize($imgPath);
- $width = $imgInfo[0];
- $height = $imgInfo[1];
- $rate = ($setWidth/$width)>($setHeight/$height)?$setHeight/$height:$setWidth/$width;
- $newWidth = floor($width*$rate);
- $newHeight = floor($height*$rate);
-
- $sim = $this->setImageType($imgPath, $imgInfo[2]);
- $dim = imagecreatetruecolor($newWidth, $newHeight);
-
- imagecopyresampled($dim, $sim, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
- $image = $this->exportImage($imgPath, $dim, $suf);
-
- imagedestroy($sim);
- imagedestroy($dim);
- return $image;
- }
-
-
-
- public function __destruct(){
- //TODO
- }
- }
|