PHP
class SlideVerifyService extends BaseService
{
public $basePath;
public $im = null;
public $im_fullbg = null;
public $im_bg = null;
public $im_slide = null;
public $bg_width = 408;
public $bg_height = 150;
public $mark_width = 50;
public $mark_height = 50;
public $_x = 0;
public $_y = 0;
public function make($bg_width = 408, $bg_height = 150)
{
$this->bg_width = $bg_width;
$this->bg_height = $bg_height;
$this->_init();
$this->_createSlide();
$this->_createBg();
ob_start();
imagepng($this->im_bg);
$bg_data = ob_get_contents();
ob_end_clean();
ob_start();
imagepng($this->im_slide, null, 7);
$slide_data = ob_get_contents();
ob_end_clean();
$this->_destroy();
return [
"captcha" => $this->_x,
"bg" => 'data:image/png;base64,' . chunk_split(base64_encode($bg_data)),
"slide" => 'data:image/png;base64,' . chunk_split(base64_encode($slide_data)),
];
}
private function _init()
{
$this->basePath = Yii::$app->getBasePath() . "/bussadmin/verify/";
$files = yii\helpers\FileHelper::findFiles($this->basePath . "/bg/",['only'=>['*.png']]);
$bg = mt_rand(1, count($files));
$this->im_fullbg = imagecreatefrompng($files[$bg-1]);
$this->im_bg = imagecreatetruecolor($this->bg_width, $this->bg_height);
imagecopy($this->im_bg, $this->im_fullbg, 0, 0, 0, 0, $this->bg_width, $this->bg_height);
$this->im_slide = imagecreatetruecolor($this->mark_width, $this->bg_height);
$this->_x = mt_rand(50, $this->bg_width - $this->mark_width - 1);
$this->_y = mt_rand(0, $this->bg_height - $this->mark_height - 1);
}
private function _createBg()
{
$file_mark = $this->basePath . '/img/mark.png';
$im = imagecreatef