PHP - 如何将矩形图像转换为方形图像? [英] PHP - How do I convert a rectangle image to a square image?

查看:282
本文介绍了PHP - 如何将矩形图像转换为方形图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在php中将矩形图像更改为方形头像,这样无论上传图像的分辨率如何,它都可以调整为集中的42 x 42像素化身。这是我正在使用的PHP代码。任何人都可以提供建议。

How do I change a rectangular image to a square-shaped avatar in php, such that no matter what is the resolution of the uploaded image, it is able to resize to a centralized 42 x 42 pixel avatar. This is the php code I am using. Anyone can advise.

<?php 
//Name you want to save your file as
$save = 'myfile1.jpg';

$file = 'original1.jpg'; 
echo "Creating file: $save"; 
$size = 0.45; 
header('Content-type: image/jpeg') ; 
list($width, $height) = getimagesize($file) ; 
$modwidth = $width * $size; 
$modheight = $height * $size; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
$image = imagecreatefromjpeg($file) ; 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

// Here we are saving the .jpg, you can make this gif or png if you want
//the file name is set above, and the quality is set to 100%
imagejpeg($tn, $save, 100) ; 
?> 


推荐答案

首先你需要告诉中央广场在哪里在尺寸为$ x和$ y的矩形中。

First you need to be tell where is the central square in a rectangle with dimensions $x and $y.

// horizontal rectangle
if ($x > $y) {
    $square = $y;              // $square: square side length
    $offsetX = ($x - $y) / 2;  // x offset based on the rectangle
    $offsetY = 0;              // y offset based on the rectangle
}
// vertical rectangle
elseif ($y > $x) {
    $square = $x;
    $offsetX = 0;
    $offsetY = ($y - $x) / 2;
}
// it's already a square
else {
    $square = $x;
    $offsetX = $offsetY = 0;
}

现在我们可以从中构建一个正方形,只需将其调整为42x42 。这样的事情应该有效:

Now we can build a square from it, just need to resize it to 42x42. Something like this should work:

list($x, $y) = getimagesize($file);

// code snippet from above goes here
// so we get the square side and the offsets

$endSize = 42;
$tn = imagecreatetruecolor($endSize, $endSize);
imagecopyresampled($tn, $image, 0, 0, $offsetX, $offsetY, $endSize, $endSize, $square, $square);






所以,如果我们有100x80的矩形图像,代码会发现大方块大小是80,x偏移是10,y偏移是0.大致看起来像这样:


So, if we have a rectangular image 100x80, the code will figure out that the big square size is 80, x offset is 10, y offset is 0. Roughly, it looks like this:

    100
-----------
|         |
|         | 80
|         |
-----------

     |
     V

    80
 ---------               42
 |       |             -----
 |       | 80   --->   |   | 42
 |       |             -----
 ---------

我们裁剪后原始矩形的大正方形,我们只是缩小到最终尺寸,在你的情况下是42。

After we crop the big square from the original rectangle, we just shrink it to the end size, which is 42 in your case.

刚刚测试并且工作正常,如果您打算将图像输出到浏览器(与标题结合),请确保删除回显线。

Just tested and works perfectly, make sure you remove the echo line if you plan to output the image into the browser (combined with the header).

这篇关于PHP - 如何将矩形图像转换为方形图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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