gpt4 book ai didi

php - 在使用临时文件夹上传之前调整多个图像大小

转载 作者:搜寻专家 更新时间:2023-10-30 23:39:00 26 4
gpt4 key购买 nike

我需要这方面的帮助,我花了一整天的时间尝试用 PHP 调整图片的大小:(((。

我找到了一个好主题Resize the image-file before uploading; Could we overwrite the temp file & upload?

但是,当我尝试为我使用我的 add.php 时,会返回一个像这样的白色小方 block :enter image description here

我的图片在我的文件夹中没有调整大小。

我可能忘记更改代码中的某些内容或无法使用但是,我真的需要帮助谢谢。

require_once('../connexion.php');

extract($_POST);

// This will count total images selected.
$TotalImage = count($_FILES['image']['name']);
$domaine = isset($_POST['domaine']) ? $_POST['domaine'] : null;

for($i = 0;$i<$TotalImage;$i++)
{
$tempFile = $_FILES['image']['tmp_name'][$i];
$image_name = $_FILES['image']['name'][$i];
$ext = strtolower(substr(strrchr($image_name, '.'), 1));
$target_path = $_SERVER['DOCUMENT_ROOT'].'/AprimeWeb/img/sliders/'.$domaine .'/'.$image_name;

$image_properties = getimagesize($tempFile);
$image_width = $image_properties[0];
$image_height = $image_properties[1];
$percent = 0.5; //Percentage by which the image is going to be resized

$newWidth = $image_width * $percent;
$newHeight = $image_height * $percent;

if ($ext == "jpeg" || $ext == "jpg") {
header('Content-type: image/jpeg');
$thumb = imagecreatefromjpeg($tempFile);
} elseif ($ext == "png") {
header('Content-type: image/png');
$thumb = imagecreatefrompng($tempFile);
}

$modifiedFile = imagecreatetruecolor($new_width, $new_height);

imagecopyresampled(
$modifiedFile,
$thumb,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height
);

if(imagejpeg($tempFile, $target_path))
{
$sql = "INSERT INTO images SET name = '$image_name', domaine = '$domaine'" ;
$return['success'] = $connexion->exec($sql);
echo json_encode($return);
}
header('Location: ../admin_sliders.php');
}

编辑答案-------------------------------------------- ------------------

@keziah 提供了好的方法,但他犯了一点错误,为了使代码正常工作,您需要添加一个 for 条件和一个 count($_FILES['image ']['name']); 和每个 $_FILES['image']['*****'] 末尾的 [$i]

像这样

    <?php
// Image config

require_once('../connexion.php');

$max_image_size = 500; //Maximum image size (height and width) (pixels)
$destination_folder = $_SERVER['DOCUMENT_ROOT'].'/AprimeWeb/img/sliders/'.$_POST['domaine'] .'/'; //upload directory ends with /
$jpeg_quality = 90; // jpeg quality (percent)
$domaine = isset($_POST['domaine']) ? $_POST['domaine'] : null;
$TotalImage = count($_FILES['image']['name']); //give the number of image you upload
extract($_POST);


for($i = 0;$i<$TotalImage;$i++){
if (isset($_FILES['image']['name'][$i])) {
// check $_FILES['image'] not empty
if (!isset($_FILES['image']['name'][$i]) || !is_uploaded_file($_FILES['image']['tmp_name'][$i])) {
die('Image file is Missing!');

}

// uploaded file info
$image_name = $_FILES['image']['name'][$i]; //file name
$image_size = $_FILES['image']['size'][$i]; //file size
$image_temp = $_FILES['image']['tmp_name'][$i]; //file temp
$image_size_info = getimagesize($image_temp); //get image size

if ($image_size_info) {
$image_width = $image_size_info[0]; //image width
$image_height = $image_size_info[1]; //image height
$image_type = $image_size_info['mime']; //image type
} else {
die("Make sure image file is valid!");
}

// creates new image from given file
// also, it validates the accepted image format
switch ($image_type) {
case 'image/png':
$image_res = imagecreatefrompng($image_temp);
break;
case 'image/gif':
$image_res = imagecreatefromgif($image_temp);
break;
case 'image/jpeg': case 'image/pjpeg':
$image_res = imagecreatefromjpeg($image_temp);
break;
default:
$image_res = false;
}

if ($image_res) {
var_dump($image_res);
//Get file extension and name to construct new file name
$image_info = pathinfo($image_name);
$image_extension = strtolower($image_info["extension"]); //image extension
$image_name_only = strtolower($image_info["filename"]); //file name only, no extension
//create a random name for new image (Eg: fileName_293749.jpg) ;
$new_file_name = $image_name_only . '_' . rand(0, 99999) . '.' . $image_extension;

//folder path to save resized images and thumbnails
$image_save_folder = $destination_folder . $new_file_name;

// proportionally resize image
if (normal_resize_image($image_res, $image_save_folder, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality)) {

/* Now, output image to user's browser or store information in the database */

$sql = "INSERT INTO images SET name = '$new_file_name', domaine = '$domaine'" ;
$return['success'] = $connexion->exec($sql);
echo json_encode($return);
}

imagedestroy($image_res); //freeup memory
}
else {
die('Invalid image format.');
}
}
}

最佳答案

normal_resize_image()

参数

  • Source - imagecreatefrom*($_FILES['image']['tmp_name']); - * 表示任何图片格式
  • 目标 - uploads/ - 你的上传目录
  • 图像类型 - getimagesize($_FILES['image']['tmp_name'])['mime'] - 图像 mime 类型
  • 最大尺寸 - 您的自定义图像尺寸为 200、400、500 像素
  • 图像宽度 - getimagesize($_FILES['image']['tmp_name'])[0] - 图像宽度
  • 图像高度 - getimagesize($_FILES['image']['tmp_name'])[1] - 图像高度
  • 质量 - JPEG 质量为 80、90、100 百分比

图像缩放器

<?php
function normal_resize_image($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality) {

if ($image_width <= 0 || $image_height <= 0) {
return false;
} //return false if nothing to resize
//do not resize if image is smaller than max size
if ($image_width <= $max_size && $image_height <= $max_size) {
if (save_image($source, $destination, $image_type, $quality)) {
return true;
}
}

//Construct a proportional size of new image
$image_scale = min($max_size / $image_width, $max_size / $image_height);
$new_width = ceil($image_scale * $image_width);
$new_height = ceil($image_scale * $image_height);

$new_canvas = imagecreatetruecolor($new_width, $new_height); //Create a new true color image
//Copy and resize part of an image with resampling
if (imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)) {
save_image($new_canvas, $destination, $image_type, $quality); //save resized image
}

return true;
}
?>

save_image()

参数

  • 来源——你的图片来源(同上)
  • 目的地 - 上传文件夹/目录(同上)
  • 图片类型-图片mime类型(同上)
  • 质量 - JPEG 质量(同上)

保存图片到资源文件

<?php
function save_image($source, $destination, $image_type, $quality) {
switch (strtolower($image_type)) {//determine mime type
case 'image/png':
imagepng($source, $destination);
return true; //save png file
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($source, $destination, $quality);
return true; //save jpeg file
break;
default: return false;
}
}

示例用法

<?php
// Image config
$max_image_size = 500; //Maximum image size (height and width) (pixels)
$destination_folder = 'uploads/'; //upload directory ends with /
$jpeg_quality = 90; // jpeg quality (percent)

if (isset($_FILES['image'])) {

// check $_FILES['image'] not empty
if (!isset($_FILES['image']) || !is_uploaded_file($_FILES['image']['tmp_name'])) {
die('Image file is Missing!');
}

// uploaded file info
$image_name = $_FILES['image']['name']; //file name
$image_size = $_FILES['image']['size']; //file size
$image_temp = $_FILES['image']['tmp_name']; //file temp

$image_size_info = getimagesize($image_temp); //get image size

if ($image_size_info) {
$image_width = $image_size_info[0]; //image width
$image_height = $image_size_info[1]; //image height
$image_type = $image_size_info['mime']; //image type
} else {
die("Make sure image file is valid!");
}

// creates new image from given file
// also, it validates the accepted image format
switch ($image_type) {
case 'image/png':
$image_res = imagecreatefrompng($image_temp);
break;
case 'image/gif':
$image_res = imagecreatefromgif($image_temp);
break;
case 'image/jpeg': case 'image/pjpeg':
$image_res = imagecreatefromjpeg($image_temp);
break;
default:
$image_res = false;
}

if ($image_res) {
//Get file extension and name to construct new file name
$image_info = pathinfo($image_name);
$image_extension = strtolower($image_info["extension"]); //image extension
$image_name_only = strtolower($image_info["filename"]); //file name only, no extension
//create a random name for new image (Eg: fileName_293749.jpg) ;
$new_file_name = $image_name_only . '_' . rand(0, 99999) . '.' . $image_extension;

//folder path to save resized images and thumbnails
$image_save_folder = $destination_folder . $new_file_name;

// proportionally resize image
if (normal_resize_image($image_res, $image_save_folder, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality)) {

/* Now, output image to user's browser or store information in the database */
echo '<div align="center">';
echo '<img src="uploads/' . $new_file_name . '" alt="Resized Image">';
echo '</div>';

// COde to store the image into database
}

imagedestroy($image_res); //freeup memory
}
else {
die('Invalid image format.');
}
}

关于php - 在使用临时文件夹上传之前调整多个图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37527358/

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