从服务器获取图像

时间:2014-05-02 11:41:30

标签: php image-upload

我使用php将图像上传到服务器。我还在id后生成一个随机图像编号。现在上传图像的名称如6_1.jpg,5_4.jpg等,其中第一个数字是id(在_之前) )和其他是随机生成的数字(在_之后)。我的代码如下

function upload_receipt_img(){
    $random_digit = rand(0,9);          
    $receipt_id = $_POST['id'];      
        if (isset($_POST['data_url'])) {      
            $file = $receipt_id._.$random_digit.'.jpg';
            $img = substr($_POST['data_url'], strpos($_POST['data_url'], ",") + 1);
            $decodedData = base64_decode($img);
            $success = file_put_contents('assets/receipt_img/' . $file, $decodedData);
            //resize main image
            $source_path = "assets/receipt_img/" . $file;
            $destination = "assets/receipt_img/" . $file;
            $newwidth = 350;
            $this->resizeImg($newwidth, $source_path, $destination);
            $data['img'] = 'assets/receipt_img/' . $receipt_id.$random_digit.'.jpg';
        }
    $this->closeConnection();
    echo $receipt_id;

}

现在我怎样才能将图像从服务器显示到我的主页。请帮助我。提前谢谢

1 个答案:

答案 0 :(得分:0)

使用标题功能

<?php 
  header('Content-type: image/jpeg'); 
  readfile($destination); // image file path
?>

每张图片。

相关问题