воскресенье, 12 сентября 2010 г.

How to Convert image to string in PHP

I have been looking to send the output from GD to a text string without proxying via a file or to a browser.

I have come up with a solution.

This code buffers the output between the ob_start() and ob_end() functions into ob_get_contents()

See the example below

// Create a test source image for this example
$im imagecreatetruecolor(30050);
$text_color imagecolorallocate($im2331491);
imagestring($im155,  'A Simple Text String'$text_color);

// start buffering
// output jpeg (or any other chosen) format & quality
// capture output to string
$contents ob_get_contents();
// end capture

// be tidy; free up memory

// lastly (for the example) we are writing the string to a file
$fh fopen("./temp/img.jpg""a+" );
    fwrite$fh$contents );
fclose$fh );

Комментариев нет: