0

I want to convert a square image into circle shape in php

suppose I have this Image enter image description here

I want to convert its shape to circle. Is there any way ?

Which free library I can use ?

I want to save this image.

Thanks.

Vinay
  • 57
  • 1
  • 11
  • All images are rectangles. To make a circle, you draw a circle inside the rectangle. The image is still a rectangle. – kainaw May 23 '16 at 16:42
  • Possible duplicate of [Transparent Circle Cropped Image with PHP](http://stackoverflow.com/questions/12953262/transparent-circle-cropped-image-with-php) – Felippe Duarte May 23 '16 at 16:44

2 Answers2

2
<?php

// Created by NerdsOfTech



// Step 1 - Start with image as layer 1 (canvas).

$img1 = ImageCreateFromjpeg("img.jpg");

$x=imagesx($img1)-$width ;

$y=imagesy($img1)-$height;





 // Step 2 - Create a blank image.

 $img2 = imagecreatetruecolor($x, $y);

$bg = imagecolorallocate($img2, 255, 255, 255); // white background

 imagefill($img2, 0, 0, $bg);





 // Step 3 - Create the ellipse OR circle mask.

 $e = imagecolorallocate($img2, 0, 0, 0); // black mask color



 // Draw a ellipse mask

 // imagefilledellipse ($img2, ($x/2), ($y/2), $x, $y, $e);



 // OR 

 // Draw a circle mask

$r = $x <= $y ? $x : $y; // use smallest side as radius & center shape

imagefilledellipse ($img2, ($x/2), ($y/2), $r, $r, $e); 





 // Step 4 - Make shape color transparent

 imagecolortransparent($img2, $e);





 // Step 5 - Merge the mask into canvas with 100 percent opacity

 imagecopymerge($img1, $img2, 0, 0, 0, 0, $x, $y, 100);




 // Step 6 - Make outside border color around circle transparent

imagecolortransparent($img1, $bg);



 // Step 7 - Output merged image

 header("Content-type: image/png"); // output header

 $input = imagepng($img1); // output merged image

 $output = 'vola.png';

 file_put_contents($output, file_get_contents($input));

// Step 8 - Cleanup memory

 imagedestroy($img2); // kill mask first

 imagedestroy($img1); // kill canvas last

 ?>

Sir I got this code working. Just unable to save this to my folder.

Thanks.

Vinay
  • 57
  • 1
  • 11
0

You can do very easily using css only.

.rounded-image {
  border-radius: 160px;
  overflow: hidden;
}
<img src="https://i.stack.imgur.com/mhjeT.jpg" class="rounded-image" />

If you want solution with php. You can check this question Rounded corners on images using PHP?

Community
  • 1
  • 1
Dezefy
  • 2,048
  • 1
  • 14
  • 23
  • I will be using this furthr sir. this is just a display by this I wont be able to save this image. Thanks. – Vinay May 23 '16 at 16:54