2011-08-20 117 views
0

我有一個關於圖像顯示使用函數getImage_w($ image,$ dst_w),它需要圖像的URL($ image)和這個圖像的目的地寬度($大小)..然後它重新繪製圖像改變其高度根據目的地的寬度。圖像顯示使用PHP而不影響HTML代碼

是這樣的函數(在庫/ image.php文件):

function getImage_w($image,$w){ 
     /*** get The extension of the image****/ 
     $ext= strrchr($image, "."); 
     /***check if the extesion is a jpeg***/ 
     if (strtolower($ext)=='.jpg' || strtolower($ext)=='.jpeg'){ 
     /***send the headers****/ 
     header('Content-type: image/jpeg'); 
     /**get the source image***/ 
     $src_im_jpg=imagecreatefromjpeg($image); 
     /****get the source image size***/ 
     $size=getimagesize($image); 
     $src_w=$size[0]; 
     $src_h=$size[1]; 
     /****calculate the distination height based on the destination width****/ 
     $dst_w=$w; 
     $dst_h=round(($dst_w/$src_w)*$src_h); 
     $dst_im=imagecreatetruecolor($dst_w,$dst_h); 
     /**** create a jpeg image with the new dimensions***/ 
     imagecopyresampled($dst_im,$src_im_jpg,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h); 
     imagejpeg($dst_im); 
} 

在文件imagetest.php我有這樣的代碼部分:

<?php 
require 'libs/image.php'; 
echo '<h1>HELLO WORLD : some html</h1> 
<img src="'.displayImg_w('http://www.sanctius.net/wp-content/uploads/2010/05/Avatar-20.jpg',200).'"> 
'; 

在過去,我以前用定義圖像的$ _GET參數編寫URL ...但現在,我想直接在我的代碼中使用該函數。

的問題是圖像被正確顯示,但

的Hello World

HTML代碼沒有被瀏覽器(我知道的標題已經由第一代碼發送翻譯......但我想知道如何。正確顯示圖像,而不會影響HTML代碼,也沒有使用該圖片的URL改變這種不希望的形式獲取參數:

庫/ image.php圖片= HTTP://www.example .com/image & width = 200

謝謝你提前。

+2

FoRMat YoUr COdE ReAlLy讓它可以在眼睛上享受! – Shef

+0

我編輯它SHEF :-) – SmootQ

回答

1

這基本上是不可能的。網頁瀏覽器請求HTML頁面並期待HTML。或者它請求一張圖片並期待一張圖片。你不能在一個請求中混合使用這兩個請求,僅僅因爲只有一個Content-Type可以有效。

但是,您可以使用data URI嵌入HTML圖像:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot"> 

請注意,base64編碼是相當無效的,所以一定要definitly壓縮你的輸出,如果瀏覽器支持它,使用示例gzip

所以對你來說可能看起來如下:

echo '<img src="data:image/jpeg;base64,' . base64_encode(displayImg_w('http://www.sanctius.net/wp-content/uploads/2010/05/Avatar-20.jpg',200)) . '">'; 

並確保displayimg_w()不輸出頭了。

此外,displayimg_w()需要在年底進行調整,以將圖像數據作爲字符串返回,而不是直接輸出:

ob_start(); 
imagejpeg($dst_im); 
return ob_get_flush(); 
+0

謝謝你的朋友,但你看這個......(iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg ==) 是圖像的URL? – SmootQ

+1

該代碼是圖像的base64編碼數據。該字符串代表一個小紅點(閱讀鏈接,它解釋了很多!)。儘管這種方法受到警告,但它不支持某些瀏覽器,包括IE7和以前的瀏覽器。 – Codemwnci

+1

不,它不是URI,它是實際的編碼數據 - 它是圖像本身。 – Shi

3

我以前的,完全錯誤的答案後,我希望能彌補其與此。試試這個代碼:

<?php 

    function getImage_w($image,$w){ 
    // Get the extension of the file 
    $file = explode('.',basename($image)); 
    $ext = array_pop($file); 
    // These operations are the same regardless of file-type 
    $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); 
    // These operations are file-type specific 
    switch (strtolower($ext)) { 
     case 'jpg': case 'jpeg': 
     $ctype = 'image/jpeg';; 
     $src_im = imagecreatefromjpeg($image); 
     $outfunc = 'imagejpeg'; 
     break; 
     case 'png': 
     $ctype = 'image/png';; 
     $src_im = imagecreatefrompng($image); 
     $outfunc = 'imagepng'; 
     break; 
     case 'gif': 
     $ctype = 'image/gif';; 
     $src_im = imagecreatefromgif($image); 
     $outfunc = 'imagegif'; 
     break; 
    } 
    // Do the resample 
    imagecopyresampled($dst_im,$src_im,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h); 
    // Get the image data into a base64_encoded string 
    ob_start(); 
    $outfunc($dst_im); 
    $imgdata = base64_encode(ob_get_contents()); // Don't use ob_get_clean() in case we're ever running on some ancient PHP build 
    ob_end_clean(); 
    // Return the data so it can be used inline in HTML 
    return "data:$ctype;base64,$imgdata"; 
    } 

    echo '<h1>HELLO WORLD : some html</h1> 
    <img src="'.getImage_w('http://www.sanctius.net/wp-content/uploads/2010/05/Avatar-20.jpg',200).'" /> 
    '; 

?> 
+0

該操作知道但要求不使用它的解決方案。 – Shi

+0

謝謝你的朋友,我會試試這個+1 – SmootQ

+1

[facepalm] @Shi對不起,我還在睡夢中。你是絕對正確的,你的解決方案就是答案...... – DaveRandom