1

我想在生成图像的页面上有一个“打印此图像”按钮。我尝试将 html 代码放在我的image.php文件中(请参阅下面的代码),但它不会显示。有人说尝试将html和php代码分开。我尝试 <img src="image.php" alt="" />在另一个名为generate.php的文件上使用代码,但我得到的只是一个损坏的图像。请帮忙!:(

这是我的image.php文件的代码:

$num_fields = $_POST['num_fields'];
// create image
$image = imagecreate(500, 500);

// allocate some colors
$white    = imagecolorallocate($image, 255, 255, 255);
$pink     = imagecolorallocate($image, 255, 105, 180);
$red = imagecolorallocate($image, 255, 000, 000);
$green     = imagecolorallocate($image, 034, 139, 034);
$brown = imagecolorallocate($image, 139, 069, 019);
$yellow      = imagecolorallocate($image, 255, 255, 000);
$orange  = imagecolorallocate($image, 255, 140, 000);
$blue  = imagecolorallocate($image, 100, 149, 237);
$purple  = imagecolorallocate($image, 218, 112, 214);
$gray  = imagecolorallocate($image, 205, 205, 193);
$black    = imagecolorallocate($image, 000, 000, 000);
$cyan    = imagecolorallocate($image, 000, 255, 255);


//bg color//
imagefilledrectangle($image,0,0,500,500,$white);

$font = 'arial.ttf'; 

if($num_fields =='6')
{ 
// for each slices
imagefilledarc($image, 250, 250, $_POST['rating-1'], $_POST['rating-1'], 0, 60, $pink, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-2'], $_POST['rating-2'], 60, 120 , $green, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-3'], $_POST['rating-3'], 120, 180 , $red, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-4'], $_POST['rating-4'], 180, 240 , $gray, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-5'], $_POST['rating-5'], 240, 300 , $orange, IMG_ARC_EDGED);
imagefilledarc($image, 250, 250, $_POST['rating-6'], $_POST['rating-6'], 300, 360 , $blue, IMG_ARC_EDGED);


//outline
imagefilledarc($image, 250, 250, 400, 400, 0,360, $black, IMG_ARC_NOFILL); //outer circle
imagefilledarc($image, 250, 250, 320, 320, 0,360, $black, IMG_ARC_NOFILL); //inner circle
imageline($image, 250, 250, 450, 250, $black ); //line bet blue and pink 0 deg
imageline($image, 250, 250, 349, 78, $black ); //line bet orange and blue
imageline($image, 250, 250, 150, 78, $black ); //line bet orange and gray 
imageline($image, 250, 250, 50, 250, $black ); //line bet red and gray 
imageline($image, 250, 250, 151, 422, $black ); //line bet red and green 
imageline($image, 250, 250, 350, 422, $black ); //line bet pink and green 


//text legends 2
imagettftext($image, 9, 243, 425, 280, $black, $font, $_POST['field-1']);
imagettftext($image, 9, 183, 315, 415, $black, $font, $_POST['field-2']);
imagettftext($image, 9, 120, 130, 380, $black, $font, $_POST['field-3']);
imagettftext($image, 9, 60, 75, 210, $black, $font, $_POST['field-4']);
imagettftext($image, 9,0, 200, 80, $black, $font, $_POST['field-5']);
imagettftext($image, 9, 300, 378, 128, $black, $font, $_POST['field-6']);


// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);   
}
4

1 回答 1

0

我有点不清楚你想要什么,但是......你展示的代码只是制作图像。除此之外,您需要一个由某种脚本语言制作的 html 页面,该页面将查询转发到您的 image.php,例如(这是非常老式的 php 编码,看起来很难看,但它应该可以工作)

<html>
<head><title>My image</title></head>
<body>
<img src="image.php?<?php
    print"num_fields=${_POST['num_fields']}&rating-1=${_POST['rating-1']}&rating-2=${_POST['rating-2']}&rating-3=${_POST['rating-3']}&rating-4=${_POST['rating-4']}&rating-5=${_POST['rating-5']}&rating-6=${_POST['rating-6']}&field-1=${_POST['field-1']}&field-2=${_POST['field-2']}&field-3=${_POST['field-3']}&field-4=${_POST['field-4']}&field-5=${_POST['field-5']}&field-6=${_POST['field-6']}"
?>
" alt="" /> 
</body>
</html>

(“我的图片”部分当然可以是您喜欢的任何页面标题)

现在您可以开始在 and 标记之间添加更多内容。如果您在现有文件中添加一些 html 输出,它将被忽略或使您的图像无效。

于 2012-10-30T12:24:57.847 回答