note 88173 deleted from function.imagecolortransparent by cmb
| From: | cmb@php.net | Date: | Sun, 01 Dec 2019 08:42:37 +0000 |
| Subject: | note 88173 deleted from function.imagecolortransparent by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213314@lists.php.net to get a copy of this message | ||
Note Submitter: designovermatter at gmail dot com
----
I was searching for a PHP code that makes transparent rounded corners and found none. So had to
write my own:
<?php
$image = "v.png";
$corner = 40;
file_exists($image) or die("No such file: ".$image); //check if image exists before
processing
$dim = getimagesize($image);
//create from type handle
if($dim[2] == 1) $new = imagecreatefromjpeg($image);
elseif($dim[2] == 2) $new = imagecreatefromgif($image);
elseif($dim[2] == 3) $new = imagecreatefrompng($image);
else die("Unsupported format: ".$dim[2]);
//find colorcode
$palette = imagecreatetruecolor($dim[0], $dim[1]);
$found = false;
while($found == false) {
$r = rand(0, 255);
$g = rand(0, 255);
$b = rand(0, 255);
if(imagecolorexact($new, $r, $g, $b) != (-1)) {
$colorcode = imagecolorallocate($palette, $r, $g, $b);
$found = true;
}
}
//draw corners
imagearc($new, $corner-1, $corner-1, $corner*2, $corner*2, 180, 270, $colorcode);
imagefilltoborder($new, 0, 0, $colorcode, $colorcode);
imagearc($new, $dim[0]-$corner, $corner-1, $corner*2, $corner*2, 270, 0, $colorcode);
imagefilltoborder($new, $dim[0], 0, $colorcode, $colorcode);
imagearc($new, $corner-1, $dim[1]-$corner, $corner*2, $corner*2, 90, 180, $colorcode);
imagefilltoborder($new, 0, $dim[1], $colorcode, $colorcode);
imagearc($new, $dim[0]-$corner, $dim[1]-$corner, $corner*2, $corner*2, 0, 90, $colorcode);
imagefilltoborder($new, $dim[1], $dim[1], $colorcode, $colorcode);
imagecolortransparent($new, $colorcode); //make corners transparent
//display rounded image
header("Content: image/png");
imagepng($new);
imagedestroy($new);
?>
It draws an arc and fills to border with a colorcode - a color that doesnot exist in current image,
to make it transparent in the end.