代码之家  ›  专栏  ›  技术社区  ›  Steve K

使用PHP从数据库调整图像大小

  •  1
  • Steve K  · 技术社区  · 15 年前

    我目前有一个脚本,它将从数据库中提取图像内容,并使用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);
    

    目前,这个脚本适用于大多数缩略图,并根据需要在屏幕上显示它们。但是,当我上传一个高宽度的图像时,比如超过1000px,它会被存储在数据库中,但是脚本不会显示重新取样的图像。

    为了进行故障排除,我检查了数据库,并打印了查询的输出。一切似乎都很正经…有人能告诉我是什么导致了这种行为吗?非常感谢您的帮助或指导!

    1 回复  |  直到 15 年前
        1
  •  2
  •   Cullen Walsh    15 年前

    $5表示PHP内存不足。检查您的PHP错误日志。