php图像处理类demo

<?php
/**
* Created by PhpStorm.
* User: chenshuai
* Date: 2018/7/12
* Time: 16:57
*/

class Image {

//水印 待做


//合并图片
public function merge($bgImg,$logoImg='',$width=30,$height=30)
{

//图片信息
list($bgImgWidth, $bgImgHeight, $bgImgType) = getimagesize($bgImg);

list($logoImgWidth, $logoImgHeight, $logoImgType) = getimagesize($logoImg);

//加载图片
if( '2' == $bgImgType ){
$new_background_image = imagecreatefromjpeg($bgImg);
}else{
$new_background_image = imagecreatefrompng($bgImg);
}

//加载图片
if( '2' == $logoImgType ){
$new_logo = imagecreatefromjpeg($logoImg);
}else{
$new_logo = imagecreatefrompng($logoImg);
}

//新建一个真彩色图像
$target_image = imageCreatetruecolor(imagesx($new_background_image),imagesy($new_background_image));

//为一幅图像分配颜色
imagesavealpha($target_image, true);
//$color = imagecolorallocate($target_image, 255, 255, 255); //底图白色
$color = imagecolorallocatealpha($target_image, 255,255,255,127); //底图透明

//区域填充 填充颜色
imagefill($target_image, 0, 0, $color);

//将某个颜色定义为透明色
imageColorTransparent($target_image, $color);

//重采样拷贝部分图像并调整大小
imagecopyresampled($target_image,$new_background_image,0,0,0,0,imagesx($new_background_image),imagesy($new_background_image),imagesx($new_background_image),imagesy($new_background_image));


//拷贝并合并图像的一部分
//imagecopymerge($target_image,$new_logo, 20,20,0,0,imagesx($new_logo),imagesy($new_logo), 100); //不能拷贝 png 透明属性
imagecopy($target_image,$new_logo, 10,10,0,0,imagesx($new_logo),imagesy($new_logo));

//将画布保存到指定的文件
imagepng($target_image, "hero_gam.png");

imagedestroy($target_image);


}

//缩略图
public function thumb($imgName,$width=60,$height=60)
{
//图片信息
list($oldWidth, $oldHeight, $imgType) = getimagesize($imgName);

//加载图片
if( '2' == $imgType ){
$source = imagecreatefromjpeg($imgName);
}else{
$source = imagecreatefrompng($imgName);
}

// 新建一个真彩色图像
$thumb = imagecreatetruecolor($width, $height);

// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width, $height, $oldWidth, $oldHeight);

// Output
if( '2' == $imgType ){
imagejpeg($thumb,'thumb.jpg');
}else{
imagepng($thumb,'thumb.png');
}

//释放空间
imagedestroy($thumb);

//返回
return 'thumb.jpg';

}


//圆型图片
public function circle($imgpath,$saveName = 'circle.png')
{
$src_img = imagecreatefromstring(file_get_contents($imgpath));
$w = imagesx($src_img);
$h = imagesy($src_img);
$w = $h = min($w, $h);

$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圆半径
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, $saveName);

//释放空间
imagedestroy($src_img);
imagedestroy($img);

return $saveName;
}


}


$mgimage = new MgImage();

//缩略图
//$mgimage->thumb('http://imshuai.cn/images/weixin_erweima.png');

//圆型
//$mgimage->circle('http://imshuai.cn/images/weixin_erweima.png')

//合并图片
$thumb = $mgimage->thumb('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531401434741&di=0acbfd1e85c7e24ddf616e5c4c92b738&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201407%2F05%2F20140705000145_rxSsR.jpeg');
$circle = $mgimage->circle($thumb);
$mgimage->merge('backgroup.png',$circle);


方法: 
circle()  圆形图片

merge()   合并图片

thumb()  缩略图

demo:


tags: PHP