看看函数imagecreatefromstring
http://php.net/manual/en/function.imagecreatefromstring.php
$image = imagecreatefromstring($imageSrc);
if ($image !== FALSE) {
// the variable is now a valid image resource
}
至于您的查询,它返回多行。您需要一一获取行。
$result = mysql_query("SELECT source FROM photos ORDER BY id DESC LIMIT 16");
while ($row = mysql_fetch_assoc($result)) {
$image = imagecreatefromstring($row['source']);
if ($image !== FALSE) {
// the variable is now a valid image resource
}
}
然而,它将非常耗费资源。更好的解决方案是将图像存储在磁盘上,并在数据库中拥有图像的路径。
还要记住,程序 mysql 函数已被弃用。你应该转移到mysqli。
http://fr2.php.net/manual/en/book.mysqli.php
编辑:你的问题没有具体说明你想要什么,无论如何。此代码(未经测试)将以类似画廊的方式并排绘制缩略图。有一些方法可以简化它,但我已经写了它,所以它很容易理解。
$num_columns = 4; // the number of thumbnails per row
$thumb_width = 400;
$thumb_height = 300;
$result = mysql_query("SELECT source FROM photos ORDER BY id DESC LIMIT 16");
// the actual number of results
$num_photos = mysql_num_rows($result);
$num_rows = ceil($num_photos / $num_columns);
$gallery_width = $num_columns * $thumb_width;
$gallery_height = $num_rows * $thumb_height;
// create a large empty image that will fit all thumbnails
$gallery = imagecreatetruecolor($gallery_width, $gallery_height);
$x = 0;
$y = 0;
// fetch the images one by one
while ($row = mysql_fetch_assoc($result)) {
$image = imagecreatefromstring($row['source']);
// the variable is now a valid image resource
if ($image !== FALSE) {
// grab the size of the image
$image_width = imagesx($image);
$image_height = imagesy($image);
// draw and resize the image to the next position in the gallery
imagecopyresized($gallery, $image, $x, $y, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);
// move the next drawing position to the right
$x += $thumb_width;
// if it has reached the far-right then move down a row and reset the x position
if ($x >= $gallery_width) {
$y += $thumb_height;
$x = 0;
}
// destroy the resource to free the memory
imagedestroy($image);
}
}
mysql_free_result($result);
// send the gallery image to the browser in JPEG
header('Content-Type: image/jpeg');
imagejpeg($gallery);
编辑:代码中的错字已修复