如何在PHP Codeigniter中下载上传的图像

问题描述:

我已上传图像,然后对图像进行旋转操作,并以其他视图获取翻转后的图像,现在如何下载翻转后的图像,请指导我?

I have uploaded an image and then applied rotation operation on the image and fetch the flipped image in other view, now how to download that flipped image please guide me?

查看用于上传图片的代码:

View code for uploading image:

<input type='file' name="userfile" size="20">
<input type="submit" name="upload" value="Flip">

用于翻转的控制器代码:

Controller code for flip:

$data['title'] ='Flip Image';
$upload['upload_path'] = './assets/images/';
$upload['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload',$upload);
$filedata1 = $this->upload->data();
if(!$this->upload->do_upload('userfile')){
show_error($this->upload->display_errors());
}           
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->data('full_path');
$config['rotation_angle'] ='hor';
$this->image_lib->initialize($config);
if(!$this->image_lib->rotate()){
    show_error($this->image_lib->display_errors());
}
$filedata = $this->upload->data();
$data['img2'] = base_url().'/assets/images/'.$filedata['file_name'];
print $this->load->view('pages/result',$data,true);

下载功能:

$this->load->helper('download');
$data = 'myimag.png';
force_download($img2, $data);
redirect('pages/result');

现在我要在其他页面上获取此图像结果,在这里我单击了下载按钮,就调用了控制器的下载功能(如上所示),以便它应开始下载文件而无需询问路径,但它显示错误: 结果视图:

Now I am fetching this image result in other page and here I called the download function of controller(given above) on click the download button so that it should start downloading the file without asking the path for it but it is showing error: Result View:

<img src="<?php echo $img2;?>" width="200" height="200"> 
<?php echo form_open('pages/download()'); ?>
<input type="submit" name="submit" value="Download">
</form>

错误:函数Pages :: download()的参数太少,传入了0 532行上的D:\ xampp \ htdocs \ ImageTools \ system \ core \ CodeIgniter.php和 正好是1个

Error: Too few arguments to function Pages::download(), 0 passed in D:\xampp\htdocs\ImageTools\system\core\CodeIgniter.php on line 532 and exactly 1 expected

希望这对您有帮助:

您可以这样做:

注意:确保此处$img2包含图像文件的完整路径

Note : make sure here $img2 contains full path of image file

<img src="<?php echo $img2;?>" width="200" height="200"> 
<a href="<?php echo $img2;?>" download="myimage" >Download</a>

或者只需执行以下操作即可:

<img src="<?php echo $img2;?>" width="200" height="200"> 
<a href="<?=base_url('pages/download/'.$img2);?>" >Download</a>

您的download方法应如下所示:

function download($img2)
{
    $this->load->helper('download');
    /*make sure here $img2 contains full path of image file*/

    force_download($img2, NULL);
}

更多信息: https://www.w3schools.com/tags/att_a_download.asp