gpt4 book ai didi

php - 调整图像大小并将每个图像放入正方形中,保持纵横比(后端)

转载 作者:行者123 更新时间:2023-12-03 09:19:41 25 4
gpt4 key购买 nike

我对 PHP 和图像没有任何经验,但我需要在上传图像时将所有图像调整为正方形。

由于我有多种产品,并且它们有不同尺寸的不同图片,我认为最好的方法是在上传过程中“标准化”它们,方法是拍摄图像并将其“适合”到 800x800 白色方 block 中,同时保持长宽比(如果最长尺寸 >800,则缩小尺寸,如果最长尺寸 <800,则重新调整尺寸)。

我最初的解决方案是通过 JavaScript 创建的,我尝试找到最大的图片并根据它调整所有其他图像的大小。不过,它不是很有用,而且可能会出错,因为如果图像加载有延迟,JS 可能无法加载图像来执行操作,从而根本不显示图像。

$product = getProductById($productid);

$filesArray = array();


if (isset($_GET['files']) && $productid > 0) {
$error = false;
$files = array();
$fileName = '';

$uploaddir = "../images/products/";
foreach ($_FILES as $file) {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$random_name = $widgets->randomFileName();
$randomFullFileName = $productid .'_'. $random_name . '.' . $ext;

//copy to location
//----------------
//HERE I NEED TO CREATE A 800x800px SQUARE and fit the image into it
//----------------
if (move_uploaded_file($file['tmp_name'], $uploaddir . $randomFullFileName)) {

//save to database
$image_id = updateProduct('default_image', 'images/products/'.$randomFullFileName, $productid);

if ($image_id > 0) {
echo 'Added new image';
} else {
echo 'Error, unable to add. <br> Please try again.';
}
} else {
echo 'Error, unable to add. <br> Please try again.';
}
}
}

之前:600x300 BEFORE

之后:800x800,用白色边框填充空间 AFTER

最佳答案

您可以尝试这种方法(已测试)。它将把您的图像放入 800x800 的方形 Canvas 中,同时保持图像的纵横比。

resize_image():此函数将保持图像的宽高比。

function resize_image($img,$maxwidth,$maxheight) {
//This function will return the specified dimension(width,height)
//dimension[0] - width
//dimension[1] - height

$dimension = array();
$imginfo = getimagesize($img);
$imgwidth = $imginfo[0];
$imgheight = $imginfo[1];
if($imgwidth > $maxwidth){
$ratio = $maxwidth/$imgwidth;
$newwidth = round($imgwidth*$ratio);
$newheight = round($imgheight*$ratio);
if($newheight > $maxheight){
$ratio = $maxheight/$newheight;
$dimension[] = round($newwidth*$ratio);
$dimension[] = round($newheight*$ratio);
return $dimension;
}else{
$dimension[] = $newwidth;
$dimension[] = $newheight;
return $dimension;
}
}elseif($imgheight > $maxheight){
$ratio = $maxheight/$imgheight;
$newwidth = round($imgwidth*$ratio);
$newheight = round($imgheight*$ratio);
if($newwidth > $maxwidth){
$ratio = $maxwidth/$newwidth;
$dimension[] = round($newwidth*$ratio);
$dimension[] = round($newheight*$ratio);
return $dimension;
}else{
$dimension[] = $newwidth;
$dimension[] = $newheight;
return $dimension;
}
}else{
$dimension[] = $imgwidth;
$dimension[] = $imgheight;
return $dimension;
}
}

现在是你的代码了,

$product = getProductById($productid);

$filesArray = array();


if (isset($_GET['files']) && $productid > 0) {
$error = false;
$files = array();
$fileName = '';

$uploaddir = "../images/products/";
foreach ($_FILES as $file) {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$random_name = $widgets->randomFileName();
$randomFullFileName = $productid .'_'. $random_name . '.' . $ext;

//copy to location
//----------------
//HERE I NEED TO CREATE A 800x800px SQUARE and fit the image into it


// Create image from file
$image = null;
switch(strtolower($file['type']))
{
case 'image/jpeg':
$image = imagecreatefromjpeg($file['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($file['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($file['tmp_name']);
break;
default:
exit('Unsupported type: '.$file['type']);
}

// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);

// Get the new dimensions
$dimension = resize_image($file, 800, 800);
$new_width = $dimension[0];
$new_height = $dimension[1];

// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);

// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

// Catch and save the image
$status = false;
switch(strtolower($file['type']))
{
case 'image/jpeg':
$status = imagejpeg($new, $uploaddir . $randomFullFileName, 90);
break;
case 'image/png':
$status = imagepng($new, $uploaddir . $randomFullFileName, 0);
break;
case 'image/gif':
$status = imagegif($new, $uploaddir . $randomFullFileName);
break;
}

// Destroy resources
imagedestroy($image);
imagedestroy($new);

//save to database
if($status){
$image_id = updateProduct('default_image', 'images/products/'.$randomFullFileName, $productid);

if ($image_id > 0) {
echo 'Added new image';
} else {
echo 'Error, unable to add. <br> Please try again.';
}
}else{
echo 'Error, unable to add. <br> Please try again.';
}

//----------------
//if (move_uploaded_file($file['tmp_name'], $uploaddir . $randomFullFileName)) {
// //save to database
// $image_id = updateProduct('default_image', 'images/products/'.$randomFullFileName, $productid);

// if ($image_id > 0) {
// echo 'Added new image';
// } else {
// echo 'Error, unable to add. <br> Please try again.';
// }
//} else {
// echo 'Error, unable to add. <br> Please try again.';
//}

}
}

关于php - 调整图像大小并将每个图像放入正方形中,保持纵横比(后端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33928843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com