php按比例调整大小并从正方形图像中切出一个圆圈 [英] php proportionally resize and cut a circle out of a square image

查看:150
本文介绍了php按比例调整大小并从正方形图像中切出一个圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have many square images with different sizes, and I want to first resize them so they are inside a 150pixel area (so the images are not distorted, so since most images are not exactly the same size on both height and width one of the sides would be smaller (proportionally).

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

Then once I do that I need to cut a perfect circle out of them and apply a 10pixel colored border.

现在我怎么能开始呢?

推荐答案

嗯,你从磁盘中获取文件,然后使用它们创建新图像,如下所示:

Well, you take your files from disk or however and then you use them to create a new image, like so:

//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);

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

Does the circle need to be bordered or the image as a whole?

这篇关于php按比例调整大小并从正方形图像中切出一个圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆