gpt4 book ai didi

php - 根据图片php的高度调整图片大小

转载 作者:行者123 更新时间:2023-11-29 11:03:53 25 4
gpt4 key购买 nike

我有上传后自动裁剪图片的功能,宽度:800px,高度:534px。但现在我想更改宽度:800px和高度:(基于原始文件图片上传),我不知道我必须在代码中更改什么。

这是我在 blog.php

中的表单代码
<form method=POST action=$aksi?module=blog&act=input class='form-horizontal' enctype='multipart/form-data'>
<div class='form-group'>
<label for='produkImageUpload'>Image *:</label>
<div class='input-group'>
<input type='file' multiple='' name='fupload'>
</div>
<button type='submit' class='btn btn-success'><i class='fa fa-floppy-o'></i>&nbsp;Save</button>
</div>
</form>

这里action.php(上传后的操作)

include "../../config/upload.php";

$ukuran_maksimal = 3000000;
$acak = rand(0000,9999);
$lebar = 800;
$tinggi = 534;

$id_kategori = $_POST['id_kategori_blog'];
$folder = "images/blog/$id_kategori/";
$lokasi_file = $_FILES['fupload']['tmp_name'];
$tipe_file = $_FILES['fupload']['type'];
$nama_file = $_FILES['fupload']['name'];
$ukuran_file = $_FILES['fupload']['size'];
$nama_file = str_replace(' ', '_', $nama_file);
$nama_file = $acak.$nama_file;

upload_blog($nama_file,$folder,$lokasi_file,$lebar,$tinggi);
mysql_query("INSERT INTO blog(id_kategori_blog, gambar) VALUES( '$_POST[id_kategori_blog]', '$nama_file' )");
echo "<script>window.alert('Upload image succes');
window.location=('../../main.php?module=blog')</script>";

这里是upload.php(函数)

function upload_blog($nama_file,$folder,$lokasi_file,$lebar,$tinggi)
{
$nama_folder = "../../../$folder/";
list($lebar_asli, $tinggi_asli, $source_type) = getimagesize($lokasi_file);
$gambar_asli = imagecreatefromjpeg($lokasi_file);
$ukuran_asli = $lebar_asli / $tinggi_asli;
$ukuran = $lebar / $tinggi;
if ($ukuran_asli > $ukuran) {
$tinggi_sementara = $tinggi;
$lebar_sementara = ( int ) ($tinggi * $ukuran_asli);
} else {
/*jika gambar sama atau lebih tinggi*/
$lebar_sementara = $lebar;
$tinggi_sementara = ( int ) ($lebar / $ukuran_asli);
}
/*rubah ukuran gambar ke ukuran sementara*/
$gambar_sementara = imagecreatetruecolor($lebar_sementara, $tinggi_sementara);
imagecopyresampled($gambar_sementara,$gambar_asli,0, 0,0, 0,$lebar_sementara, $tinggi_sementara,$lebar_asli, $tinggi_asli);
/*Copy cropped region from temporary image into the desired GD image*/
$x_absis = ($lebar_sementara - $lebar) / 2;
$y_absis = ($tinggi_sementara - $tinggi) / 2;
$gambar_akhir = imagecreatetruecolor($lebar, $tinggi);
imagecopy($gambar_akhir,$gambar_sementara,0, 0,$x_absis, $y_absis,$lebar, $tinggi);
imagejpeg($gambar_akhir,$nama_folder.$nama_file);
imagedestroy($gambar_akhir);
}

请帮我解决这个问题。谢谢

最佳答案

试试这个:

$file = $_FILES["files"]['tmp_name'];
list($width, $height) = getimagesize($file);

$width 包含文件的宽度,$height 包含文件的高度。在代码中使用它们作为调整大小的因素。

或者

$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0]; // width of image
$image_height = $image_info[1]; // height of image

关于php - 根据图片php的高度调整图片大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41756292/

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