Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
cccaimingjian committed Oct 13, 2022
0 parents commit 575f52f
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [overtrue]
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
phpcs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP environment
uses: shivammathur/setup-php@v2
- name: Install dependencies
run: composer install
- name: PHPCSFixer check
run: composer check-style
phpunit:
strategy:
matrix:
php_version: [8.0, 8.1, 8.2]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP environment
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
coverage: xdebug
- name: Install dependencies
run: composer install
- name: PHPUnit check
run: ./vendor/bin/phpunit --coverage-text
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.idea

/vendor/
node_modules/
npm-debug.log
yarn-error.log

# Laravel 4 specific
bootstrap/compiled.php
app/storage/

# Laravel 5 & Lumen specific
public/storage
public/hot

# Laravel 5 & Lumen specific with changed public path
public_html/storage
public_html/hot

storage/*.key
.env
Homestead.yaml
Homestead.json
/.vagrant
.phpunit.result.cache

cghooks.lock
.php-cs-fixer.cache
composer.lock
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2020 overtrue <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
laravel-image-watermark
---

Make the whole image full of watermarks

## Installing

```shell
composer require cccaimingjian/laravel-image-watermark -vvv
```

## Usage

TODO

模版来源与超哥的项目,如果你喜欢超哥的项目并想支持他,[点击这里 :heart:](https://github.com/sponsors/overtrue)
## PHP 扩展包开发

> 想知道如何从零开始构建 PHP 扩展包?
>
> 请关注超哥的实战课程,
> 超哥在此课程中分享了一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package?rf=81208)
## I bought a JetBrains license

Waiting for Jetbrains to sponsor me an open-source projects license

![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)

## License

MIT
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "cccaimingjian/laravel-image-watermark",
"description": "Make the whole image full of watermarks",
"keywords": [
"watermarks"
],
"license": "MIT",
"authors": [
{
"name": "cccaimingjian",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.4|>=8.0",
"ext-gd": "*"
},
"autoload": {
"psr-4": {
"cccaimingjian\\ImageWatermark\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"require-dev": {
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true
}
}
}
189 changes: 189 additions & 0 deletions src/Maker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

namespace cccaimingjian\ImageWatermark;

class Maker
{
protected int $w_interval = 50;
protected int $h_interval = 50;
protected float $str_angel = 15.0;
protected int $color = 0x30BEBEBE;
protected int $pic_w = 0;
protected int $pic_h = 0;
protected $image = null;
protected string $font_path = './font/SourceHanSerifSC-Medium.otf';
public string $file_path = '';
public string $string = 'watermark';
public float $font_size = 10.0;

public function __construct(string $original_path = '')
{
if ($original_path){
$this->file_path = $original_path;
}
}

/**
* Set the image path (Original image)
* 设置你要添加水印的那张图片的路径(原图)
* @param string $file_path
*/
public function setInputFilePath(string $file_path){
$this->file_path = $file_path;
}

/**
* Set the angle of the watermark string
* 设置水印字符的角度
* @param float $angle
* The angle in degrees, with 0 degrees being left-to-right reading text.
* Higher values represent a counter-clockwise rotation.
* For example, a value of 90 would result in bottom-to-top reading text.
*/
public function setAngle(float $angle){
$this->str_angel = $angle;
}

/**
* Set the font size of the watermark string
* 设置水印字体大小
* @param float $size
* The font size. Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2).
*/
public function setFontSize(float $size){
$this->font_size = $size;
}

/**
* Set the watermark string
* 设置水印的内容
* @param $string
* the string you want to add
*
*/
public function setWatermarkString(string $string){
$this->string = $string;
}

/**
* Set the color of the watermark string
* 设置水印内容的颜色
* @param int $color
* The color index. Using the negative of a color index has the effect of turning off antialiasing. See imagecolorallocate.
*/
public function setWatermarkColor(int $color){
$this->color = $color;
}

/**
* Set the interval of width
* 设置水印横向的间隔
* @param $interval int
* the pixel between watermark's START in horizontal direction
* 注意是开头的间隔,间隔大小请根据水印的字符长度合理设置
*/
public function setWatermarkWidthInterval(int $interval){
$this->w_interval = $interval;
}

/**
* Set the interval of height
* 设置水印纵向的间隔
* @param $interval
* the pixel between watermark in vertical direction
*/
public function setWatermarkHeightInterval(int $interval){
$this->h_interval = $interval;
}

/**
* Set the font path
* 设置水印字体文件
* @param $font_path
* the font file path
*/
public function setWatermarkFont(string $font_path){
$this->font_path = $font_path;
}

/**
* draw watermark
* 画水印
* @throws Exception
*/
public function drawWatermark(){
if (!$this->image){
$this->loadPicture();
}
for ($i = 0; $i<=$this->pic_w; $i = $i + $this->w_interval){
for ($j = 0; $j<=$this->pic_h; $j = $j + $this->h_interval){
imagettftext($this->image,
$this->font_size,
$this->str_angel,
$i,$j,
$this->color,
$this->font_path,$this->string);
}
}
}

/**
* encode the image to JPG
* @param string $file_name [optional]
* The path to save the file to. If not set or null, return the image content.
* @return bool|string
*/
public function encodeToJPG(string $file_name = ''){
if (!$file_name){
ob_start();
imagejpeg($this->image);
return ob_get_clean();
}
return imagejpeg($this->image,$file_name);
}

/**
* encode the image to PNG
* @param string $file_name
* @return bool|string
*/
public function encodeToPNG(string $file_name = ''){
if (!$file_name){
ob_start();
imagepng($this->image);
return ob_get_clean();
}
return imagepng($this->image,$file_name);
}

/**
* @return null|GdImage
*/
public function getGdImage(){
return $this->image;
}

/**
* Load the image into Memory
* 读取图片
* @return bool
* @throws Exception
*/
protected function loadPicture()
{
if (!file_exists($this->file_path)){
throw new Exception('File: "'.$this->file_path.'" does not exist');
}
$image_data = file_get_contents($this->file_path);
if (!$image_data){
throw new Exception('file_get_contents() can NOT get the file:"'.$this->file_path.'"');
}
$this->image = imagecreatefromstring($image_data);
if (!$this->image){
throw new Exception('Can NOT Load the file:"'.$this->file_path.'", please confirm the file is an image');
}
$this->pic_w = imagesx($this->image);
$this->pic_h = imagesy($this->image);
return true;
}
}
Binary file added src/font/SourceHanSerifSC-Medium.otf
Binary file not shown.

0 comments on commit 575f52f

Please sign in to comment.