简体   繁体   English

上传具有gif格式php的图片

[英]upload picture which has gif format php

I tried upload picture which have jpg format then success but when I try upload gif format the result give black blank picture. 我尝试上传具有jpg格式然后成功的图片,但是当我尝试上传gif格式时,结果会给出黑色空白图片。 I think I have to add script for gif format in my code but I dunno what should I add. 我想我必须在我的代码中添加gif格式的脚本,但我不知道应该添加什么。 here code in blog.php 这里的代码在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>

and here action.php (action after upload) 这里是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>";

and here the upload.php (function) 这里是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);
}

help me please to solve the problem. 请帮我解决问题。 thanks 谢谢

The clue is imagecreatefromjpeg which creates an image from a jpeg. 线索是imagecreatefromjpeg ,它从jpeg创建一个图像。

You should use [getImageSize()][1] to find the format, and use the [imagecreatefromgif][2] if needed. 您应该使用[getImageSize()][1]来查找格式,并在需要时使用[imagecreatefromgif][2] Or use [imagecreatefromstring][3] which can take either. 或者使用[imagecreatefromstring][3] ,它可以采取任何一种方法。

This also has the advantage that if "getimagesize" fails, you know it's not a valid image. 这也有一个好处,如果“getimagesize”失败,你知道它不是一个有效的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM