Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class CircleCrop
- {
- private $src_img;
- private $src_w;
- private $src_h;
- private $dst_img;
- private $dst_w;
- private $dst_h;
- public function __construct($img)
- {
- $this->src_img = $img;
- $this->src_w = imagesx($img);
- $this->src_h = imagesy($img);
- $this->dst_w = imagesx($img);
- $this->dst_h = imagesy($img);
- }
- 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()
- {
- // Intializes destination image
- $this->reset();
- // Create a black image with a transparent ellipse, and merge with destination
- $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);
- // Fill each corners of destination image with transparency
- $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;
- }
- }
- $bg = '#EEEEEE';
- if (array_key_exists('bg', $_POST))
- {
- if (preg_match("|^#[0-9a-fA-F]{6}$|", $_POST['bg']))
- {
- $bg = $_POST['bg'];
- }
- }
- $url = null;
- if (array_key_exists('url', $_GET))
- {
- $url = $_GET['url'];
- if (filter_var($url, FILTER_VALIDATE_URL) !== false)
- {
- $img = imagecreatefromjpeg($url);
- $crop = new CircleCrop($img);
- $crop->crop()->display();
- }
- die();
- }
- if (array_key_exists('url', $_POST))
- {
- $url = $_POST['url'];
- if (filter_var($url, FILTER_VALIDATE_URL) !== false)
- {
- $url = $_POST['url'];
- }
- }
- ?>
- <html>
- <body style="background-color: <?php echo $bg; ?>;">
- <form action="test-circle-crop.php" method="post">
- <div>Enter image URL:</div>
- <input style="width:100%" type="text" name="url" value="http://stylonica.com/wp-content/uploads/2014/03/Cute-Dog-Wallpaper.jpg" />
- <br/>
- <div>Enter background color:</div>
- <input style="width:100%" type="text" name="bg" value="#EEEEEE" />
- <br/>
- <input type="submit" value="CircleCrop!" />
- </form>
- <?php if (!is_null($url)): ?>
- <img src="test-circle-crop.php?url=<?php echo urlencode($url); ?>">
- <?php endif ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement