我正在从图像创建缩略图,这些图像的路径已经存储在MySQL数据库中。但是当我回显缩略图时,只显示一个图像图标,而不显示缩略图。我需要改变什么?请保持您的答案简单,因为我是新手PHP。
下面是我使用的代码:
<?php
//database connectivity code
$query = mysql_query("SELECT * FROM images");
while($row = mysql_fetch_array($query))
{
$im = imagecreatefromjpeg($row['image_path']);
$width = imagesx($im);
$height = imagesy($im);
$imgw = 150;
$imgh = $height / $width * $imgw;
$thumb = imagecreatetruecolor($imgw,$imgh);
imagecopyresampled($thumb,$im, 0,0,0,0,$imgw, $imgh, ImageSX($im), ImageSY($im));
?>
<img src = "<?php echo $thumb ?>" >
<?php
} //closing while
?>我试着使用了许多其他函数,但都没有起到多大作用。它们中的大多数都显示了异常长的不可读数据字符串。请指出该怎么做。
提前谢谢。
发布于 2016-04-25 00:08:18
我对PHP图像函数一无所知,但是...
变量$thumb似乎是从imagecopyresampled返回的资源ID。img标记上的src属性应该是图像文件的路径。
发布于 2016-04-25 00:19:46
此示例将不起作用。你必须创建两个文件:
<?php
# image.php file
$query = mysql_query("SELECT * FROM images WHERE id=" . mysql_real_escape_string($_GET['image_id']));
row = mysql_fetch_array($query)
$im = imagecreatefromjpeg($row['image_path']);
$width = imagesx($im);
$height = imagesy($im);
$imgw = 150;
$imgh = $height / $width * $imgw;
$thumb = imagecreatetruecolor($imgw,$imgh);
imagecopyresampled($thumb,$im, 0,0,0,0,$imgw, $imgh, ImageSX($im), ImageSY($im));
imagejpeg($thumb, null, 100);
?>然后你可以在你的html中添加
<?php
$query = mysql_query("SELECT * FROM images");
while($row = mysql_fetch_array($query))
{
echo '<img src = "image.php?image_id='.$row['id'].'">';
}
?>https://stackoverflow.com/questions/36825455
复制相似问题