I am trying to figure out a more efficient way to create 1px by 1px image (jpg, png and gif) from one RGB color code, in php.
The example below shows one way to execute it, but I was hoping for some kind of algorithm that would get me the same result without having to load any libraries or php extensions.
Example:
function rgbToDataUri($r,$g,$b,$type)
{
$im = imageCreateTrueColor(1, 1);
imageFill($im, 0, 0, ImageColorAllocate($im,$r,$g,$b));
ob_start();
switch($type)
{
case 'gif':
imageGif($im);
break;
case 'jpg':
case 'jpeg':
imageJpeg($im);
break;
default:
imagePng($im);
}
$img = ob_get_contents();
ob_end_clean();
return 'data:image/' . $type . ';base64,' . base64_encode($img);
}
echo rgbToDataUri(0,0,0,'gif');
Conclusion:
data:image/gif;base64,R0lGODdhAQABAIAAAAQCBAAAACwAAAAAAQABAAACAkQBADs=
My goals (in order of priority):
- Low memory consumption
- High processor performance
- High processing speed
Requirements include support for gif, jpg and png. Anywhere from 20 to 50 of these single pixel images will be created with each request (each pixel is independent of the others).
How to create a binary file for 1px monochrome image?