diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b6a4b86 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/public/hot +/public/storage +/storage/*.key +/vendor +/.idea +/.vagrant +Homestead.json +Homestead.yaml +npm-debug.log +yarn-error.log +.env diff --git a/CircleCrop.php b/CircleCrop.php new file mode 100644 index 0000000..ce929ac --- /dev/null +++ b/CircleCrop.php @@ -0,0 +1,83 @@ +src_img = $img; + $this->src_w = imagesx($img); + $this->src_h = imagesy($img); + $this->dst_w = $dstWidth; + $this->dst_h = $dstHeight; + } + + public function __destruct() + { + if (is_resource($this->dst_img)) + { + imagedestroy($this->dst_img); + } + } + + public function display() + { + header("Content-type: image/png"); + imagepng($this->dst_img); + return $this; + } + + public function reset() + { + if (is_resource(($this->dst_img))) + { + imagedestroy($this->dst_img); + } + $this->dst_img = imagecreatetruecolor($this->dst_w, $this->dst_h); + imagecopy($this->dst_img, $this->src_img, 0, 0, 0, 0, $this->dst_w, $this->dst_h); + return $this; + } + + public function size($dstWidth, $dstHeight) + { + $this->dst_w = $dstWidth; + $this->dst_h = $dstHeight; + return $this->reset(); + } + + public function crop() + { + $this->reset(); + + $mask = imagecreatetruecolor($this->dst_w, $this->dst_h); + $maskTransparent = imagecolorallocate($mask, 255, 0, 255); + imagecolortransparent($mask, $maskTransparent); + imagefilledellipse($mask, $this->dst_w / 2, $this->dst_h / 2, $this->dst_w, $this->dst_h, $maskTransparent); + + imagecopymerge($this->dst_img, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h, 100); + + $dstTransparent = imagecolorallocate($this->dst_img, 255, 0, 255); + imagefill($this->dst_img, 0, 0, $dstTransparent); + imagefill($this->dst_img, $this->dst_w - 1, 0, $dstTransparent); + imagefill($this->dst_img, 0, $this->dst_h - 1, $dstTransparent); + imagefill($this->dst_img, $this->dst_w - 1, $this->dst_h - 1, $dstTransparent); + imagecolortransparent($this->dst_img, $dstTransparent); + + return $this; + } +} \ No newline at end of file diff --git a/code.php b/code.php index c146535..caf9b33 100755 --- a/code.php +++ b/code.php @@ -23,6 +23,8 @@ */ +use App\CircleCrop; + if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { // If the browser has a cached version of this image, send 304 header( 'Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304 ); @@ -200,8 +202,16 @@ function( $matches ) { //Determine where to set the Y position of the text box so it is centered $textY = ceil( ( $height - $textHeight ) / 2 + $textHeight ); -//Create the rectangle with the specified background color -imageFilledRectangle( $img, 0, 0, $width, $height, $bg_color ); + +if (isset($_GET['circular']) && $_GET['circular'] == 'true') { + $width = imagesx($img); + $height = imagesy($img); + $img = new CircleCrop($img, $width, $height); +} else { + //Create the rectangle with the specified background color + imageFilledRectangle( $img, 0, 0, $width, $height, $bg_color ); +} + //Create and positions the text imagettftext( $img, $fontsize, $text_angle, $textX, $textY, $fg_color, $font, $text );