2013-07-02 15 views
0

我建立一個類imgResize的:圖顯示在二進制代碼與正確的標題上的瀏覽器

class imgResize 
{ 
    public static function resizeAndOutput($img) { 

    $orginal_img = $img; 
    $width = 591; 
    $height = 332; 
    $new_width = 161; 
    $new_height = 91; 

    $edit_img = imagecreatetruecolor($new_width, $new_height); 
    $image = imagecreatefromjpeg($orginal_img); 
    imagecopyresampled($edit_img, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
    header('Content-Type: image/jpeg'); 

    imagejpeg($edit_img); 
} 

}

和HTML我試圖讓本上顯示的飛行img

<img class="img_small" src="<?php imgResize::resizeAndOutput($img)"> 

剛剛得到這樣的二進制數據:

�����JFIF���������>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality ���C�   $.' 

我該如何解決我的問題?

+0

你從哪裏返回函數的圖像源?我看不到任何回報聲明? –

+0

我不需要返回聲明,因爲我創建一個imagejpeg臨時 – Wykk

+0

好吧,如果你不想保存它,那麼你不需要'img'元素..imagejpeg將在頁面中顯示圖像...這就像您直接用它的url打開圖像..因爲在php中它是這樣使用的:[imagejpeg](http://php.net/manual/en/ function.imagejpeg.php) –

回答

1

你要編碼的數據URI圖像(和嵌入編碼的數據在你的HTML字符串中的Base64),並刪除在PHP是頭()線,

或創建一個鏈接到該圖像,例如http://example.com/?myimage=id並輸出圖像(如同給出正確的標題並回顯所得到的圖像)

告訴我您是否需要更多信息,或者提供足夠的見解。

+0

//頭('內容($ edit_img); return base64_encode($ edit_img); and addet in src =「data:image/jpeg; base64,「 – Wykk

+0

你能寫一個例子嗎?我該怎麼做才能讓base64工作? – Wykk

+0

你在評論中寫的東西應該是正確的,試着將結果字符串粘貼到瀏覽器中(比如data:image/jpeg ; base64,asdkljasdklasd ==)並查看你得到的圖像 – DRC

0

如果將它直接輸出到src元素中,它當然只是HTML中的二進制垃圾。這不是<img>元素的工作方式。 <img>元素的src需要指向圖像的URL。這可以是一個URL,如img/foo.jpgapp/some_script_that_outputs_an_image.phpData URI。但不是簡單的二進制數據。輸出圖像的腳本將在第二種情況下起作用,作爲app/some_script_that_outputs_an_image.php鏈接的目標。

0

你實際上做得對,但在最後一部分你有問題。你必須保存圖像,然後返回到圖像的urlimg元素..試試這個代碼:

class imgResize 
{ 
    public static function resizeAndOutput($img) { 

    $orginal_img = $img; 
    $width = 591; 
    $height = 332; 
    $new_width = 161; 
    $new_height = 91; 

    $edit_img = imagecreatetruecolor($new_width, $new_height); 
    $image = imagecreatefromjpeg($orginal_img); 
    imagecopyresampled($edit_img, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
    header('Content-Type: image/jpeg'); 

    //Now we have the image lets create a random filename 
    //Modify this to your needs 
    $filename = "images/IMG_" . mt_rand(1000,9999) . ".jpg"; 
    //Random sometimes sucks.. Lets see if we already got a file with that name 
    //And if exists, loop until we get a unique one 
    while(file_exists($filename)) { 
     $filename = "images/IMG_" . mt_rand(1000,9999) . ".jpg"; 
    } 
    //Now let's save the file 
    imagejpeg($edit_img, $filename); 
    //And finally return the file url 
    return $filename; 
} 
} 

如果你不想保存它,你應該輸出直接到瀏覽器。你不需要任何img標籤。這是因爲使用imagejpeg時,就像您在瀏覽器中直接打開了一個圖像文件。 (我不知道我是否清楚,我只是想描述它的真實性)

所以php文件將是相同的,你不需要任何html。只需調用該函數即可。它會做什麼是需要它:

imgResize::resizeAndOutput($img);

0
class imgResize 

{ 公共靜態功能resizeAndOutput($ IMG){

$orginal_img = $img; 
$width = 591; 
$height = 332; 
$new_width = 161; 
$new_height = 91; 

$edit_img = imagecreatetruecolor($new_width, $new_height); 
$image = imagecreatefromjpeg($orginal_img); 
imagecopyresampled($edit_img, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
ob_start(); 
imagejpeg($edit_img); 
$image_data = ob_get_contents(); 
ob_end_clean(); 
return base64_encode($image_data); 

}

HTML

<img class="img_small" src="data:image/jpeg;base64,<?php echo imgResize::resizeAndOutput($img)"> 
+0

這個作品相當不錯,我希望我可以幫助這個其他傢伙hf – Wykk

相關問題