调整大小的base64图片 [英] Resizing base64 Images
问题描述
我有多个图片 - 保存为Base64字符串,现在我想调整这些图像,让他们的缩略图...
I have multiple Images - saved as Base64 Strings and now i want to resize these images to get thumbnails of them...
最好将使用Javascript(节点服务器)来调整它们的大小,但它有可能用PHP来调整他们。
Best would be using Javascript (Node-Server) to resize them, but it would be possible to resize them with php, too.
在此先感谢
推荐答案
我同意乔恩汉娜方法:不要解析中的Base64 code则重新取样之前装载到GD图像。但是拿回来数据它并不像我那么容易不过。在GAE PHP它需要 打开输出缓冲的通过设置 output_buffering =ON
在php.ini文件。
I agree to the method from Jon Hanna: Do Parsing the Base64code then load it to GD Image before Resample. However to get it back as data it is not as easy as I though. On php in GAE it will need to enable output buffering by setting output_buffering = "On"
in php.ini file.
下面我详细解释了一步。
Here I explain the step in detail.
本文档是作为参考,以创建图像的资源使用Base64编码的解析code:<一href=\"http://php.net/manual/en/function.imagecreatefromstring.php\">http://php.net/manual/en/function.imagecreatefromstring.php
This doc is taken as reference to Create Image Resource using the Parsing of Base64code: 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));
这是它可以直接把到重新取样功能的图像资源:<一href=\"http://php.net/manual/en/function.imagecopyresampled.php\">http://php.net/manual/en/function.imagecopyresampled.php
This is an image resource which can be directly put to the Resample function: 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);
结果也是一个图像资源。为了得到它作为一个数据,我们需要的缓存。结果
看到
<一href=\"http://stackoverflow.com/questions/8502610/how-to-create-a-base64en$c$cd-string-from-image-resource\">how创建从图像资源一个base64en codeD字符串
The result is also an image resource. To get it as a data we need Buffering.
See
how to create a base64encoded string from image resource
// Buffering
ob_start();
imagepng($image_p);
$data = ob_get_contents();
ob_end_clean();
使用DOC下面我设置一个GCS斗的我的项目的作为一个网站,这样我就可以商店和放大器;显示它直接:
<一href=\"https://cloud.google.com/storage/docs/website-configuration#tips\">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");
这篇关于调整大小的base64图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!