ROOT 定义的项目根目录
<?php
class Poster
{
const SAVE_PATH = '/poster/image';
/**
* 图片合并
* @param string $dst_file 目标图片
* @param string $src_file 原图片
* @param int $dst_x 目标图片起点横坐标
* @param int $dst_y 目标图片起点纵坐标
* @param int $src_x 源图片起点横坐标
* @param int $src_y 源图片起点纵坐标
* @param int $angle 源图片旋转角度
* @return string 保存后图片路径
*/
public function imgMerge ($dst_file ,$src_file ,$dst_x ,$dst_y ,$src_x=0 ,$src_y=0) {
if (!file_exists($dst_file)) {
exit("合并目标图片不存在!");
}
if (!file_exists($src_file)) {
exit("合并源图片不存在!");
}
$dstInfo = getimagesize($dst_file);//图片信息
$srcInfo = getimagesize($src_file);//图片信息
$dst_fnc = str_replace('/', 'createfrom', $dstInfo['mime']);//创建图片资源函数
$src_fnc = str_replace('/', 'createfrom', $srcInfo['mime']);//创建图片资源函数
$out_fnc = str_replace('/', '', $dstInfo['mime']);//输出函数
list( ,$sufix) = explode('/', $dstInfo['mime']);//输出图片后缀
$src_img = $src_fnc($src_file);//源图片资源
$dst_img = $dst_fnc($dst_file);//目标图片资源
$src_w = $srcInfo[0];
$src_h = $srcInfo[1];
$pct = 100;
//合并图片
if( imagecopy($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h) )
{
imagedestroy($src_img);
$this->checkPath();
$filename = self::SAVE_PATH.'/'.md5(microtime()).'.'.$sufix;
if( $out_fnc($dst_img ,ROOT.$filename) ){
imagedestroy($dst_img);
return $filename;
}
}
}
/**
* 旋转图片
* @param string $img_file 图片路径
* @param int $angle 旋转角度
* @param int $color 旋转后空余背景
* @return string 保存后图片路径
*/
public function imgRotate ($img_file ,$angle) {
if (!file_exists($img_file)) {
exit("旋转图片不存在!");
}
$info = getimagesize($img_file);
$fnc = str_replace('/', 'createfrom', $info['mime']);
$img = $fnc($img_file);
$color = imagecolorallocatealpha($img,255,255,255,127);
if ($rotate_img = imagerotate($img, $angle, $color)) {
imagealphablending($rotate_img ,false);
imagesavealpha($rotate_img ,true);
$this->checkPath();
$filename = self::SAVE_PATH.'/'.md5(microtime()).'.png';
$this->checkPath();
imagepng($rotate_img ,ROOT.$filename);
imagedestroy($img);
imagedestroy($rotate_img);
return $filename;
}
}
/**
* 写入文字
* @param string $img_file 图片位置
* @param string $text 写入文字
* @param int $size 写入文字大小
* @param int $x $y 写入文字起点坐标
* @return string 保存后图片路径
*/
public function write ($img_file ,$text ,$size ,$x ,$y) {
if (!file_exists($img_file)) {
exit("写入图片不存在!");
}
$info = getimagesize($img_file);
$fnc = str_replace('/', 'createfrom', $info['mime']);
$img = $fnc($img_file);
$color = imagecolorallocate($img, 0, 0, 0);
$fontfile = ROOT.'/fonts/SIMYOU.TTF';
$angle = 0;
imagettftext($img, $size, $angle, $x, $y, $color, $fontfile, $text);
$filename = self::SAVE_PATH.'/'.md5(microtime()).'.png';
$this->checkPath();
imagepng($img ,ROOT.$filename);
return $filename;
}
/**
* 裁剪图片
* @param string $img_file 图片位置
* @param int $cut_w 裁剪后的宽
* @param int $cut_h 裁剪后的高
* @param int $cut_x $cut_y 裁剪的起点坐标
* @return string 保存后图片路径
*/
public function cutImg ($img_file ,$cut_w ,$cut_h ,$cut_x=0 ,$cut_y=0) {
if (!file_exists($img_file)) {
exit('裁剪图片不存在!');
}
$info = getimagesize($img_file);
list($src_w ,$src_h) = $info;
$src_fnc = str_replace('/', 'createfrom', $info['mime']);
$src_img = $src_fnc($img_file);
$dst_img = imagecreatetruecolor($cut_w, $cut_h);
$color = imagecolorallocatealpha($dst_img, 255, 255, 255 ,127);
$dst_x = 0;
$dst_y = 0;
if( $cut_w > $src_w || $cut_h > $src_h ) {//缩放图片
imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $cut_x, $cut_y, $cut_w, $cut_h, $src_w, $src_h);
} else {//裁剪
imagecopy($dst_img, $src_img, $dst_x, $dst_y, $cut_x, $cut_y, $cut_w, $cut_h);
}
$this->checkPath();
$filename = self::SAVE_PATH .'/' .md5(microtime()).'.png';
imagepng($dst_img ,ROOT.$filename);
imagedestroy($dst_img);
imagedestroy($src_img);
return $filename;
}
protected function checkPath () {
$path = ROOT.self::SAVE_PATH;
if(!is_dir($path)) {//检查目录
mkdir($path ,0777 ,true);
}
}
}