php按比例调整大小并从方形图像中切出一个圆圈

时间:2010-12-22 18:40:03

标签: php imagemagick gd

我有许多不同尺寸的方形图像,我想首先调整它们的大小,使它们位于150像素区域内(因此图像不会失真,因此大多数图像的高度和宽度都不完全相同)两边会比较小(按比例)。

然后,一旦我这样做,我需要从它们中切出一个完美的圆圈并应用一个10像素的彩色边框。

现在我怎么能开始呢?

1 个答案:

答案 0 :(得分:2)

好吧,你从磁盘上获取文件,然后用它们创建一个新图像,如下所示:

//Would want to use imagecreatefromgif or imagecreatefrompng, depending on file type.
//Loading up the image so we can get it's dimensions and determine the proper size.
$maxsize = 150;
$img = imagecreatefromjpeg("$jpgimage"); 


$width = imagesx($img);
$height = imagesy($img); //Get height and width

//This stuff figures out the ratio to reduce the shortest side by by using the longest side, since 
//the longest side will be the new maximum length
if ($height > $width) 
{   
$ratio = $maxsize / $height;  
$newheight = $maxsize;
$newwidth = $width * $ratio; 
{
else 
{
$ratio = $maxsize / $width;   
$newwidth = $maxsize;  
$newheight = $height * $ratio;   
}

//create new image resource to hold the resized image
$newimg = imagecreatetruecolor($newwidth,$newheight); 

$palsize = ImageColorsTotal($img);  //Get palette size for original image
for ($i = 0; $i < $palsize; $i++) //Assign color palette to new image
{ 
$colors = ImageColorsForIndex($img, $i);   
ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']);
} 

//copy original image into new image at new size.
imagecopyresized($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//Get a color for the circle, in this case white.
$circlecol = imagecolorallocate($newimg,255,255,255);
//draw circle at center point, or as close to center as possible, with a width and height of 150
//use imagefilledellipse for a filled circle
imageellipse($newimg, round($newwidth / 2), round($newheight / 2), 150, 150, $circlecol);

圆圈是否需要镶边或整个图像?

相关问题