繁体   English   中英

如何从gd字符串获取像素颜色信息? 如果我添加alpha,gd imagecreatefromstring()不会重现相同的图像

[英]How to get pixel color information from gd string ? gd imagecreatefromstring() does not reproduce the same image if i add alpha

我有透明的gif图像,我想将其转换为字符串,保存到数据库,然后从数据库中检索并读取像素颜色和透明胶片。 但是,在将图像转换为字符串之后,我无法恢复原始图像。 Base64解码的字符串和原始字符串看起来相同。 但是我无法从解码后的字符串中重新创建图像颜色和透明胶片? 我尝试过imagecreatefromstring() ,并尝试将字符串放入文件中,然后使用imagecreatefromgif() 这两个函数产生的像素颜色与原始颜色不同。

但是如何从压缩字符串创建gd图像? 我需要获取像素颜色。 因此,我需要gd对象,才能使用gd函数。

在开始(步骤2)中,第一个像素为20,20,20,20 (RGBA)

after(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

如果我在步骤6中使用函数$imgD = imagecreatefromgif( $imgdec ); 那么我根本没有得到像素数据:
112 cat=, cfi = width=, $height, $type, $attr

这样就可以恢复像素颜色,尽管不能恢复透明度:

// 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 ); )中看到,在两种情况下,在编码/解码之前和之后,颜色都是灰色。 也许我不了解透明度的工作原理? 也许不是用于单独的颜色,而是仅用于背景?

结论是,如果需要颜色和透明胶片,最好使用svg,例如: https : //github.com/jasonlong/geo_pattern 透明胶片和颜色正常工作。 此外,base64编码的字符串更短。 并且该软件包具有多种语言的实现,包括phphttps://github.com/RedeyeGroup/geopattern-php )和javascripthttps://github.com/btmills/geopattern )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM