简体   繁体   English

图片未在 Codeigniter PHP 中调整大小和上传

[英]Image not resize and upload in Codeigniter PHP

I am trying to create thumbnail(resize) of uploaded image using codeigniter, But image is not uploaded in server/folder,Here is my code, Where I went wrong ?我正在尝试使用 codeigniter 创建上传图像的缩略图(调整大小),但图像未上传到服务器/文件夹中,这是我的代码,我哪里出错了?

if (isset($_FILES['image'])) {
    if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {

        $filename         = time() . uniqid(rand()) . $_FILES['image']['name'];
        move_uploaded_file($_FILES["image"]["tmp_name"], "vendorProfile/" . $filename);
        $saveArr['image'] = $filename;
        $this->load->library('image_lib');

        $source_paths = base_url() . 'vendorProfile/' . $filename;
        $source_path  = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/' . $filename;
        $target_path  = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/thumb';

        $config_manip = array(
            'image_library'  => 'gd2',
            'source_image'   => $source_path,
            'new_image'      => $target_path,
            'maintain_ratio' => TRUE,
            'create_thumb'   => TRUE,
            'thumb_marker'   => '_thumb',
            'width'          => 150,
            'height'         => 150
        );

        $this->load->library('image_lib', $config_manip);
        if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
        } else {
            echo "image is uploaded";
        }
        $this->image_lib->clear();
    } 
}       

You just look at the file path which you pass on $config_manip array where you need to pass file path without Http or https PHP don't accept Http.您只需查看您在 $config_manip 数组上传递的文件路径,您需要在没有 Http 或 https 的情况下传递文件路径 PHP 不接受 Http。 So if in your case your file struct like something this所以如果在你的情况下你的文件结构像这样

-
application
system
image_folder <----

Then you just pass like something this然后你就像这样通过

$config_manip = array(
        'image_library'  => 'gd2',
        'source_image'   => './image_folder/'.$file_name,
        'new_image'      => './thumb_folder/'.$target_path,
        'maintain_ratio' => TRUE,
        'create_thumb'   => TRUE,
        'thumb_marker'   => '_thumb',
        'width'          => 150,
        'height'         => 150
    );

If you are using the ubuntu server you need to give permission for that folder to upload the file/image.如果您使用的是 ubuntu 服务器,则需要授予该文件夹上传文件/图像的权限。

if (!file_exists($target_path)) {
    if (!mkdir($target_path, 0777, true)) {
        chmod($target_path, 0777);
   }
}

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

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