0

I'm trying to create a image with rounded border in Php.

I tried with this but only creates a white circle:

<?php
  header('Content-type: image/jpeg');
  $img = imagecreatefromjpeg('person_1.jpg');
  $color = imagecolorallocate($img, 255, 255, 255);
  imagefilledellipse($img , 300, 300, 600, 600, $color);
  imagejpeg($img);
  imagedestroy($img);
?>

I'm trying to do something like (sorry for the bad example): https://i.stack.imgur.com/A7ueN.jpg

And I get this: https://i.stack.imgur.com/Dfw5I.jpg

Hope you can help me with this, thank you.

Ultraviolet
  • 31
  • 1
  • 6
  • Welcome to Stack Overflow. Please [edit] your question to include a screenshot of the image you get right now and the image you want to generate instead. – Progman Dec 15 '18 at 22:46
  • Done, I updated the post with example images. – Ultraviolet Dec 15 '18 at 22:50
  • Why is it needs to be done in PHP but not CSS? a second thought! – Anand G Dec 15 '18 at 22:52
  • Do you need it done with PHP? Consider CSS for the task, it's much easier – Itay Gal Dec 15 '18 at 22:53
  • 1
    It's best to just send the image and let the browser mask it using CSS. Processing images in PHP (or any server-side code for that matter) is a huge misuse of resources. Let the user's browser do some of the work. – Havenard Dec 15 '18 at 23:00
  • The original idea was create the image and then download it. I know how to make it in CSS. – Ultraviolet Dec 16 '18 at 00:35

1 Answers1

1

Just in case you can do it with CSS and not PHP, it will probably be better and faster. Create a div and set the background image to your image, then set the radius border to 50%, this will create a circle.

.rounded {
  background-image: url("http://itayg.com/gallery/landscape/ThreeLagoons.jpg");
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background-repeat: no-repeat;
  background-size: cover;
}
<div class="rounded"></div>
Itay Gal
  • 10,706
  • 6
  • 36
  • 75
  • 1
    I agree with the reasoning behind giving this answer, but I think OP might have his reasons for rounding it server side. Potentially involves saving it, cutting extra pixels might let it take up less space? – dGRAMOP Dec 15 '18 at 23:06
  • 1
    @dGRAMOP Resizing I get, but throwing information away for a marginal reduction in file size is a silly thing to do. And to make matters worse, it won't look as good as doing it in CSS because image compression will add noise to the circle. – Havenard Dec 15 '18 at 23:09
  • Agreed, but not the question OP was asking. I would never personally use imagick to do something like this. – dGRAMOP Dec 15 '18 at 23:11
  • 1
    The original idea was create the image and then download it. I know how to make it in CSS. – Ultraviolet Dec 16 '18 at 00:35