使用 PHP 从数据库调整图像大小
我目前有一个脚本,它将从数据库中提取图像内容,并使用 createimagefromstring() 返回调整大小(重新采样)的图像以供显示:
$max_width = 170;
// Get the image
@mysql_connect('host', 'user', 'pass');
@mysql_select_db('db');
$query = "SELECT `thumbnail_contents`, `thumbnail_mimetype` FROM `videos` WHERE video_number = '$video_number'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_object($result);
// Resize if pic bigger than $max_width
// Set the header
header('Content-type:' . $row->thumbnail_mimetype);
// Get original size of image
$image = imagecreatefromstring($row->thumbnail_contents);
$current_width = imagesx($image);
$current_height = imagesy($image);
// Set thumbnail width
$widths = array($current_width, $max_width);
$new_width = min($widths);
// Calculate thumbnail height from given width to maintain ratio
$new_height = $current_height / $current_width*$new_width;
// Create new image using thumbnail sizes
$thumb = imagecreatetruecolor($new_width,$new_height);
// Copy original image to thumbnail
imagecopyresampled($thumb,$image,0,0,0,0,$new_width,$new_height,imagesx($image),imagesy($image));
// Show thumbnail on screen
$show = imagejpeg($thumb);
// Clean memory
imagedestroy($image);
imagedestroy($thumb);
目前,此脚本适用于大多数缩略图,并根据需要在屏幕上显示它们。 但是,当我上传高宽度的图像(例如超过 1000 像素)时,它会存储在数据库中,但脚本不会显示重新采样的图像。
为了排除故障,我检查了数据库,并打印了查询的输出。 一切似乎都很顺利......有人能告诉我是什么导致了这种行为吗? 非常感谢任何帮助或指导!
I currently have a script which will pull in image contents from a database and, using createimagefromstring(), returns a resized (resampled) image for display:
$max_width = 170;
// Get the image
@mysql_connect('host', 'user', 'pass');
@mysql_select_db('db');
$query = "SELECT `thumbnail_contents`, `thumbnail_mimetype` FROM `videos` WHERE video_number = '$video_number'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_object($result);
// Resize if pic bigger than $max_width
// Set the header
header('Content-type:' . $row->thumbnail_mimetype);
// Get original size of image
$image = imagecreatefromstring($row->thumbnail_contents);
$current_width = imagesx($image);
$current_height = imagesy($image);
// Set thumbnail width
$widths = array($current_width, $max_width);
$new_width = min($widths);
// Calculate thumbnail height from given width to maintain ratio
$new_height = $current_height / $current_width*$new_width;
// Create new image using thumbnail sizes
$thumb = imagecreatetruecolor($new_width,$new_height);
// Copy original image to thumbnail
imagecopyresampled($thumb,$image,0,0,0,0,$new_width,$new_height,imagesx($image),imagesy($image));
// Show thumbnail on screen
$show = imagejpeg($thumb);
// Clean memory
imagedestroy($image);
imagedestroy($thumb);
Currently, this script works for most thumbnails and shows them on-screen as desired. However, when I upload an image with a high width, say over 1000px, it gets stored in the database, but the script doesn't show the resampled image.
To troubleshoot, I've checked the db, as well as print_r'd the output from the query. Everything seems to be kosher... Could someone tell me what might be causing such behavior? Any help or guidance is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$5 表示 php 内存不足。 检查你的 php 错误日志。
$5 says php is running out of memory. Check your php error logs.