Php GD Lib保存图像
我有以下代码片段Php GD Lib保存图像,php,gd,Php,Gd,我有以下代码片段 $code = generateCode($characters); /* font size will be 75% of the image height */ $font_size = $height * 0.75; $image = imagecreate($width, $height) or die('Cannot initialize new GD image stream'); /* set the colours
$code = generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
$code=generateCode($characters);
/*字体大小将是图像高度的75%*/
$font_size=$height*0.75;
$image=imagecreate($width,$height)或die('cannotinitializenewgdimage-stream');
/*定颜色*/
$background\u color=imagecolorallocate($image,255,255);
$text_color=imagecolorallocate($image,20,40,100);
$noise_color=imagecolorallocate($image,100120180);
/*在背景中生成随机点*/
对于($i=0;$i使用输出缓冲:
ob_start();
imagepng(...); // or imagejpeg(...);
$img=ob_end_clean();
由于图像现在保存在变量中,因此可以将图像存储在数据库中(无需保存到文件),也可以使用以下方式渲染图像:
header('Content-type: image/png');
echo $img;
是的,您可以,提示:第二个参数,如果未设置或为空,将直接输出原始图像流
希望有帮助
您没有明确说明是要将其保存到文件、数据库还是变量中。