php 封装gd 库

使用sae 版 thinkphp 在本地和sae 上无法实现缩略图,缩略图主要使用在瀑布流上面,不然一张图片2 3 M,速度太慢,就自己封装了一个,见笑!

<?php
class Thumb{
    // 设置最大宽度,用来在编辑器中使用和显示
    private  $max_width  = null;
    private  $file_name  = null;
    private  $water_name = null;
    //获得文件名和图片宽度
    public function __construct($max_widht,$file_name,$water_name) {
        $this->max_width  = $max_widht;
        $this->file_name  = $file_name;
        $this->water_name = $water_name;
    }
    public function create_image(){
        // 获得ori图片信息
        list($width,$height,$type) = getimagesize($this->file_name);      
        // 当原有图片大于 要求的最大宽度时,才需要进行压缩
        if($width > $this->max_width){
            // 获得图片压缩百分比
            $per = $this->max_width / $width;
            $new_width = $width * $per;
            $new_height = $height * $per;          
        }else{
            $new_height =  $height;
            $new_width  =  $width;
        }
        //创建一个真彩色图像
        $image_p = imagecreatetruecolor($new_width, $new_height -10);

        $image = $this->image_obj($type,  $this->file_name); 

        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        $this->image_dump($type, $image_p, $this->file_name);
        $this->water();
    }

    /*
     * 生成为图片添加水印
     */
    private function water(){
        $water_name = $this->water_name;
        $dist_name = $this->file_name;
        list($dist_width,$dist_height,$type) = getimagesize($dist_name);
        $dist_im   = $this->image_obj($type, $this->file_name);
        $water_name = "D:/xampps/htdocs/buyingfeiblog/1/App/Modules/Admin/Tpl/Public/Images/water.png";
        list($w_width,$w_height) = getimagesize($water_name); // 获得图片水印信息

        $water_src =  imagecreatefrompng($water_name);

        // 设置图片水印位置 在右下角
        $x = ($dist_width - $w_width) / 4 * 3 ;
        $y =($dist_height - $w_height) /4 * 3 ;
        if(imagecopy($dist_im, $water_src, $x, $y, 0, 0, $w_width, $w_height)){
            imagepng($dist_im,$dist_name);
            echo "success";
        }else{
            echo "error";
        }
    }
    // 生成图片类型,生成不同图片 保持图片原本类型不发生变化
    private function image_dump($type,$image_p,$filename){
         switch ($type){
            case 1:
                imagegif($image_p, $filename);
                $dis_im = imagecreatefromgif(file_name);
                break;
            case 2:
                imagejpeg($image_p,  $this->file_name);
                $dis_im = imagecreatefromjpeg(file_name); 
                break;
            case 3:
                imagepng($image_p,file_name);
                $dis_im = imagecreatefrompng(file_name);
                break;
            default :
        }
    }
    // 根据图片不同,生成不同资源对象
    private function image_obj($type,$filename){
         switch ($type){
//          1 = GIF,2 = JPG,3 = PNG,
            case 1:
               $image =   imagecreatefromgif($filename);
              break;
            case 2:
               $image =   imagecreatefromjpeg($filename);
              break;
            case 3:
               $image =   imagecreatefrompng($filename);
               break;
            default :
        }
        return $image;
    }
}
    $thumb = new  Thumb(725,"D:/xampps/htdocs/test/test.jpg");
    $thumb->create_image();//create_image
?>

就是这么简单,
主要包括生成真彩图,
创建图形对象资源
图片进行合并,ok!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。