Share Pastebin
Guest
Public paste!

saka

By: a guest | Mar 21st, 2010 | Syntax: PHP | Size: 1.98 KB | Hits: 86 | Expires: Never
Copy text to clipboard
  1. <?php
  2.  
  3.  
  4. $imgpath = "source.jpg";
  5. $corner = 20;
  6.  
  7. $image = open_image($imgpath);
  8.  
  9. image_roundcorners($image, $corner);
  10.  
  11. //display rounded image
  12. imagepng($image,"output.png");
  13. echo '<html><body bgcolor="#888888" style="text-align:center;padding-top:100px">';
  14. echo '<img src="'.$imgpath.'" /> <img src="output.png" />';
  15. echo '</body></html>';
  16. imagedestroy($image);
  17.  
  18.  
  19. // image_roundcorners() - takes a single image by reference and puts transparent rounded corners on it with radius $corner
  20. // XXX currently does not support antialiasing :(
  21. function image_roundcorners(&$image, $corner) {
  22.  
  23.         $width = imagesx($image);
  24.         $height = imagesy($image);
  25.        
  26.         imagealphablending($image, false); // we'd like the alpha we draw to stick
  27.         imagesavealpha($image,true);
  28.  
  29.         // allocate an alpha transparent color
  30.         $trans = imagecolorallocatealpha($image, 255, 255, 255, 127);
  31.  
  32.         //draw corners
  33.         imagearc($image, $corner-1, $corner-1, $corner*2, $corner*2, 180, 270, $trans);
  34.         imagefilltoborder($image, 0, 0, $trans, $trans);
  35.  
  36.         imagearc($image, $width-$corner, $corner-1, $corner*2, $corner*2, 270, 0, $trans);
  37.         imagefilltoborder($image, $width, 0, $trans, $trans);
  38.  
  39.         imagearc($image, $corner-1, $height-$corner, $corner*2, $corner*2, 90, 180, $trans);
  40.         imagefilltoborder($image, 0, $height, $trans, $trans);
  41.  
  42.         imagearc($image, $width-$corner, $height-$corner, $corner*2, $corner*2, 0, 90, $trans);
  43.         imagefilltoborder($image, $height, $height, $trans, $trans);
  44.  
  45.         imagealphablending($image, true); // turn blending back on     
  46. }
  47.  
  48.  
  49. // open_image($path) will load an image into memory so we can work with it, and return an error if we fail
  50. function open_image($path) {
  51.         if (!is_string($path)) return $path; // XXX assume binary data is a gd image already
  52.         $image = @imagecreatefromstring(file_get_contents($path));
  53.         if (!$image) die("could not open image ($path) make sure it exists");
  54.         imagealphablending($image,true); imagesavealpha($image,true); // preserve transparency
  55.         return $image;
  56. }
  57.  
  58.  
  59. ?>