2016-12-14 78 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);