我有一张图像,我正在从数据库中以 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/