123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace Tool\MayouTool\Captcha\Src;
- class Image
- {
- /**
- * 已生成的图片
- *
- * @var resource
- */
- protected $image;
- public function __construct($image)
- {
- $this->image = $image;
- }
- /**
- * 获取 base64的数据,用做image标签的src
- *
- * @return string
- */
- public function getDataUrl()
- {
- return 'data:image/jpeg;base64,' . $this->getBase64();
- }
- /**
- * 获取base64
- *
- * @return string
- */
- public function getBase64()
- {
- return base64_encode($this->getContent());
- }
- /**
- * 获取图片
- *
- * @return resource
- */
- public function getImage()
- {
- return $this->image;
- }
- /**
- * 获取图片
- *
- * @return resource
- */
- public function getContext()
- {
- return $this->getImage();
- }
- /**
- * 保存为图片
- *
- * @param string $filename 文件名
- * @return $this
- */
- public function save($filename)
- {
- $this->output($filename);
- return $this;
- }
- /**
- * 直接输出
- *
- * @param string|null $filename 文件名
- * @return $this
- */
- public function output($filename = null)
- {
- imagejpeg($this->getContext(), $filename);
- return $this;
- }
- /**
- * 获取输出内容
- *
- * @return string
- */
- public function getContent()
- {
- ob_start();
- $this->output();
- return ob_get_clean();
- }
- }
|