我有透明的gif图像,我想转换成字符串,保存到数据库,以后从数据库检索和读取像素颜色和透明度。但在我将图像转换为字符串后,我无法恢复原始图像。Base64解码后的字符串和原始字符串看起来是一样的。但是我不能从解码的字符串中重新创建图像颜色和透明度??我尝试过imagecreatefromstring(),并尝试将字符串放到文件中,然后使用imagecreatefromgif()。这两个函数产生的像素颜色都不同于原始的was。
但是如何从压缩字符串中创建gd图像呢?我需要获取像素颜色。因此,我需要gd对象,以便使用gd函数。
在开始(Step2)中,第一个像素是20,20,20,20 (RGBA)
经过(Step7)转换成字符串,编码,解码后的第一个像素是20,22,20,0 (RGBA)
不同的G颜色和透明度
如果使用其他数字,有时会使所有四个RGBA颜色参数不同。如何解决以及为什么会发生这种情况?
<?php
$width = 300;
$height = 300;
//1) Create image
$img = imageCreateTrueColor($width, $height); //ImageCreate (width, height)
imagealphablending($img, false); //wihtout these two does not work
imagesavealpha($img, true); //without these two does not work
//2)Put a pixel on the image
$c1 = ImageColorAllocateAlpha($img, 20, 20, 20, 20);
imagesetpixel ( $img , 0 , 0 , $c1);
//check pixel data
$cat = imagecolorat( $img, 0 ,0 );
$cfi = imagecolorsforindex($img, $cat);
echo '<br><br> 17 before decoding cat='.$cat.', cfi = '; print_r($cfi);
// 17 before decoding cat=336860180, cfi = Array ( [red] => 20 [green] => 20 [blue] => 20 [alpha] => 20 )
//3) convert image to string
ob_start();
imagegif( $img );//imagegif — Output image to browser or file
$image_data = ob_get_contents();
ob_end_clean();
echo '<br><br> ***********************************<br> image_data='. $image_data;
//GIF87a,,�,,,��������ڋ����H�扦�ʶ���L�������� �Ģ�L*�̦� �J�Ԫ���j�ܮ������N���� �������������(8HXhx��������)9IYiy�������� *:JZjz�������� +;K[k{��������,N^n~��������/?O_o��������0���<�0�:|1�ĉ+Z��1�Ƨ�;z�2�ȑ$K�<�2�ʕ,[�| 3�̙4kڼ�3�Ν<{�� 4�СD�=�4�ҥL�:} 5�ԩT�Z��5�֭\�z� 6�رd˚=�6�ڵlۺ}7�ܹt�ڽ�7�|���8����>�8��Ō;~9��ɔ+[��9��͜;{�:��ѤK�>�:��լ�;
//4) encode the string, in order to save to datbase, etc...
$img64str = base64_encode($image_data);
echo '<br><br> ***********************************<br> img64='. $img64str;
//img64=R0lGODdhLAEsAYAAAAQCBBQWFCwAAAAALAEsAQAC/gyOqcvtD6OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5LL5jE6r1+y2+w2Py+f0uv2Oz+v3/L7/DxgoOEhYaHiImKi4yNjo+AgZKTlJWWl5iZmpucnZ6fkJGio6SlpqeoqaqrrK2ur6ChsrO0tba3uLm6u7y9vr+wscLDxMXGx8jJysvMzc7PwMHS09TV1tfY2drb3N3e39DR4uPk5ebn6Onq6+zt7u/g4fLz9PX29/j5+vv8/f7/8PMKDAgQQLGjyIMKHChQwbOnwIMaLEiRQrWryIMaPGp40cO3r8CDKkyJEkS5o8iTKlypUsW7p8CTOmzJk0a9q8iTOnzp08e/r8CTSo0KFEixo9ijSp0qVMmzp9CjWq1KlUq1q9ijWr1q1cu3r9Cjas2LFky5o9izat2rVs27p9Czeu3Ll069q9izev3r18+/r9Cziw4MGECxs+jDix4sWMGzt+DDmy5MmUK1u+jDmz5s2cO3v+DDq06NGkS5s+jTq16tWskRYAADs=
//5) decode the string retrieved from database, file, etc....
$imgdec = base64_decode($img64str); // =$image_data
echo '<br><br> ***********************************<br> imgdec='. $imgdec;
//GIF87a,,�,,,��������ڋ����H�扦�ʶ���L�������� �Ģ�L*�̦� �J�Ԫ���j�ܮ������N���� �������������(8HXhx��������)9IYiy�������� *:JZjz�������� +;K[k{��������,N^n~��������/?O_o��������0���<�0�:|1�ĉ+Z��1�Ƨ�;z�2�ȑ$K�<�2�ʕ,[�| 3�̙4kڼ�3�Ν<{�� 4�СD�=�4�ҥL�:} 5�ԩT�Z��5�֭\�z� 6�رd˚=�6�ڵlۺ}7�ܹt�ڽ�7�|���8����>�8��Ō;~9��ɔ+[��9��͜;{�:��ѤK�>�:��լ�;
//6a) create a new image from the string (try or 6a or 6b - produce the same, G color is 22 instead of 20, transparency is 0 instead of 20)
$imgD = imagecreatefromstring( $imgdec );
//6b)
file_put_contents( __DIR__ . 'px1.gif', $imgdec );
$imgD = imagecreatefromgif( __DIR__ . 'px1.gif' );
//END 7) you see that the first pixel data differs
$cat = imagecolorat( $imgD, 0 ,0 );
$cfi = imagecolorsforindex( $imgD, $cat );
echo '<br><br> 112 cat='.$cat.', cfi = '; print_r( $cfi );
list($width, $height, $type, $attr) = getimagesize( $fname );
echo ' width='.$width.', $height'.$height.', $type'.$type.', $attr'.$attr;
//112 cat=1, cfi = Array ( [red] => 20 [green] => 22 [blue] => 20 [alpha] => 0 ) width=, $height, $type, $attr如果我在step6中使用函数$imgD = imagecreatefromgif( $imgdec );,那么我根本得不到像素数据:
112 cat=, cfi = width=, $height, $type, $attr
发布于 2017-05-14 16:40:10
这样可以恢复像素颜色,但不能恢复透明度:
// 1-2) Create image with single pixel :
$img = imageCreateTrueColor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);
$c1 = ImageColorAllocateAlpha($img, 20, 20, 20, 20);
imagecolortransparent($img, $c1);
imagesetpixel ( $img , 0 , 0 , $c1);
//3) convert image to string
ob_start();
imagegif( $img );//imagegif — Output image to browser or file
$image_data = ob_get_contents();
ob_end_clean();
//4) encode the string, in order to save to datbase, etc...
$img64str = base64_encode($image_data);
//5) decode the string retrieved from database, file, etc....
$imgdec = base64_decode($img64str); // =$image_data然后恢复像素颜色(但找不到像素透明度值):
$imgD = imagecreatefromstring( $imgdec );
imagealphablending($imgD, false); //wihtout these two does not work
imagesavealpha($imgD, true); //without these two does not work
$cat = imagecolorat( $imgD, 0 ,0 );
$cfi = imagecolorsforindex( $imgD, $cat );
//echoes cfi = Array ( [red] => 20 [green] => 20 [blue] => 20 [alpha] => 127 )此外,将img保存到文件时,我看不到我设置的颜色。例如,如果我设置了暗红色的$c1 = ImageColorAllocateAlpha($img, 179, 29, 20, 0);,我可以在图像文件(imagegif( $img, $fname );)中看到,在编码/解码前后,在这两种情况下,颜色都是灰色的。也许我不understaNd透明度是如何工作的?也许它不适用于sepArate颜色,但仅适用于背景?
总结说,如果你需要颜色和透明度,最好使用svg,例如:https://github.com/jasonlong/geo_pattern。透明胶片和颜色正常工作。此外,base64编码的字符串更短。这个包有许多语言的实现,包括php ( https://github.com/RedeyeGroup/geopattern-php )和javascript ( https://github.com/btmills/geopattern )。
https://stackoverflow.com/questions/43958154
复制相似问题