99 lines
3.1 KiB
PHP
99 lines
3.1 KiB
PHP
<?php
|
|
namespace app\api\wxapi;
|
|
|
|
/* $codeImg = new codeImg();
|
|
$source = "./uploads/share.png"; //背景图
|
|
$codeurl = "./uploads/pic.png"; //合成图
|
|
$n_pic='./uploads/1.png';
|
|
$generateImg = $codeImg->generateImg($n_pic,$source, $codeurl, 80, 95, 590, 850); */
|
|
|
|
class CodeImg {
|
|
|
|
public function generateImgPng($filename='',$source, $codeurl, $pic_position_x=0, $pic_position_y=0, $pic_w=0, $pic_h=0)
|
|
{
|
|
$main = imagecreatefrompng($source); //imagecreatefromjpeg
|
|
$source_w=imagesx($main);
|
|
$source_h=imagesy($main);
|
|
|
|
$target = imagecreatetruecolor($source_w, $source_h);
|
|
$white = imagecolorallocate($target, 255, 255, 255);
|
|
imagefill($target, 0, 0, $white);
|
|
imagecopyresampled($target, $main, 0, 0, 0, 0, $source_w, $source_h, $source_w, $source_h);
|
|
|
|
$codepic = imagecreatefrompng($codeurl);
|
|
$codepic_w=imagesx($codepic);
|
|
$codepic_h=imagesy($codepic);
|
|
|
|
$pic_w=$pic_w?$pic_w:$codepic_w;
|
|
$pic_h=$pic_h?$pic_h:$codepic_h;
|
|
|
|
$position_x=$source_w/2-$codepic_w/2;
|
|
$position_y=$source_h-$codepic_h-$source_h*0.2;
|
|
|
|
$pic_position_x=$pic_position_x?$pic_position_x:$position_x;
|
|
$pic_position_y=$pic_position_y?$pic_position_y:$position_y;
|
|
|
|
imagecopyresampled($target, $codepic, $pic_position_x, $pic_position_y, 0, 0, $pic_w, $pic_h,$codepic_w,$codepic_h);
|
|
imagedestroy($codepic);
|
|
//$this->createImg();
|
|
|
|
@mkdir('./' . dirname($filename));
|
|
imagepng($target, $filename); //imagejpeg
|
|
imagedestroy($main);
|
|
imagedestroy($target);
|
|
|
|
return $filename;
|
|
}
|
|
|
|
public function PngToJpeg($source, $codeurl){
|
|
$tmp = imagecreatefrompng($source);
|
|
$w = imagesx($tmp);
|
|
$y = imagesy($tmp);
|
|
$simg = imagecreatetruecolor($w, $y);
|
|
$bg =imagecolorallocate($simg, 255, 255, 255);
|
|
imagefill($simg, 0, 0, $bg);
|
|
imagecopyresized($simg, $tmp, 0, 0, 0, 0,$w, $y, $w, $y);
|
|
|
|
imagejpeg($tmp, $codeurl);
|
|
imagedestroy($tmp);
|
|
}
|
|
|
|
public function generateImgJpg($filename='',$source, $codeurl, $pic_position_x=0, $pic_position_y=0, $pic_w=0, $pic_h=0)
|
|
{
|
|
$main = imagecreatefromjpeg($source); //imagecreatefromjpeg
|
|
$source_w=imagesx($main);
|
|
$source_h=imagesy($main);
|
|
|
|
$target = imagecreatetruecolor($source_w, $source_h);
|
|
$white = imagecolorallocate($target, 255, 255, 255);
|
|
imagefill($target, 0, 0, $white);
|
|
imagecopyresampled($target, $main, 0, 0, 0, 0, $source_w, $source_h, $source_w, $source_h);
|
|
|
|
$codepic = imagecreatefromjpeg($codeurl);
|
|
$codepic_w=imagesx($codepic);
|
|
$codepic_h=imagesy($codepic);
|
|
|
|
$pic_w=$pic_w?$pic_w:$codepic_w;
|
|
$pic_h=$pic_h?$pic_h:$codepic_h;
|
|
|
|
$position_x=$source_w/2-$codepic_w/2;
|
|
$position_y=$source_h-$codepic_h-$source_h*0.2;
|
|
|
|
$pic_position_x=$pic_position_x?$pic_position_x:$position_x;
|
|
$pic_position_y=$pic_position_y?$pic_position_y:$position_y;
|
|
|
|
imagecopyresampled($target, $codepic, $pic_position_x, $pic_position_y, 0, 0, $pic_w, $pic_h,$codepic_w,$codepic_h);
|
|
imagedestroy($codepic);
|
|
//$this->createImg();
|
|
|
|
@mkdir('./' . dirname($filename));
|
|
imagejpeg($target, $filename); //imagejpeg
|
|
imagedestroy($main);
|
|
imagedestroy($target);
|
|
|
|
return $filename;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|