2016-04-24 50 views
0

我从路径存储在MySQL数据库中的图像创建缩略图。但是,当我回显缩略图时,仅显示图像图标,而不显示缩略图。我需要改变什么?请保持您的答案简单,因为我是PHP新手。如何回显在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 
    ?> 

但没有做太大的帮助。他们中的大多数人显示了一串不可读的数据。请指出该怎么做。

在此先感谢。

回答

0

我什么都不知道PHP图像功能,但...

变量$拇指似乎是一个资源ID从imagecopyresampled返回。 img标签上的src属性应该是图像文件的路径。

0

这个例子不起作用。你必须创建2个文件:

<?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'].'">'; 
} 
?> 
+0

谢谢回复。我试了一下,但它似乎也没有这样工作。它仍然只显示图像图标来代替缩略图。 – Leeds

+0

你能从你的代码中获得更多吗?这种方法应该可以工作,但这取决于你的环境 –

+0

嗯,我想通了。我必须用中的php标签替换您建议的 Leeds