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

时间:2017-01-20 04:47:34

标签: php html mysql ajax

我有上传宽度后自动裁剪图片的功能: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);
}
请帮助我解决问题。感谢

1 个答案:

答案 0 :(得分:1)

试试这个:

$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