PHP操作图片

<?php
defined("BASEPATH") OR exit("不能越文件请求");
class TestPhoto extends Oss_controller{

    #画布的基本使用
    public function test01()
    {
        // 1.创建画布资源
        $width = 400; //px
        $height = 400;
        $image = imagecreatetruecolor($width,$height);
        // 2.在画布中产生一个颜色
        $color = imagecolorallocate($image,255,255,255);
        //3.将颜色填充到画布中
        imagefill($image,0,0,$color);
        //4.输出画布
//        header('Content-Type:image/jpeg');
//        imageJpeg($image);//输出图片
        //4. 保存图片
        $path = APPPATH.'views/images/2.jpg';
        imagejpeg($image,$path);
        //5. 销毁生成的图片资源
        imagedestroy($image);
    }

    #在画布上写字
    public function writefont()
    {
        // 1.创建画布资源
        $width = 400;
        $height = 400;
        $image = imagecreatetruecolor($width,$height);
        // 2.在画布中产生一个颜色,并填充到画布
        $color = imagecolorallocate($image,255,255,255);
        imagefill($image,0,0,$color);

        //3.写字
        #(1)引入字体
        $font = APPPATH.'views/font/Deng.ttf';
        #(3)写字的笔
        $content = "赵锐庆";
        $red = imagecolorallocate($image,255,0,0);
        imagettftext($image,18,0,10,30,$red,$font,$content);
        /*
         *$image 指的是画布
         * 18  是指大小
         * 0   是旋转角度
         * 10  是字体的x坐标
         * 30  是字体的Y坐标
         * $font 是指字体文件
         * $contetn 是指文案内容
         * */
        //4.输出画布
//        header('Content-Type:image/jpeg');
//        imageJpeg($image);//输出图片
        //4. 保存图片
        $path = APPPATH.'views/images/3.jpg';
        imagejpeg($image,$path);
        //5. 销毁生成的图片资源
        imagedestroy($image);
    }

    #在画布上画点
    public function writepoint()
    {
        // 1.创建画布资源
        $width = 400;
        $height = 400;
        $image = imagecreatetruecolor($width,$height);
        // 2.在画布中产生一个颜色,并填充到画布
        $color = imagecolorallocate($image,255,255,255);
        imagefill($image,0,0,$color);
        //3.写字
        #(1)引入字体
        $font = APPPATH.'views/font/Deng.ttf';
        #(3)写字的笔
        $content = "赵锐庆";
        $red = imagecolorallocate($image,255,0,0);
        imagettftext($image,18,0,10,30,$red,$font,$content);
        // 4. 画点
        $yellow = imagecolorallocate($image,255,0,0);
        imagesetpixel($image,200,200,$yellow);
        /*
         *$image 是画布
         * 200   x轴
         * 200   y轴
         * $yellow  颜色
         * */

        //4.输出画布
        header('Content-Type:image/jpeg');
        imageJpeg($image);//输出图片
        //4. 保存图片
//        $path = APPPATH.'views/images/3.jpg';
//        imagejpeg($image,$path);
        //5. 销毁生成的图片资源
        imagedestroy($image);
    }

    #在画布上画线
    public function writeline()
    {
        // 1.创建画布资源
        $width = 400;
        $height = 400;
        $image = imagecreatetruecolor($width,$height);
        // 2.在画布中产生一个颜色,并填充到画布
        $color = imagecolorallocate($image,255,255,255);
        imagefill($image,0,0,$color);
        //3.写字
        #(1)引入字体
        $font = APPPATH.'views/font/Deng.ttf';
        #(3)写字的笔
        $content = "赵锐庆";
        $red = imagecolorallocate($image,255,0,0);
        imagettftext($image,18,0,10,30,$red,$font,$content);
        // 4. 画点
        $yellow = imagecolorallocate($image,255,0,0);
        imagesetpixel($image,200,200,$yellow);
        /*
         *$image 是画布
         * 200   x轴
         * 200   y轴
         * $yellow  颜色
         * */
        // 5.画线条
        $x = 30;
        $y = 30;
        $x1 = 300;
        $y1 = 300;
        $green = imagecolorallocate($image,0,255,0);
        imageline($image,$x,$y,$x1,$y1,$green);
        /*
         *$image 是画布
         * $x   x轴起点
         * $y   y轴起点
         * $x1  x轴末点
         * $y1  y轴末点
         * $green  颜色
         * */

        //4.输出画布
        header('Content-Type:image/jpeg');
        imageJpeg($image);//输出图片
        //4. 保存图片
//        $path = APPPATH.'views/images/3.jpg';
//        imagejpeg($image,$path);
        //5. 销毁生成的图片资源
        imagedestroy($image);
    }

    #在画布上画正方形
    public function writesquare()
    {
        // 1.创建画布资源
        $width = 400;
        $height = 400;
        $image = imagecreatetruecolor($width,$height);
        // 2.在画布中产生一个颜色,并填充到画布
        $color = imagecolorallocate($image,255,255,255);
        imagefill($image,0,0,$color);
        //3.写字
        #(1)引入字体
        $font = APPPATH.'views/font/Deng.ttf';
        #(3)写字的笔
        $content = "赵锐庆";
        $red = imagecolorallocate($image,255,0,0);
        imagettftext($image,18,0,10,30,$red,$font,$content);
        // 4. 画点
        $yellow = imagecolorallocate($image,255,0,0);
        imagesetpixel($image,200,200,$yellow);
        /*
         *$image 是画布
         * 200   x轴
         * 200   y轴
         * $yellow  颜色
         * */
        // 5.画线条
        $x = 30;
        $y = 30;
        $x1 = 300;
        $y1 = 300;
        $green = imagecolorallocate($image,0,255,0);
        imageline($image,$x,$y,$x1,$y1,$green);
        /*
         *$image 是画布
         * $x   x轴起点
         * $y   y轴起点
         * $x1  x轴末点
         * $y1  y轴末点
         * $green  颜色
         * */

        //6. 画线条
        //画矩形只需要知道对角线的2个坐标就可以了
        imagerectangle($image,$x,$y,$x1,$y1,$green);
        //给矩形填充背景色
        $green2 = imagecolorallocate($image,0,255,255);
        imagefilledrectangle($image,$x,$y,$x1,$y1,$green2);

        //4.输出画布
        header('Content-Type:image/jpeg');
        imageJpeg($image);//输出图片
        //4. 保存图片
//        $path = APPPATH.'views/images/3.jpg';
//        imagejpeg($image,$path);
        //5. 销毁生成的图片资源
        imagedestroy($image);
    }

    #图片缩略图
    public function generateThumbnails(){
        //加载图片资源
        $path = APPPATH.'views/images/ceshi.jpg';
        #获取图片的信息
        $imageinfo = getimagesize($path);
                /*
                 Array
                (
                    [0] => 1440
                    [1] => 900
                    [2] => 2
                    [3] => width="1440" height="900"
                    [bits] => 8
                    [channels] => 3
                    [mime] => image/jpeg
                )
                 */
        //1.将图片转成source资源
        $oldimage = imagecreatefromjpeg($path);

        //2.生成画布 后面就是缩略图的大小
        #可以使用固定宽高,但是容易变形,我们使用等比,手动计算
        $width = 200;
        $rate = $imageinfo[1]/$imageinfo[0];
        $height = $rate*$width;
        $newImg = imagecreatetruecolor($width,$height);

        //3.生成缩略图
        /*
         方法参数介绍
            形参1:$newImg 新的画布资源
            形参2:$oldImage 需要被缩略图的原图的资源文件
            形参3:$dst_x   画布起始点x坐标
            形参4:$dst_y   画布起始点y坐标
            形参5:$src_x   源的x坐标轴
            形参6:$src_y   源的y坐标轴
            形参7:$dst_w   画布的所显示宽度x轴
            形参8:$dst_h   画布的所显示宽度y轴
            形参9:$src_w    源图像的宽度
            形参10:$src_h    源图像的高度
         */
        $dst_x = 0;
        $dst_y = 0;
        $src_x = 0;
        $src_y = 0;
        $dst_w = $width;
        $dst_h = $height;
        $src_w = $imageinfo[0];
        $src_h = $imageinfo[1];
        imagecopyresampled($newImg,$oldimage,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);

        //4.输出画布
        header('Content-Type:image/jpeg');
        imageJpeg($newImg);//输出图片
    }

    #图片加水印
    public function watermark(){
        $water = APPPATH.'views/images/1.jpg';
        $photo = APPPATH.'views/images/ceshi.jpg';
        $water_so = imagecreatefromjpeg($water);
        $photo_so = imagecreatefromjpeg($photo);
        $waterinfo = getimagesize($water);
        //水印合成
        imagecopymerge($photo_so,$water_so,0,0, 0,0,$waterinfo[0],$waterinfo[1],20);
        //4.输出画布
        header('Content-Type:image/jpeg');
        imageJpeg($photo_so);//输出图片
    }

    #PHP合成图片规范
    public function synthesis(){

    }

    #处理图片尺寸
    public function repairImage(){
        # 1.根据图像路径将图像转成source资源 且获取图像信息
        $path = APPPATH.'views/images/01.png';
        $imageinfo = getimagesize($path);
        $oldImage = imagecreatefromjpeg($path);
        # $oldImage = imagecreatefrompng($path);
        # 2.生成画布
        $width = 473;
        $height = 357;
        $newImg = imagecreatetruecolor($width,$height);
        # 3. 生成画笔,为画布填色
        $background_color = imagecolorallocate($newImg,255,255,255);
        imagefill($newImg,0,0,$background_color);

        # 4.生成缩略图
        $dst_x = 0; #$dst_x   画布起始点x坐标
        $dst_y = 0; #$dst_y   画布起始点y坐标
        $src_x = 0; #$src_x   源的x坐标轴
        $src_y = 0; #$src_y   源的y坐标轴
        $dst_w = $width;    #$dst_w   画布的所显示宽度x轴
        $dst_h = $height;   #$dst_h   画布的所显示宽度y轴
        $src_w = $imageinfo[0]; #$src_w    源图像的宽度
        $src_h = $imageinfo[1]; #$src_h    源图像的高度
        imagecopyresampled($newImg,$oldImage,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);

        //4.输出画布
        var_dump($newImg);
//        header('Content-Type:image/jpeg');
//        imageJpeg($newImg);//输出图片
    }

    public function photohecheng(){

        //需要拼接的图片资源
        $path01 = APPPATH.'views/images/01.png';
        $path02 = APPPATH.'views/images/02.png';
        $path03 = APPPATH.'views/images/03.png';

        $oldImage01 = imagecreatefromjpeg($path01);
        $imageinfo01 = getimagesize($path01); #mime image/jpeg

        $oldImage02 = imagecreatefrompng($path02);
        $imageinfo02 = getimagesize($path02); #mime image/png

        $oldImage03 = imagecreatefromjpeg($path03);
        $imageinfo03 = getimagesize($path03); #mime image/jpeg

        //将资源处理成必要的参数
        $imagetmp01 = imagecreatetruecolor(100,100);
        imagecopyresampled($imagetmp01,$oldImage01,0,0,0,0,100,100,$imageinfo01[0],$imageinfo01[1]);

        $imagetmp02 = imagecreatetruecolor(100,100);
        imagecopyresampled($imagetmp02,$oldImage02,0,0,0,0,100,100,$imageinfo02[0],$imageinfo02[1]);
//
        $imagetmp03 = imagecreatetruecolor(100,100);
        imagecopyresampled($imagetmp03,$oldImage03,0,0,0,0,100,100,$imageinfo03[0],$imageinfo03[1]);

//        header('Content-Type:image/jpeg');
//        imageJpeg($imagetmp03);//输出图片

          //创建大画布
        $background = imagecreatetruecolor(600,600); // 画布
        $color = imagecolorallocate($background,255, 255, 255); // 画笔
        imagefill($background,0,0,$color); //涂色
//

        /*
            dst_im    目标图像
            src_im    被拷贝的源图像
            dst_x    目标图像开始 x 坐标
            dst_y    目标图像开始 y 坐标,x,y同为 0 则从左上角开始
            src_x    拷贝图像开始 x 坐标
            src_y    拷贝图像开始 y 坐标,x,y同为 0 则从左上角开始拷贝
            src_w    (从 src_x 开始)拷贝的宽度
            src_h    (从 src_y 开始)拷贝的高度
         * */
//        imagecopyresized($background,$oldImage01,0,0,0,0,600,600,100,100);
        /*
         *
         *
         *
         * */
        imagecopy($background,$imagetmp01,0,0,0,0,100,100);
        imagecopy($background,$imagetmp02,110,0,0,0,100,100);
        imagecopy($background,$imagetmp03,220,0,0,0,100,100);
        header('Content-Type:image/jpeg');
        imageJpeg($background);//输出图片

    }

//    //测试画布
//    public function test($data){
//        $background = imagecreatetruecolor(600,600); // 画布
//        $color = imagecolorallocate($background,255, 255, 255); // 画笔
//        imagefill($background,0,0,$color); //涂色
//    }

/*
 * 1.主方法:方法需要接收一个主图地址,三张小图地址(数组),主图背景色RGB,小图背景色
 *      先检测图片是否可以正常打开,否则不予执行;
 * 2.
 * */
public function protect(){
    $mainImgSrc = APPPATH.'views/images/a.png';
    $SecondaryImgSrcs = array(
        APPPATH.'views/images/01.png',
        APPPATH.'views/images/02.png',
        APPPATH.'views/images/03.png'
    );
    $backgroundRGB = '166,166,166';
    $result = $this->pictureComposition($mainImgSrc,$SecondaryImgSrcs,$backgroundRGB);
//    echo '<pre>';
//    var_dump($result);
}

//图片合成
    /**
     * @param $mainImgSrc  主图图片链接
     * @param array $SecondaryImgSrcs 次要图片链接
     * @param $backgroundRGB 主背景图颜色
     * @return bool|mixed
     */
public function pictureComposition($mainImgSrc,array $SecondaryImgSrcs,$backgroundRGB){
    #检测图片是否真实存在
    if(empty($mainImgSrc)) return false;
    array_push($SecondaryImgSrcs,$mainImgSrc);
    foreach ($SecondaryImgSrcs as $value){
        if(!file_exists($value)) return $value;
    }

    #源图片大图 1449 1200
    $bigImg = $this->setImgSource($mainImgSrc,1449,831,'255,255,255',1);
    #小图第一张 间隙为12 总长为1449
    $smallImg01 = $this->setImgSource($SecondaryImgSrcs[0],475,357,'255,255,255',1);
    $smallImg02 = $this->setImgSource($SecondaryImgSrcs[1],475,357,'255,255,255',1);
    $smallImg03 = $this->setImgSource($SecondaryImgSrcs[2],475,357,'255,255,255',1);

    #创建背景图
    $RGB = explode(',',$backgroundRGB);
    $background = imagecreatetruecolor(1449,1200);
    $color = imagecolorallocate($background,$RGB[0],$RGB[1],$RGB[2]);
    imagefill($background,0,0,$color);

    #将资源图片copy到大画布
    imagecopy($background,$bigImg,0,0,0,0,1449,831);
    imagecopy($background,$smallImg01,0,843,0,0,475,357);
    imagecopy($background,$smallImg02,487,843,0,0,475,357);
    imagecopy($background,$smallImg03,974,843,0,0,475,357);

    header('Content-Type:image/jpeg');
    imageJpeg($background);
    # 销毁画布资源
    imagedestroy($background);

}

    /**
     * @param $ImgSrc 图片的url
     * @param $width  图片要处理的宽
     * @param $height 图片要处理的高
     * @param $RGB    图片要生成背景色(针对png透明图片)
     * @param int $print 要求处理的功能 1 返回处理后图片资源类型  2 在浏览器输出图片 3 将图片下载到指定文件夹(如果是3 下面参数都是必填)
     * @param string $folderpath (非必填) 但是在要求下载图片就是必填项  文件西下载到的文件夹路径
     * @param string $filename (非必填) 指定文件的名称及后缀
     * @return bool|false|resource
     */
private function setImgSource($ImgSrc,$width,$height,$RGB,$print=1,$folderpath='',$filename=''){
    # 创建一块带有颜色的画布
    $background = imagecreatetruecolor($width,$height);
    $RGB = explode(',',$RGB);
    $color = imagecolorallocate($background,$RGB[0],$RGB[1],$RGB[2]); #画笔
    imagefill($background,0,0,$color);

    #解析Image 转成资源类型
    $ImgSrcInfo = getimagesize($ImgSrc);
    switch($ImgSrcInfo['mime']){
        case 'image/jpg':
        case 'image/jpeg':
            $imagecreate = 'imagecreatefromjpeg';
            break;
        case 'image/png':
            $imagecreate = 'imagecreatefrompng';
            break;
        default:
            $imagecreate = 'imagecreatefromstring';
            break;
    }
    $oldResource = $imagecreate($ImgSrc);

    #合成
    imagecopyresampled($background,$oldResource,0,0,0,0,$width,$height,$ImgSrcInfo[0],$ImgSrcInfo[1]);
    //这个原理是拉升 固定好画布宽高就一定会被拉升
    //等比例宽高 这边设计到分辨率换算
//    $rote = $ImgSrcInfo[1]/$ImgSrcInfo[0];
//    $height_y = $rote*$width;
//    imagecopyresampled($background,$oldResource,0,0,0,0,$width,$height,$width,$height_y);
//    imagecopy($background,$oldResource,0,0,0,0,600,$height_y);

    #输出方式
    # 1 返回资源类型 2 在浏览器输出 3 保存图片
    if($print === 1){
        return $background;
    }elseif ($print === 2){
        header('Content-Type:image/jpeg');
        imageJpeg($background);
        # 销毁画布资源
        imagedestroy($background);
    }elseif ($print === 3){
        #处理字符串末尾‘/’
        if(empty($folderpath)) return false;
        $path = trim($folderpath);
        $strlen = strlen($path)-1;
        if(substr($path,$strlen,1)!='/'){
            $path = $path.'/';
        }
        #拼接成路径
        $downloadPath = $path.$filename;
        # 下载
        imagejpeg($background,$downloadPath);
        imagedestroy($background);
    }else{
        return false;
    }

}


}
posted @ 2020-06-24 15:32  锐庆  阅读(368)  评论(0)    收藏  举报