2016-12-14 63 views
0

所以我建立一個PHP頁面,並試圖在一個表中如何調整大小並顯示來自url的圖像?

目前我的代碼看起來像這樣顯示圖像:

echo "<h3>TEST TABLE</h3>"; 
    echo "<table width='350px' border='1px'>"; //blah blah 

    while ($row2 = $res2->fetch_assoc()){ // takeing data from database as url is stored in mysql database 
     echo"<tr><td>".$row2['Name']."</td>"; 
     echo"<td>".$row2['Description']."</td>"; 

     $image =$row2['Img']; 
     $imageData = base64_encode(file_get_contents($image)); 
     echo '<td><img src="data:image/jpeg;base64,'.$imageData.'"></td>'; //So this chunk is how I capture img from url and display 



     echo "<td><small>$Info</small></td></tr>"; 
    } 
    echo "</table>"; 

我做捕獲和URL成功地顯示圖像。然而,我不太確定如何調整imgs的大小,以便像500x500左右那樣固定大小。任何意見和建議,將不勝感激!謝謝!

回答

4

可以簡單地傳遞寬度,高度,以圖像標記,如:

<img src="data:image/jpeg;base64,'.$imageData.'" height="500" width="500"> 

其中高度= 「500」 寬度= 「500」 表示500像素。

如果您希望每幅圖像的尺寸爲500x500,則需要在圖像上傳時對其進行定義。

1

類似這樣的東西會有效, 獲取圖像並將其存儲在另一個變量中,並調用php resizer函數,並允許php本身有效地執行縮略圖。 即使你可以定製它,

// Get new sizes 
list($width, $height) = getimagesize($filename); 
$newwidth = $width * $percent; 
$newheight = $height * $percent; 

// Load 
$thumb = imagecreatetruecolor($newwidth, $newheight); 
$source = imagecreatefromjpeg($filename); 

// Resize 
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

// Output 
imagejpeg($thumb); 
相關問題