将图像形状从正方形转换为圆形

时间:2016-05-23 16:39:54

标签: php

我想在php中将方形图像转换为圆形

假设我有这张图片enter image description here

我想将其形状转换为圆形。有什么办法吗?

我可以使用哪个免费图书馆?

我想保存此图片。

感谢。

2 个答案:

答案 0 :(得分:0)

你可以很容易地使用css。



.rounded-image {
  border-radius: 160px;
  overflow: hidden;
}

<img src="http://i.stack.imgur.com/mhjeT.jpg" class="rounded-image" />
&#13;
&#13;
&#13;

如果你想用php解决方案。您可以查看此问题Rounded corners on images using PHP?

答案 1 :(得分:0)

<?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

 ?>

先生,我得到了这段代码。只是无法将其保存到我的文件夹中。

感谢。