0

我需要您的帮助才能在我的网站中混合使用两种功能。我的目的是上传和调整图像大小。我有一个功能可以很好地处理用户图片,我还有另一个用于上传图片(列表或广告图片),但此功能不会创建缩略图。它只是上传它。我喜欢做的是像功能专用用户图片一样上传和调整大小。

希望您能够帮助我。这是有关用户图片的功能(拇指调整大小工作)

public function photo($id = "") {
    $target_path = realpath(APPPATH . '../images/users');
    //echo $target_path;

    if (!is_writable(dirname($target_path))) {
        $this->session->set_flashdata('flash_message', $this->Common_model->flash_message('error', 'Sorry! Destination folder is not writable.'));
        redirect('users/edit', 'refresh');
    } else {
        if (!is_dir(realpath(APPPATH . '../images/users') . '/' . $id)) {
            //echo $this->path.'/'.$id;
            mkdir(realpath(APPPATH . '../images/users') . '/' . $id, 0777, true);
        }

        $target_path = $target_path . '/' . $id . '/userpic.jpg';

        if ($_FILES['upload123']['name'] != '') {
            move_uploaded_file($_FILES['upload123']['tmp_name'], $target_path);

            $thumb1 = realpath(APPPATH . '../images/users') . '/' . $id . '/userpic_thumb.jpg';
            GenerateThumbFile($target_path, $thumb1, 107, 78);

            $thumb2 = realpath(APPPATH . '../images/users') . '/' . $id . '/userpic_profile.jpg';
            GenerateThumbFile($target_path, $thumb2, 209, 209);

            $thumb3 = realpath(APPPATH . '../images/users') . '/' . $id . '/userpic_micro.jpg';
            GenerateThumbFile($target_path, $thumb3, 36, 36);

            $this->session->set_flashdata('flash_message', $this->Common_model->flash_message('success', 'Your profile photo updated successfully.'));
            redirect('users/edit', 'refresh');
        } else {
            $this->session->set_flashdata('flash_message', $this->Common_model->flash_message('error', 'Please browse your profile photo.'));
            redirect('users/edit', 'refresh');
        }
    }
}

这是实现 resize 的函数 id :

if ($this->input->post()) {
    $listId = $param;
    $images = $this->input->post('image');
    $is_main = $this->input->post('is_main');

    $fimages = $this->Gallery->get_imagesG($listId);
    if ($is_main != '') {
        foreach ($fimages->result() as $row) {
            if ($row->id == $is_main)
                $this->Common_model->updateTableData('list_photo', $row->id, NULL, array("is_featured" => 1));
            else
                $this->Common_model->updateTableData('list_photo', $row->id, NULL, array("is_featured" => 0));
        }
    }

    if (!empty($images)) {
        foreach ($images as $key => $value) {
            $image_name = $this->Gallery->get_imagesG(NULL, array('id' => $value))->row()->name;
            unlink($this->path . '/' . $listId . '/' . $image_name);

            $conditions = array("id" => $value);
            $this->Common_model->deleteTableData('list_photo', $conditions);
        }
    }

    if (isset($_FILES["userfile"]["name"])) {
        $insertData['list_id'] = $listId;

        if (!is_dir($this->path . '/' . $listId)) {
            //echo $this->path.'/'.$id;
            mkdir($this->path . '/' . $listId, 0777, true);
            $insertData['is_featured'] = 1;
        }

        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->path . '/' . $listId,
            'encrypt_name' => TRUE,
            'remove_spaces' => TRUE
        );

        //echo $this->path.'/'.$id;
        $this->load->library('upload', $config);
        $data = $this->upload->do_upload();
        if ($data) {
            $this->outputData['file'] = $this->upload->data();
            $insertData['name'] = $this->outputData['file']['file_name'];
            $insertData['created'] = local_to_gmt();

            if ($this->outputData['file']['file_name'] != '')
                $this->Common_model->insertData('list_photo', $insertData);
        }
    }
}
4

2 回答 2

0

下部代码中没有任何内容可以生成拇指文件。

你需要这样的东西:

$this->thumb_path   = realpath(APPPATH . '../directory/in/codeigniter/site/image/thumb');

然后在 $this->upload->data() 中完成图像上传后,您将需要生成拇指(调整您的设置)

$thumb_config = array(
    'source_image' => $data['full_path'],
    'new_image' => $this->thumb_path,
    'maintain_ratio' => true,
    'width' => 200,
    'height' => 200,
    'quality' => '100%'
  );

  $this->load->library('image_lib', $thumb_config);
  $this->image_lib->resize();
于 2012-11-12T20:50:03.410 回答
0

生成您需要使用图像处理 (ImageMagick)的图像拇指

我提供以下代码来生成图像缩略图。

function GenerateThumbnails($sourcePath1 = NULL, $destinationPath1 = NULL, $fileNames = NULL, $width = NULL, $height = NULL) {
try {
    $sourcePath = $sourcePath1;
    $destinationPath = $sourcePath . $destinationPath1 . "/";
    if (isset($_POST['sourcePath'])) {
        //$sourcePath = $_SERVER["DOCUMENT_ROOT"] . $_POST['sourcePath'];
        $sourcePath = $_POST['sourcePath'];
    }
    if (isset($_POST['destinationPath'])) {
        //$destinationPath = $_SERVER["DOCUMENT_ROOT"] . $_POST['destinationPath'];
        $destinationPath = $_POST['destinationPath'];
    }
    if (isset($_POST['fileNames'])) {
        $fileNames = json_decode($_POST['fileNames']);
    }
    if (isset($_POST['width'])) {
        $width = $_POST['width'];
    }
    if (isset($_POST['height'])) {
        $height = $_POST['height'];
    }
    foreach ($fileNames as $fileName) {
        if (is_dir($sourcePath)) {
            if (file_exists($sourcePath . $fileName)) {
                $fetchFileExtension = array_values(array_filter(explode(".", $fileName)));
                $fileExtension = end($fetchFileExtension);
                $sourcePathFileName = $sourcePath . $fileName;
                $destinationPathFileName = $destinationPath . $fileName;
                $image = new Imagick();
                $image->readImage($sourcePathFileName);
                $image->scaleImage(1000, 0);
                $image->setImageColorspace(255);
                $image->setImageFormat('jpg');
                $image = $image->flattenImages();
                $image->thumbnailImage($width, $height, true);
                (is_dir($destinationPath)) ? $image->writeImage($destinationPathFileName) : (createDirectory($destinationPath) AND $image->writeImage($destinationPathFileName));
                chmod($destinationPathFileName, 0777);
                $image->clear();
                $image->destroy();
                list($width1, $height1) = getimagesize($destinationPathFileName);
                $data['orientation'] = ($width1 > $height1) ? 1 : 2;
                $data['thumbnailChecksum'] = md5_file($destinationPathFileName);
                $data['message'] = "Thumbnail created successfully .";
            } else {
                $data['message'] = "Thumbnail creation failed.";
            }
        }
    }
    return $data;
} catch (Exception $ex) {
    print $ex->getMessage();
    return false;
}

}

于 2018-03-16T09:18:24.290 回答