Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
83 changes: 83 additions & 0 deletions CircleCrop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Created by PhpStorm.
* User: kushagra
* Date: 05/10/17
* Time: 11:17 PM
*/

namespace App;


class CircleCrop
{
private $src_img;
private $src_w;
private $src_h;
private $dst_img;
private $dst_w;
private $dst_h;

public function __construct($img, $dstWidth, $dstHeight)
{
$this->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;
}
}
14 changes: 12 additions & 2 deletions code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );

Expand Down