【问题标题】:Create thumb after upload/ PHP Codeigneter上传后创建拇指/ PHP Codeigniter
【发布时间】:2018-03-16 17:18:24
【问题描述】:

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

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

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');
        }
    }
}

这是我想要实现调整大小的功能:

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);
        }
    }
}

【问题讨论】:

  • 没有意思写这种长代码,具体信息请尝试询问。
  • -1 表示没有研究。它就在文档中。 image manipulation
  • 感谢我看到了我尝试实现它的文档,但我没有成功。这就是我寻求帮助的原因$
  • 如果 CI 图像库太难使用,也许你应该使用 imagemagick:imagemagick.org/script/index.php 它在大多数主机上都可用或很容易添加。并不比单行命令更容易。

标签: php codeigniter


【解决方案1】:

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

你需要这样的东西:

$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();

【讨论】:

    【解决方案2】:

    生成您需要使用的图片缩略图Image Processing (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;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2014-12-23
      • 2011-02-06
      • 2012-12-14
      • 2012-07-09
      • 2012-03-23
      • 1970-01-01
      相关资源
      最近更新 更多