2012-06-13 47 views
2

我正在測試一個縮略圖創建器腳本,該腳本創建了一個可以在網頁瀏覽器中顯示而不是先保存的縮略圖。顯示新創建的縮略圖而不先保存?

目前我必須使用下面的代碼來顯示圖像:

include('image.inc.php'); 

    header('Content-Type: image/png'); 
    create_thumbnail('me.jpg', false, 200, 200); 

但我怎麼能沿着側HTML標記一個標準的網頁上顯示動態生成的縮略圖?

create_thumbnail功能:

function create_thumbnail($path, $save, $width, $height){ 

    // Get the width[0] and height[1] of image in an array 
    $info = getimagesize($path); 

    // Create a new array that just contains the width and height 
    $size = array($info[0], $info[1]); 

    // Check what file type the image is 
    if($info['mime'] == 'image/png'){ 

     $src = imagecreatefrompng($path); 

    }else if($info['mime'] == 'image/jpeg'){ 

     $src = imagecreatefromjpeg($path); 

    }else if($info['mime'] == 'image/gif'){ 

     $src = imagecreatefromgif($path); 

    }else{ 
     // If it isn't a good file type don't do anything 
     return false; 
    } 

    // Create a thumbnail with the passed dimensions 
    $thumb = imagecreatetruecolor($width, $height); 


    $src_aspect = $size[0]/$size[1]; 

    $thumb_aspect = $width/$height; 

    if($src_aspect < $thumb_aspect){ 

     // Image is tall 
     $scale = $width/$size[0]; 
     $new_size = array($width, $width/$src_aspect); 
     $src_pos = array(0, ($size[1] * $scale - $height)/$scale/2); 
    }else if($src_aspect > $thumb_aspect){ 

     // Image is wide 
     $scale = $height/$size[1]; 
     $new_size = array($height * $src_aspect, $height); 
     $src_pos = array(($size[0] * $scale - $width)/$scale /2, 0); 

    }else{ 

     // Image is square 
     $new_size = array($width, $height); 
     $src_pos = array(0, 0); 
    } 

    // Stop the new dimensions being less than 1 (this stops it breaking the code). Takes which ever value is higher. 
    $new_size[0] = max($new_size[0], 1); 
    $new_size[1] = max($new_size[1], 1); 


    // Copy the image into the new thumbnail 
    // Newly created thumbnail, image copying from, starting x-coord of thumbnail, starting y-coord of thumbnail(0,0 will fill up entire thumbnail), x-coord of original image, y-coord of original image, width of new thumbnail, height of new thumbnail, width of original image, height of original image 
    imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $size[0], $size[1]); 

    if($save === false){ 
     return imagepng($thumb); 
    }else{ 

    // Create the png image and save it to passed location 
    return imagepng($thumb, $save); 
    } 

回答

0

將其轉換爲base64(使用base64_encode),然後用它顯示:

<img src="data:image/jpg;base64,iVBORw0KGgoAAAANS==" /> 
+0

我試圖這樣的:'$圖像= create_thumbnail( 'me.jpg',假,200,200); \t \t echo「」;'沒有工作。任何建議爲什麼? – crm

+0

@crm'imagepng'返回一個布爾值,(可選)輸出原始圖像流。如果你想捕獲該流,你必須使用輸出緩衝:http://php.net/manual/en/function.ob-get-contents.php – jeroen

+0

@crm你沒有將其轉換爲base64。 –

1

如果我正確理解你的問題,你只需要插入img標籤並將其src屬性設置爲您的php腳本。

是這樣的:

<img src="thumbnailgenerator.php?image_path=me.jpg"> 
+0

如果我想在頁面上顯示多個縮略圖,在這種情況下如何將變量傳遞給縮略圖生成器腳本? – crm

+0

@crm我不知道你現在怎麼做,但一個簡單的解決方案是使用查詢字符串:'thumbnailgenerator.php?image_id = 5'並獲得你的php腳本中的變量:'$ _GET [' image_id']'。 – jeroen

+0

感謝您的替代建議,但我試圖讓它與當前腳本一起工作。 – crm