可以改变大小
添加文字
图片合并
<?php
function meg_images($base, $new, $x, $y, $newName){
$info = getimagesize($base);
print_r($info);
$bak = imagecreatefromjpeg($base);
$go = imagecreatefromgif($new);
$gox = imagesx($go);
$goy = imagesy($go);
imagecopy($bak, $go, $x, $y, 0, 0, $gox, $goy);
imagepng($bak, $newName.image_type_to_extension(IMAGETYPE_JPEG));
imagedestroy($bak);
imagedestroy($go);
}
// meg_images("./a.jpg", "./b.gif", 50, 50, "./c.png");
class ImageHandle
{
private $tmpdir = ".";
private $font = array("family" => "./simkai.ttf", "color" => array(0, 0, 0), "size" => 16 );
public function SetTmpDir($tmpdir = "."){
$this->tmpdir = $tmpdir;
}
public function SetFont($font){
// $this->font = $font;
foreach ($this->font as $key => $value) {
if(array_key_exists($key, $font)){
$this->font[$key] = $font[$key];
}
}
}
function ImageToWidthHeight($image, $width, $height){
$imageinfo = getimagesize($image);
$former = null;
switch (end($imageinfo)) {
case 'image/jpeg':
$former = imagecreatefromjpeg($image);
break;
case 'image/png':
$former = imagecreatefrompng($image);
break;
default: break;
}
if($former){
$new = imagecreatetruecolor($width, $height);
imagecopyresampled($new, $former, 0, 0, 0, 0, $width, $height, imagesx($former), imagesy($former));
$tpath = $this->tmpdir.DIRECTORY_SEPARATOR.md5(time()."".mt_rand()).image_type_to_extension(IMAGETYPE_JPEG);
imagejpeg($new, $tpath, 100);
imagedestroy($new);
imagedestroy($former);
return $tpath;
}
return "";
}
function MergeTwoJpegImage($image1, $image2, $x, $y){
$base = imagecreatefromjpeg($image1);
$go = imagecreatefromjpeg($image2);
$gox = imagesx($go);
$goy = imagesy($go);
// imagecopy($base, $go, $x, $y, 0, 0, $gox, $goy);
imagecopymerge($base, $go, $x, $y, 0, 0, $gox, $goy, 100);
$tpath = $this->tmpdir.DIRECTORY_SEPARATOR.md5(time()."".mt_rand()).image_type_to_extension(IMAGETYPE_JPEG);
imagejpeg($base, $tpath, 100);
imagedestroy($base);
imagedestroy($go);
return $tpath;
}
function ImageAddText($image, $text, $x = 0, $y = 0, $textAlign = 0){
$base = imagecreatefromjpeg($image);
$color = imagecolorallocate($base, $this->font['color'][0], $this->font['color'][1], $this->font['color'][2]);
if($textAlign == 2){
// 右对齐 $x 为距离$image 的右侧距离
$fontbox = imageftbbox($this->font['size'], 0, $this->font['family'], $text);
imagettftext($base, $this->font['size'], 0, ceil(imagesx($base) - $x - $fontbox[2]), $y, $color, $this->font['family'], $text);
}else if($textAlign == 1){
// 文字居中 自动识别图片中间位置,$x无效
$fontbox = imageftbbox($this->font['size'], 0, $this->font['family'], $text);
imagettftext($base, $this->font['size'], 0, ceil( (imagesx($base) - $fontbox[2]) / 2), $y, $color, $this->font['family'], $text);
}else{
// 左对齐
imagettftext($base, $this->font['size'], 0, $x, $y, $color, $this->font['family'], $text);
}
$tpath = $this->tmpdir.DIRECTORY_SEPARATOR.md5(time()."".mt_rand()).image_type_to_extension(IMAGETYPE_JPEG);
imagejpeg($base, $tpath, 100);
imagedestroy($base);
return $tpath;
}
function Sharpen($image, $degree){
$tmpfile = imagecreatefromjpeg($image);
$width = imagesx($tmpfile);
$height = imagesy($tmpfile);
$truecolor = imagecreatetruecolor($width, $height);
$cnt = 0;
for ($x = 1; $x < $width; $x++){
for ($y = 1; $y < $height; $y++){
$src_clr1 = imagecolorsforindex($truecolor, imagecolorat($tmpfile, $x-1, $y-1));
$src_clr2 = imagecolorsforindex($truecolor, imagecolorat($tmpfile, $x, $y));
$r = intval($src_clr2["red"] + $degree * ($src_clr2["red"] - $src_clr1["red"]));
$g = intval($src_clr2["green"] + $degree * ($src_clr2["green"] - $src_clr1["green"]));
$b = intval($src_clr2["blue"] + $degree * ($src_clr2["blue"] - $src_clr1["blue"]));
$r = min(255, max($r, 0));
$g = min(255, max($g, 0));
$b = min(255, max($b, 0));
if (($DST_CLR = imagecolorexact($tmpfile, $r, $g, $b)) == -1)
$DST_CLR = imagecolorallocate($tmpfile, $r, $g, $b);
$cnt++;
if ($DST_CLR == -1) die("color allocate faile at $x, $y ($cnt).");
imagesetpixel($truecolor, $x, $y, $DST_CLR);
}
}
$tpath = $this->tmpdir.DIRECTORY_SEPARATOR.md5(time()."".mt_rand()).image_type_to_extension(IMAGETYPE_JPEG);
imagejpeg($truecolor, $tpath);
imagedestroy($tmpfile);
return $tpath;
}
}
function heChengHaiBao($beijing, $touxiang, $nicheng, $erweima){
$ih = new ImageHandle();
$ih->SetTmpDir("./im"); //图片生成目录
$ih->SetFont(array("color"=>array(255,255,255),"size"=>20));
$bj = $ih->ImageToWidthHeight($beijing, 640, 1008);
$tx = $ih->ImageToWidthHeight($touxiang, 165, 165);
$qr = $ih->ImageToWidthHeight($erweima, 200, 200);
$q2 = $ih->ImageToWidthHeight("./im/aa/erweima2.png", 200, 200); //二维码右侧的固定图
$tg = $ih->ImageToWidthHeight("./im/aa/ewai.png", 400, 120); //二维码下方的文字图
$tmp1 = $ih->MergeTwoJpegImage($bj, $tx, 237, 100);
$tmp2 = $ih->MergeTwoJpegImage($tmp1, $qr, 120, 460);
$tmp3 = $ih->MergeTwoJpegImage($tmp2, $q2, 320, 460);
$tmp4 = $ih->MergeTwoJpegImage($tmp3, $tg, 120, 720);
$tmp5 = $ih->ImageAddText($tmp4, $nicheng, 0, 295, 1);
@unlink($bj);
@unlink($tx);
@unlink($qr);
@unlink($q2);
@unlink($tg);
@unlink($tmp1);
@unlink($tmp2);
@unlink($tmp3);
@unlink($tmp4);
return $tmp5;
}
heChengHaiBao("./im/aa/moban.jpg", "./im/aa/yami.png", "这是昵称", "./im/aa/erweima.png");
?>