img

来源:互联网 发布:日韩明星壁纸软件 编辑:程序博客网 时间:2024/04/29 12:55
<?php/** * Created by PhpStorm. * User: aoshi * Date: 2017/6/27 * Time: 16:50 *//* * author aoshi * cutImages        存放图片的目录 * */class cutImage {    public $dir;    function __construct($dir='') {        $this->dir = $dir;    }    /*     * * #按照比例裁剪 暂时没有用到     * $src     字符串     * $scale    宽/高     * */    function cutByScale($src,$scale) {        //创建源图的实例        $fileStr = file_get_contents($src);        $src = imagecreatefromstring($fileStr);        $s_x = imagesx($src);        $s_y = imagesy($src);        //裁剪开始区域左上角的点的坐标        $s_scale = $s_x / $s_y;        if($scale >= $s_scale) {            $t_x = $s_x;            $t_y = $t_x * (1 / $scale);        } else {            $t_y = $s_y;                //真实高度            $t_x = $s_y * $scale;       //真实宽度        }        $x = 0;     //起始X点        $y = 0;     //起始Y点        //压缩代码//        if(($scale > 1/3) && ($scale < 3) && ($t_x > 500)) {//            if($scale > 1) {//                $t_x_cut_down = 400;//                $t_y_cut_down = $t_x_cut_down * $scale;//                $t_y_cut_down = ceil($t_y_cut_down);//            } else {//                $t_y_cut_down = 700;//                $t_x_cut_down = $t_y_cut_down * (1 / $scale);//                $t_x_cut_down = ceil($t_x_cut_down);//            }//        } else {//            $t_x_cut_down = ceil($t_x);//            $t_y_cut_down = ceil($t_y);//        }        //将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升        $new_image = imagecreatetruecolor($t_x, $t_y);        imagecopyresampled($new_image, $src, $x, $y, $x, $y, $t_x, $t_y, $t_x, $t_y);        //输出图片        //header('Content-Type: image/jpeg');        $fileDir = $this->dir;        $filePath = '/cutImage/' . date('Ymd'). '/';        $filePathinfo = $fileDir . $filePath;        if(!is_dir($filePathinfo)) {            mkdir($filePathinfo,0777,true);        }        $fileName = time().'.jpg';        $fileCreatePathinfo = $filePathinfo . $fileName;        imagejpeg ($dim,$fileCreatePathinfo);        return $filePath . $fileName;        //imagejpeg($new_image);        imagedestroy($src);        imagedestroy($new_image);        return $file_path;    }    /*     * 无损裁剪 根据比例算出尺寸     * $src     字符串     * $scale    宽/高     * */    function cutByScaleToPx($src,$scale) {        $fileStr = file_get_contents($src);        $src = imagecreatefromstring($fileStr);        $s_x = imagesx($src);        $s_y = imagesy($src);        //裁剪开始区域左上角的点的坐标        $s_scale = $s_x / $s_y;        if($scale >= $s_scale) {            $t_x = $s_x;            $t_y = $t_x * (1 / $scale);            $t_y = ceil($t_y);        } else {            $t_y = $s_y;                //真实高度            $t_x = $s_y * $scale;       //真实宽度            $t_x = ceil($t_x);        }        if($s_scale > 1){            //图片宽大于高            $sx = abs(($s_y-$s_x)/2);            $sy = 0;            $thumbw = $s_y;            $thumbh = $s_y;        } else {            //图片高大于等于宽            $sy = abs(($s_x-$s_y)/2.5);            $sx = 0;            $thumbw = $s_x;            $thumbh = $s_x;        }        if(function_exists("imagecreatetruecolor")) {            $dim = imagecreatetruecolor($t_x, $t_y); // 创建目标图gd2        } else {            $dim = imagecreate($t_x, $t_y); // 创建目标图gd1        }        imageCopyreSampled ($dim,$src,0,0,$sx,$sy,$t_x,$t_y,$thumbw,$thumbh);        $fileDir = $this->dir;        $filePath = 'cutImage/' . date('Ymd'). '/';        $filePathinfo = $fileDir . $filePath;        if(!is_dir($filePathinfo)) {            mkdir($filePathinfo,0777,true);        }        $fileName = time().'.jpg';        $fileCreatePathinfo = $filePathinfo . $fileName;        imagejpeg ($dim,$fileCreatePathinfo);        imagedestroy($src);        imagedestroy($dim);        return $filePath . $fileName;    }    /*     * 无损裁剪 根据尺寸裁剪     * $src         图片地址     * $t_x        宽大小     * $t_y        高大小     * */    function cutByPx($src,$t_x,$t_y) {        $fileStr = file_get_contents($src);        $src = imagecreatefromstring($fileStr);        $s_x = imagesx($src);        $s_y = imagesy($src);        if($s_x > $s_y){            //图片宽大于高            $sx = abs(($s_y-$s_x)/2);            $sy = 0;            $thumbw = $s_y;            $thumbh = $s_y;        } else {            //图片高大于等于宽            $sy = abs(($s_x-$s_y)/2.5);            $sx = 0;            $thumbw = $s_x;            $thumbh = $s_x;        }        if(function_exists("imagecreatetruecolor")) {            $dim = imagecreatetruecolor($t_x, $t_y); // 创建目标图gd2        } else {            $dim = imagecreate($t_x, $t_y); // 创建目标图gd1        }        imageCopyreSampled ($dim,$src,0,0,$sx,$sy,$t_x,$t_y,$thumbw,$thumbh);        $fileDir = $this->dir;        $filePath = 'cutImage/' . date('Ymd'). '/';        $filePathinfo = $fileDir . $filePath;        if(!is_dir($filePathinfo)) {            mkdir($filePathinfo,0777,true);        }        $fileName = time() . getRandChar(6) .'.jpg';        $fileCreatePathinfo = $filePathinfo . $fileName;        imagejpeg ($dim,$fileCreatePathinfo);        imagedestroy($src);        imagedestroy($dim);        return $filePath . $fileName;    }    /*     * 缩放图片     *     * desription 压缩图片     * @param sting $imgsrc 图片路径     * */    function zoomImg($imgsrc,$minWidth = 720,$minHeight = 720) {        list($width,$height,$type)=getimagesize($imgsrc);        //根据比例判断是否是长图 宽图        $scale = $width / $height;        if($scale > 2) {            //宽图            $width = $height;        } else if($scale < 0.5) {       //长图            $height = $width;        }        if($width > $height) {      //            if($width > $minWidth) {                $new_width = $minWidth;                $new_height = $new_width * $height / $width;                $new_height = ceil($new_height);            } else {                $new_width = $width;                $new_height = $height;            }        } else {            if($height > $minHeight) {                $new_height = $minHeight;                $new_width = $new_height * $width / $height;                $new_width = ceil($new_width);            } else {                $new_width = $width;                $new_height = $height;            }        }        switch($type){            case 1:                $giftype=$this->check_gifcartoon($imgsrc);                if($giftype){                    // header('Content-Type:image/gif');                    $image_wp=imagecreatetruecolor($new_width, $new_height);                    $image = imagecreatefromgif($imgsrc);                }                break;            case 2:                // header('Content-Type:image/jpeg');                $image_wp=imagecreatetruecolor($new_width, $new_height);                $image = imagecreatefromjpeg($imgsrc);                break;            case 3:                // header('Content-Type:image/png');                $image_wp=imagecreatetruecolor($new_width, $new_height);                $image = imagecreatefrompng($imgsrc);                break;            default:                return false;                break;        }        imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);        $fileDir = $this->dir;        $filePath = 'cutImage/' . date('Ymd'). '/';        $filePathinfo = $fileDir . $filePath;        if(!is_dir($filePathinfo)) {            mkdir($filePathinfo,0777,true);        }        $fileName = time() . getRandChar(6) .'.jpg';        $fileCreatePathinfo = $filePathinfo . $fileName;        //75代表的是质量、压缩图片容量大小        imagejpeg($image_wp, $fileCreatePathinfo,50);        imagedestroy($image_wp);        $data = array('filePath'=>$filePath,'fileName'=>$fileName,'fileSrc'=>$filePath . $fileName);        return $data;    }    /**     * desription 判断是否gif动画     * @param sting $image_file图片路径     * @return boolean t 是 f 否     */    function check_gifcartoon($image_file){        $fp = fopen($image_file,'rb');        $image_head = fread($fp,1024);        fclose($fp);        return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;    }}
原创粉丝点击