gd库处理图片(将正方形转换圆形) (调整 头像大小)(文字图片合并)(创建画布)

/**
 * 生成缩略图
 * $imgSrc           图片源路径
 * $resize_width     图片宽度
 * $resize_height    图片高度
 * $dstimg           缩略图路径
 * $isCut            是否剪切图片
 */
function reSizeImg($imgSrc, $resize_width, $resize_height, $dstimg, $isCut = false) {
    //图片的类型
    $type = substr(strrchr($imgSrc, "."), 1);
    //初始化图象
    if ($type == "jpg" || $type == "jpeg") {
        $im = imagecreatefromjpeg($imgSrc);
    }
    if ($type == "gif") {
        $im = imagecreatefromgif($imgSrc);
    }
    if ($type == "png") {
        $im = imagecreatefrompng($imgSrc);
    }

    $width = imagesx($im);
    $height = imagesy($im);

    //生成图象
    //改变后的图象的比例
    $resize_ratio = ($resize_width) / ($resize_height);
    //实际图象的比例
    $ratio = ($width) / ($height);
    if (($isCut) == 1) {
        if ($ratio >= $resize_ratio) {
            //高度优先
            $newimg = imagecreatetruecolor($resize_width, $resize_height);
            imagecopyresampled($newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, (($height) * $resize_ratio), $height);
            ImageJpeg($newimg, $dstimg);
        }
        if ($ratio < $resize_ratio) {
            //宽度优先
            $newimg = imagecreatetruecolor($resize_width, $resize_height);
            imagecopyresampled($newimg, $im, 0, 0, 0, 0, $resize_width, $resize_height, $width, (($width) / $resize_ratio));
            ImageJpeg($newimg, $dstimg);
        }
    } else {
        if ($ratio >= $resize_ratio) {
            $newimg = imagecreatetruecolor($resize_width, ($resize_width) / $ratio);
            imagecopyresampled($newimg, $im, 0, 0, 0, 0, $resize_width, ($resize_width) / $ratio, $width, $height);
            ImageJpeg($newimg, $dstimg);
        }
        if ($ratio < $resize_ratio) {
            $newimg = imagecreatetruecolor(($resize_height) * $ratio, $resize_height);
            imagecopyresampled($newimg, $im, 0, 0, 0, 0, ($resize_height) * $ratio, $resize_height, $width, $height);
            ImageJpeg($newimg, $dstimg);
        }
    }
    ImageDestroy($im);
}
<?php
/*
* 将图片切成圆角
*/
function yuanjiao($imgpath){
    $ext  = getimagesize($imgpath);
    $src_img = null;
    switch ($ext['mime']) {
        case 'image/jpeg':
            $src_img = imagecreatefromjpeg($imgpath);
            break;
        case 'image/png':
            $src_img = imagecreatefrompng($imgpath);
            break;
    }
    $wh  = getimagesize($imgpath);
    $w   = $wh[0];
    $h   = $wh[1];
    $w   = min($w, $h);
    $h   = $w;
    $img = imagecreatetruecolor($w, $h);
    //这一句一定要有
    imagesavealpha($img, true);
    //拾取一个完全透明的颜色,最后一个参数127为全透明
    $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
    imagefill($img, 0, 0, $bg);
    $r   = $w / 2; //圆半径
    $y_x = $r; //圆心X坐标
    $y_y = $r; //圆心Y坐标
    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {
            $rgbColor = imagecolorat($src_img, $x, $y);
            if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                imagesetpixel($img, $x, $y, $rgbColor);
            }
        }
    }
    imagepng($img,$imgpath);
}
?>
/*
 *  调整 头像大小
 *  $srcImage 图片路径 (绝对路径)
 *  $maxwidth 动态设置图片宽度
 *  $maxheight 动态设置图片高度
 *  $name   后期追击参数 (动态设置图片名称)
 * */
function resizeImage($srcImage,$maxwidth,$maxheight,$name=1)
{
    list($width, $height, $type, $attr) = getimagesize($srcImage);
    switch ($type) {
        case 1:
            $img = imagecreatefromgif($srcImage);
            break;
        case 2:
            $img = imagecreatefromjpeg($srcImage);
            break;
        case 3:
            $img = imagecreatefrompng($srcImage);
            break;
        default:
            $img = imagecreatefrompng($srcImage);
            break;
    }
    $canvas = imagecreatetruecolor($maxwidth,$maxheight); // 创建一个真彩色图像 我把它理解为创建了一个画布

    imagecopyresampled($canvas,$img,0,0,0,0,$maxwidth,$maxheight,$width,$height);
    // 选取原图片整个长宽上的像素,将原图片左上角和画布左上角对齐,画布宽100px,高100px。
    // 这样设置是缩小或放大原图片。 如果原图片宽或高比画布大就会缩小,如果原图片宽或高比画布小就会放大。
    // 如果想截取图片,不想放大或缩小。 就要将最后两个参数设置成分别和画布宽和高相等。 像这样:imagecopyresampled($canvas,$sourceImage,0,0,0,0,$newWidth,$newHeight,$newWidth,$newHeight);
    // 如果想控制从哪里开始截取,就要设置后面两个“0”。 前面一个“0”是原图片上X轴坐标,后面一个是Y轴坐标。 比如,想从原图片X轴100px,Y轴50px的点开始截取。就要这样设置:imagecopyresampled($canvas,$sourceImage,0,0,100,50,$newWidth,$newHeight,$newWidth,$newHeight);
    return $canvas;//返回参数为已经打开的图片格式
}
/**
 * 图片追加文字
 * $bigImgPath           背景图片
 * $lock_img            本地保存图片
 * $text            合成文字
 * $fontSize = 12;   //字体大小
 * $left = 50;      //左边距
 * $top = 150;       //顶边距
 *
 * */
function zh_imagefttext($bigImgPath,$lock_img,$text,$left=0,$top=0,$fontSize=12){
    $img = imagecreatefromstring(file_get_contents($bigImgPath));

    $font = app()->getRootPath().'/public/SimHei.ttf';//字体,字体文件需保存到相应文件夹下
    $black = imagecolorallocate($img, 0, 0, 0);//字体颜色 RGB

    $fontBox = imagettfbbox($fontSize, 0, $font, $text);//获取文字所需的尺寸大小

    if($left==0){
        $left = ceil((650 - $fontBox[2]) / 2);
    }
    imagettftext($img, $fontSize, 0, $left, $top, $black, $font,$text);

    list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
    switch ($bgType) {
        case 1: //gif
            imagegif($img,$lock_img);
            break;
        case 2: //jpg
            imagejpeg($img,$lock_img);
            break;
        case 3: //png
            imagepng($img,$lock_img);  //在 images 目录下就会生成一个 circle.png 文件,上面也可设置相应的保存目录及文件名。
            break;
        default:
            break;
    }
}
/*
        * 创建画布并合并
        *
        *
        * */
        function test(){
            $w = 50;//图片与画布左间距
            $h = 50;//图片与画布上间距
            $canvas_k = 370;//画布宽度
            $canvas_g = 650;//画布高度
            $bigImg = imagecreatetruecolor($canvas_k,$canvas_g);/* 创建画布 */
            //增加一个白色的底,不然新建的画布是黑色的
            $white = imagecolorallocate($bigImg, 255, 255, 255);
            imagefill($bigImg, 0, 0, $white);

            $qCodeImg = imagecreatefromstring(file_get_contents('./1.png'));

            list($qCodeWidth, $qCodeHight) = getimagesize('./1.png');
            //$qCodeWidth 图片的宽度 $qCodeHight 图片的高度(超出图片本身大小,则会由黑色底部填充)
            imagecopy($bigImg,$qCodeImg, $w,$h, 0, 0,$qCodeWidth, $qCodeHight);

            //输出图片
            imagepng($bigImg);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昊喵喵博士

大哥你真帅,小姐姐你真漂亮

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值