Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2014
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. <?php
  2.  
  3. class CircleCrop
  4. {
  5.  
  6. private $src_img;
  7. private $src_w;
  8. private $src_h;
  9. private $dst_img;
  10. private $dst_w;
  11. private $dst_h;
  12.  
  13. public function __construct($img)
  14. {
  15. $this->src_img = $img;
  16. $this->src_w = imagesx($img);
  17. $this->src_h = imagesy($img);
  18. $this->dst_w = imagesx($img);
  19. $this->dst_h = imagesy($img);
  20. }
  21.  
  22. public function __destruct()
  23. {
  24. if (is_resource($this->dst_img))
  25. {
  26. imagedestroy($this->dst_img);
  27. }
  28. }
  29.  
  30. public function display()
  31. {
  32. header("Content-type: image/png");
  33. imagepng($this->dst_img);
  34. return $this;
  35. }
  36.  
  37. public function reset()
  38. {
  39. if (is_resource(($this->dst_img)))
  40. {
  41. imagedestroy($this->dst_img);
  42. }
  43. $this->dst_img = imagecreatetruecolor($this->dst_w, $this->dst_h);
  44. imagecopy($this->dst_img, $this->src_img, 0, 0, 0, 0, $this->dst_w, $this->dst_h);
  45. return $this;
  46. }
  47.  
  48. public function size($dstWidth, $dstHeight)
  49. {
  50. $this->dst_w = $dstWidth;
  51. $this->dst_h = $dstHeight;
  52. return $this->reset();
  53. }
  54.  
  55. public function crop()
  56. {
  57. // Intializes destination image
  58. $this->reset();
  59.  
  60. // Create a black image with a transparent ellipse, and merge with destination
  61. $mask = imagecreatetruecolor($this->dst_w, $this->dst_h);
  62. $maskTransparent = imagecolorallocate($mask, 255, 0, 255);
  63. imagecolortransparent($mask, $maskTransparent);
  64. imagefilledellipse($mask, $this->dst_w / 2, $this->dst_h / 2, $this->dst_w, $this->dst_h, $maskTransparent);
  65. imagecopymerge($this->dst_img, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h, 100);
  66.  
  67. // Fill each corners of destination image with transparency
  68. $dstTransparent = imagecolorallocate($this->dst_img, 255, 0, 255);
  69. imagefill($this->dst_img, 0, 0, $dstTransparent);
  70. imagefill($this->dst_img, $this->dst_w - 1, 0, $dstTransparent);
  71. imagefill($this->dst_img, 0, $this->dst_h - 1, $dstTransparent);
  72. imagefill($this->dst_img, $this->dst_w - 1, $this->dst_h - 1, $dstTransparent);
  73. imagecolortransparent($this->dst_img, $dstTransparent);
  74.  
  75. return $this;
  76. }
  77.  
  78. }
  79.  
  80. $bg = '#EEEEEE';
  81. if (array_key_exists('bg', $_POST))
  82. {
  83. if (preg_match("|^#[0-9a-fA-F]{6}$|", $_POST['bg']))
  84. {
  85. $bg = $_POST['bg'];
  86. }
  87. }
  88.  
  89. $url = null;
  90.  
  91. if (array_key_exists('url', $_GET))
  92. {
  93. $url = $_GET['url'];
  94. if (filter_var($url, FILTER_VALIDATE_URL) !== false)
  95. {
  96. $img = imagecreatefromjpeg($url);
  97. $crop = new CircleCrop($img);
  98. $crop->crop()->display();
  99. }
  100. die();
  101. }
  102.  
  103. if (array_key_exists('url', $_POST))
  104. {
  105. $url = $_POST['url'];
  106. if (filter_var($url, FILTER_VALIDATE_URL) !== false)
  107. {
  108. $url = $_POST['url'];
  109. }
  110. }
  111.  
  112. ?>
  113.  
  114. <html>
  115. <body style="background-color: <?php echo $bg; ?>;">
  116.  
  117. <form action="test-circle-crop.php" method="post">
  118. <div>Enter image URL:</div>
  119. <input style="width:100%" type="text" name="url" value="http://stylonica.com/wp-content/uploads/2014/03/Cute-Dog-Wallpaper.jpg" />
  120. <br/>
  121. <div>Enter background color:</div>
  122. <input style="width:100%" type="text" name="bg" value="#EEEEEE" />
  123. <br/>
  124. <input type="submit" value="CircleCrop!" />
  125. </form>
  126.  
  127. <?php if (!is_null($url)): ?>
  128.  
  129. <img src="test-circle-crop.php?url=<?php echo urlencode($url); ?>">
  130.  
  131. <?php endif ?>
  132.  
  133. </body>
  134. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement