需求:最近在做一个原生的php项目,要通过用户上传的图片和信息,生成一个对应的证件,就想到了使用GD库,但是现在的是自己不太会,所以靠百度,找到了一个大佬写的代码,然后封装了一下,实现了此效果,留下记录,以后可以直接拿来使用,
php代码
<?php
class ImageHandle
{
private $tmpdir = ".";
private $font = array("family" => "E:/phpstudy_pro/WWW/php7/simkai.ttf", "color" => array(255, 0, 0), "size" => 22 );
private $color = [255,0,255];
private $size = 18;
public function SetTmpDir($tmpdir = "."){
$this->tmpdir = $tmpdir;
}
public function SetFont($font){
foreach ($this->font as $key => $value) {
if(array_key_exists($key, $font)){
$this->font[$key] = $font[$key];
}
}
}
public 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 "";
}
public function MergeTwoJpegImage($image1, $image2, $x, $y){
$base = imagecreatefromjpeg($image1);
$go = imagecreatefromjpeg($image2);
$gox = imagesx($go);
$goy = imagesy($go);
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;
}
public function ImageAddText($image, $text, $x = 0, $y = 0, $textAlign = 0){
$base = imagecreatefromjpeg($image);
$color = imagecolorallocate($base, $this->color[0], $this->color[1], $this->color[2]);
if($textAlign == 2){
$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){
imagestring($base, 5, 0, 0, "Hello world!", $color);
imagestring($base, 5, 100, 250, "Hello Testt!", $color);
}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 heChengHaiBao($beijing, $touxiang, $nicheng){
$ih = new ImageHandle();
$ih->SetTmpDir("./im");
$ih->SetFont(array("color"=>array(255,255,255),"size"=>20));
$bj = $ih->ImageToWidthHeight($beijing, 500, 300);
$tx = $ih->ImageToWidthHeight($touxiang, 200, 200);
$tmp1 = $ih->MergeTwoJpegImage($bj, $tx, 30, 30);
$tmp5 = $ih->ImageAddText($tmp1, $nicheng, 0, 150, 1);
@unlink($bj);
@unlink($tx);
@unlink($tmp1);
return $tmp5;
}
heChengHaiBao("./test.jpg", "./one.jpg", $font);
?>
需要实时学习,切不可妄自骄傲自满,昨天遇见的需求,然后明白了一个道理,其实代码不是很难写的,主要是你要首先知道你的逻辑是同的,你想要的实现的是什么东西,想起我们老板给我说过一段话, 写代码,首先要先写伪代码, 这步是要做什么,下一步是要做什么,这一步是为了下一步要做成什么。刚开始的时候还是有点不太理解,或者有点自大。但是现在发现,经验不是白长的,更何况你先在还只是刚回爬的小孩子儿,好好学习,现在这个状态还是比较满足的,凡事能让自己上瘾的,首先就直接干到他,比如游戏,直接卸载,比如看得小说,直接卸载,昨天看小说看到了3点,有个终结的地方,直接不看了,当时就把小说软件给卸载了。单由于昨天的熬夜,今天办公就不是很有精神。