php - 在发送给客户端之前在 PHP 中调整 BLOB 图像的大小

标签 php html mysql blob

我有一张图像,我正在从数据库中以 BLOB 形式获取图像,但是我正在向客户端发送完整尺寸的图像,然后在他们这边调整大小。图片可能会变得非常大,所以我想在将图片发送给客户之前调整图片的大小。

我目前有以下代码:

if (!empty($row['img'])) {$img = $row['img'];}
$width = imagesx(imagecreatefromstring($img));
$height = imagesy(imagecreatefromstring($img));
$resizer = $width/200;
$newHeight = floor($height/$resizer);
$news = $news . "<div class='newsItem' style='min-height:".$newHeight."px;'>";
if (isset($img)) {$news = $news . "<img src='data:image/jpeg;base64,".base64_encode($img)."' width='200' height='".$newHeight."'>";}
$news = $news . "<h2>".$title."</h2><hr class='newsHr'><span>".$text."</span></div>";

我可以使用哪些函数来调整 $img 的大小?

最佳答案

这应该适合您。示例取自 Manual

<?php    
// Load
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);

    // Resize
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    // Output
    imagejpeg($thumb);
?>

将 BLOB mage 转换为文件使用。

file_put_contents('/path/to/new/file_name', $my_blob);

关于php - 在发送给客户端之前在 PHP 中调整 BLOB 图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30277816/

相关文章:

PHP 将周数和年份转换回 Carbon?

php - Laravel 中 hasManyThrough 的问题

mysql - 运行 sqoop list 数据库命令时出现 cloudera 链接错误

php - 将一个表中的字段保存到 PHP 变量并在另一个查询中使用

c# - ADO.NET MVC 查询外键

php - 在 php 中获取锁的最佳方法

php - 为什么 is_numeric(NAN) 返回 TRUE?

HTML5 验证 - 进行实时验证的简便方法?

HTML和CSS如何在将背景图像添加到该部分后隐藏文本

javascript - 密码未显示