【发布时间】:2012-09-01 23:28:19
【问题描述】:
我有多个图像 - 保存为 Base64 字符串,现在我想调整这些图像的大小以获取它们的缩略图...
最好使用 Javascript (Node-Server) 来调整它们的大小,但也可以使用 php 来调整它们的大小。
提前致谢
【问题讨论】:
标签: php image node.js resize base64
我有多个图像 - 保存为 Base64 字符串,现在我想调整这些图像的大小以获取它们的缩略图...
最好使用 Javascript (Node-Server) 来调整它们的大小,但也可以使用 php 来调整它们的大小。
提前致谢
【问题讨论】:
标签: php image node.js resize base64
我同意the method from Jon Hanna:解析 Base64code,然后在重新采样之前将其加载到 GD Image。然而,要将它作为数据取回,它并不像我那么容易。在 GAE 中的 php 上,需要通过在 php.ini 文件中设置 output_buffering = "On" 来设置 enable output buffering。
这里我详细解释一下步骤。
此文档被视为使用 Base64code 的解析创建图像 Resource 的参考:http://php.net/manual/en/function.imagecreatefromstring.php
// Create image resource from Base64code
$data64 = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$image = imagecreatefromstring(base64_decode($data64));
这是一个图片资源,可以直接放到Resample函数中:http://php.net/manual/en/function.imagecopyresampled.php
// Resample
$image_p = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_w, $new_h, $org_w, $org_h);
结果也是一个图像资源。要将其作为数据获取,我们需要缓冲。
看
how to create a base64encoded string from image resource
// Buffering
ob_start();
imagepng($image_p);
$data = ob_get_contents();
ob_end_clean();
使用下面的文档,我将 my project 上的 GCS 存储桶设置为网站,以便我可以直接存储和显示它: https://cloud.google.com/storage/docs/website-configuration#tips
//Store & Display
$context = stream_context_create([
'gs' =>[
'acl'=> 'public-read',
'Content-Type' => 'image/jpeg',
'enable_cache' => true,
'enable_optimistic_cache' => true,
'read_cache_expiry_seconds' => 300,
]
]);
file_put_contents("gs://mybucket/resample/image.jpeg", $data, false, $context);
header("Location: http://mybucket/resample/image.jpeg");
【讨论】:
imagecopyresampled() 产生的质量比imagecopyresized() 好得多,因为它会进行插值。
最好的办法是在 PHP 中使用 PHPThumb。
另一种方法是根据您的喜好调用 ImageMagick:
【讨论】:
【讨论】:
也许你可以使用一个库来处理它。试试宽幅图像。我已经使用它并且工作得很好。
例子:
$image = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $req->image));
$thumbnail = WideImage::load($image)
->resize(300, 300, 'inside')
->crop('center', 'center', 300, 300);
【讨论】: