簡體   English   中英

如何使用Codeigniter / PHP在運行時動態調整圖像大小?

[英]How to resize a image dynamically on the run using Codeigniter/PHP?

這是我分享的Foursquare圖片網址:-

https://igx.4sqi.net/img/general/width960/59805036_Qt3WZYJ5z3_LPh8tpG4wtqJSrVokkKioxYebWogfOjo.jpg

它顯示原始圖像。 現在,如果我使用以下圖像鏈接,

https://igx.4sqi.net/img/general/200x1600/59805036_Qt3WZYJ5z3_LPh8tpG4wtqJSrVokkKioxYebWogfOjo.jpg

圖像會根據尺寸200x1600自動調整大小。 給定任何尺寸,圖像將自動調整大小,這證明沒有預定義尺寸。

當上述鏈接放在url中時,圖像會自動調整大小。 如何在使用Codeigniter或PHP的情況下動態管理此問題?

我對此一無所知。

使用CI非常簡單。 閱讀圖像處理類的文檔。

您將需要創建一個控制器來處理:

<?php

class Image extends CI_Controller
{

    public function resize($filename, $width, $height){

        // path to image in your project
        $image_path = 'uploads/img/'.$filename;

        // if image doesn't exist show 404 error
        if(!file_exists($image_path))
            show_404();

        // Load image library
        $this->load->library('image_lib');


        // configure image library
        $config = array(
            'image_library' => 'gd2',
            'source_image' => $image_path,
            'dynamic_output' => true,
            'width' => $width,
            'height' => $height,
        );

        // load configuration
        $this->image_lib->initialize($config);

        // resize
        $this->image_lib->resize();

    }

}

現在打開瀏覽器並嘗試:localhost / project_folder / image / resize / example.jpg / 500/500

要使URL看起來像問題中的URL,可以將route添加到route配置文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM