PHP生成海報/PHP合併圖片/PHP圖片處理
外部類生成圓角圖片下載地址 https://mywebmymail.com/easyphpthumbnail/
使用該功能必須有GD庫支援,文字水印還需要 freetype 庫支援
一、圖片處理類
<?php
/**
* Created by PhpStorm.
* User: panxp
* Date: 2018-08-24
* Time: 9:50
*/
namespace Utility;
use Lib;
class ImageLib
{
//生成新圖片的地址
public $newImage;
public function __construct($newImage = '')
{
if ($newImage) {
$this->newImage = $newImage;
}
}
/** 合併圖片可以多次使用 **/
public function mergeImage($bgImgPath, $thumbImagePath, $dstx = 290, $dsty = 600)
{
$bigImg = imagecreatefromstring(file_get_contents($bgImgPath));
$qCodeImg = imagecreatefromstring(file_get_contents($thumbImagePath));
list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($thumbImagePath);
// imagecopymerge使用註解
imagecopymerge($bigImg, $qCodeImg, $dstx, $dsty, 0, 0, $qCodeWidth, $qCodeHight, 100);
// list($bigWidth, $bigHight, $bigType) = getimagesize($bgImgPath);
$bigType = 2;
if ($this->newImage) {
imagejpeg($bigImg, $this->newImage);
} else {
switch ($bigType) {
case 2: //jpg
header('Content-Type:image/jpg');
imagejpeg($bigImg);
break;
default:
# code...
break;
}
}
imagedestroy($bigImg);
imagedestroy($qCodeImg);
}
/** 生成文水印 **/
public function waterMark($text, $left, $top, $bgDir = '')
{
if ($this->newImage) {
$bgDir = $this->newImage;
}
$img = imagecreatefromstring(file_get_contents($bgDir));
$font = realpath('.') . DS . 'font' . DS . 'fangzhenghuali_GBK.ttf';//字型
$black = imagecolorallocate($img, 51, 51, 51);//字型顏色 RGB
$fontSize = 33; //字型大小
$circleSize = 0; //旋轉角度
imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, $text);
// list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
imagejpeg($img, $this->newImage);
imagedestroy($img);
}
/**
* 縮小圖片
* @param $filename
*/
public function thumbImage($filename, $newFileName, $n_w = 160, $n_h = 160)
{
list($width, $height) = getimagesize($filename);
//縮放比例
if ($width > $n_w) {
$new = imagecreatetruecolor($n_w, $n_h);
$img = imagecreatefromjpeg($filename);
//copy部分影象並調整
imagecopyresized($new, $img, 0, 0, 0, 0, $n_w, $n_h, $width, $height);
//影象輸出新圖片、另存為
imagejpeg($new, $newFileName);
imagedestroy($new);
imagedestroy($img);
return array('width' => $n_w, 'height' => $n_h);
} else {
return array('width' => $width, 'height' => $height);
}
}
/**
* 生成圓角圖片
*/
public function roundCornerImage($thumbAvatarDir,$roundCornerAvatar)
{
include_once APP . '/Lib/ThumbImage.php';
$thumb = new Lib\ThumbImage();
$thumb->Backgroundcolor = '#0000FF';
//$thumb->Thumblocation = '/data/www/cake/zhly_api/topicPoiQrcode/';
//$thumb->Thumbprefix = 'roundCornerAvatar';
$thumb->ThumbFileDir = $roundCornerAvatar;
$thumb->Clipcorner = array(2, 50, 0, 1, 1, 1, 1);
$thumb->Maketransparent = array(1, 1, '#0000FF', 30);
$thumb->Createthumb($thumbAvatarDir, 'file');
}
}
二、使用示例(使用專案中,沒有拷貝全部程式碼)
$image = new Utility\ImageLib($newImage);
if (!file_exists($roundCornerImageAvatarDir)) {
$http = new HttpSocket(array(
'ssl_verify_host' => false
));
//把遠端頭像儲存到本地
file_put_contents($avatarDir, $http->get($avatar));
//縮小頭像到160x160
$thumbAvatar = $image->thumbImage($avatarDir, $thumbAvatarDir, $thumbAvatarWidth, $thumbAvatarHeight);
//頭像縮小
if ($thumbAvatar['width'] == $thumbAvatarWidth) {
//生成圓角頭像
$image->roundCornerImage($thumbAvatarDir, $roundCornerImageAvatarDir);
} else {
//頭像原圖小於160
//生成圓角頭像
$mergeAvatarDistX = (750 - $thumbAvatar['width']) / 2;
$mergeAvatarDistY = $mergeAvatarDistY + ($mergeAvatarDistY - $thumbAvatar['height']) / 2.8;
$image->roundCornerImage($avatarDir, $roundCornerImageAvatarDir);
}
} else {
return true;
}
//合併二維碼
$image->mergeImage($bgImage, $qrcodeImage, 270, 1065);
//生成文字水印
$image->waterMark($text, $txtMarkLeft, 440);
//生成文字水印
$image->waterMark($textExt, 295, 510);
//合併頭像
$image->mergeImage($newImage, $roundCornerImageAvatarDir, $mergeAvatarDistX, $mergeAvatarDistY);
// header('Content-Type:image/jpg');
// imagejpeg(imagecreatefromjpeg($newImage));
return true;