簡體   English   中英

圖片網址重寫

[英]Images URLs Rewriting

我已經寫了一個腳本image.php,它很長,但是我只復制了下面部分代碼。

注意:

$image = $_GET['ph'];
$w = $_GET['w'];

以便:

function getImage_w($image, $w){
    $ext = strrchr($image, ".");
    if (strtolower($ext) == '.jpg' || strtolower($ext) == '.jpeg'){
        header('Content-type: image/jpeg');

        $src_im_jpg = imagecreatefromjpeg($image);
        $size = getimagesize($image);

        $src_w = $size[0];
        $src_h = $size[1];
        $dst_w = $w;
        $dst_h = round(($dst_w/$src_w)*$src_h);
        $dst_im = imagecreatetruecolor($dst_w,$dst_h);

        imagecopyresampled($dst_im,$src_im_jpg,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);

        imagejpeg($dst_im);
    }
}

該腳本采用兩個GET變量$_GET['ph']$_GET['w']ph定義圖像的URL, w定義目標調整大小寬度。

例如:如果我的腳本在libs/目錄中,而照片在resources目錄中,並且我想顯示width = 100照片,則必須使用以下html:

<img src="libs/image.php?ph=../resources/myimage.jpg&w=100">

我的問題是,我的圖像正確顯示在瀏覽器中,但是URL格式錯誤。 我想在images屬性中放置images alt屬性而不是此url。

並自動將image.php應用於圖像請求。

另外,當用戶要將圖像保存在桌面上時,我遇到了問題,該圖像以image.php名稱image.php ,這是不希望的。

先感謝您。

RewriteEngine on
#change 'resize' to the folder your image.php is in
RewriteBase /resize

#e.g. /resize/path/to/image/sample-200.jpg
# will become /resize/image.php?ph=path/to/image/sample.jpg&w=200
RewriteRule ^(.*)-([0-9]+)(\.jpg)$ image.php?ph=$1$3&w=$2 [L,NC]

請注意,您不能在網址中使用..,而應使用相對於根的網址。 為此,您需要在您的php文件中的路徑中添加document_root:

$image = $_SERVER['DOCUMENT_ROOT'] . "/" . str_replace('..','', $_GET['ph']);

保存圖像的用戶將獲得正常的圖像名稱,但帶有'-'並在末尾添加了尺寸。

不確定您要使用alt。 另外我也不認為重寫所有圖像是明智的,因此此腳本僅以/ resize /開始重寫圖像。

www.example.com/hosting.jpg是不可能的,因為php不知道在哪個文件夾中查找該圖像,以及如何調整該圖像的大小。

暫無
暫無

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

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