早睡早起,方能养生
Sleep early rise early, way to keep healthy

PHP生成带圆形头像和二维码的海报

super
2020-08-11 12:21
views 3229

2021/09/25 相关更新:

 

PHP给图片加水印 , 合并二维码 , 最新三合一 imagick

 

------------------

 

原文 :

 

PHP合成海报

 

$bigImg     = imagecreatefromstring(file_get_contents('.' . $background)); // 背景图 /public/bg.jpg
$image_1    = imagecreatefromstring(file_get_contents('.' . $headimg)); // 头像 /public/xx.png
$image_2    = imagecreatefromstring(file_get_contents('.' . $code)); // 二维码 /public/code.png

$x_1 = 10; // 头像x坐标
$y_1 = 10; // 头像y坐标
$x_2 = 200; // 二维码x坐标
$y_2 = 200; // 二维码y坐标

// 二维码缩放
// $this->imageResize('.' . $image_2, 200, 200);

// 复制图片一(头像)到背景图片中(重新取样-获取透明图片)
imagecopyresampled($bigImg, $image_1, $x_1, $y_1, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));

// 与二维码合成
imagecopymerge($bigImg, $image_2, $x_2, $y_2, 0, 0, imagesx($image_2), imagesy($image_2), 100);

$original_analysis = getimagesize('.' . $background);

switch ($original_analysis[2]) {
    case 1:
        imagegif($bigImg, './save_path/xxx.jpg');
        break;
    case 2:
        imagejpeg($bigImg, './save_path/xxx.jpg');
        break;
    case 3:
        imagepng($bigImg, './save_path/xxx.png');
        break;
    default:
        exit("不支持的水印图片文件类型");
}

imagedestroy($bigImg);

 

图片缩放:

 

/**
 * @param $imagedata    图像数据
 * @param $width        缩放宽度
 * @param $height       缩放高度
 * @param int $per      缩放比例,为0不缩放,>0忽略参数2、3的宽高
 */
public function imageResize($file, $width, $height, $per = 0)
{
    // 1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
    $imagedata = file_get_contents($file);
    // 获取图像信息
    list($bigWidth, $bigHight, $bigType) = getimagesizefromstring($imagedata);
    // 缩放比例
    if ($per > 0) {
        $width  = $bigWidth * $per;
        $height = $bigHight * $per;
    }
    // 创建缩略图画板
    $block = imagecreatetruecolor($width, $height);
    // 启用混色模式
    imagealphablending($block, false);
    // 保存PNG alpha通道信息
    imagesavealpha($block, true);
    // 创建原图画板
    $bigImg = imagecreatefromstring($imagedata);
    // 缩放
    imagecopyresampled($block, $bigImg, 0, 0, 0, 0, $width, $height, $bigWidth, $bigHight);
    // 生成临时文件名
    $tmpFilename = tempnam(sys_get_temp_dir(), 'image_');
    // 保存
    switch ($bigType) {
        case 1: 
            imagegif($block, $tmpFilename);
            break;
        case 2: 
            imagejpeg($block, $tmpFilename);
            break;
        case 3: 
            imagepng($block, $tmpFilename);
            break;
    }
    imagepng($block, $file);
    // 销毁
    imagedestroy($block);
}

 

方形头像变成圆形

 

/**
 * 方形图片处理成圆形图片
 * 图片如果不是正方形取最小边的圆半径,从左边开始剪切成圆形
 * @param string $imgpath [description]
 * @return [type] [description]
 */
public function imageChangeRoundness($imgPath = 'xx.jpg')
{
    $ext = pathinfo($imgPath);
    $src_img = null;
    switch ($ext['extension']) {
        case 'jpg':
            $src_img = imagecreatefromjpeg($imgPath);
            break;
        case '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);
            }
        }
    }
    return $img;
}

// 使用
$img = $this->imageChangeRoundness('./QRcode/132.jpg');

$original_analysis = getimagesize('./QRcode/132.jpg');

switch ($original_analysis[2]) {
    case 1:
        imagegif($img, './QRcode/132.jpg');
        break;
    case 2:
        imagejpeg($img, './QRcode/132.jpg');
        break;
    case 3:
        imagepng($img, './QRcode/132.png');
        break;
    default:
        exit("不支持的水印图片文件类型");
}

imagedestroy($img);


分享
0 条讨论
top