doumei8126 2014-06-30 10:03
浏览 26
已采纳

php混合关联数组,包含文本和图像

can you have a associative array containing text in the first element and a image in the 2nd. How would you declare a array like that?

I have a function that I would like to generate a random piece of text. Then it turns the text into a image. I want to include both the text and Image in the return value (which i figured a array will work great).

below a random text string of 6 chars is turend into a image and sent back via header. but I would like to send in back in a 2 element associative array. Elm1 => random text Elm2 => generated image

so its not a image file, but a generated image.

Sorry but Im very new to PHP

//Get each letter in one variable, we will format all letters different
$letter1=substr($rand_str,0,1);
$letter2=substr($rand_str,1,1);
$letter3=substr($rand_str,2,1);
$letter4=substr($rand_str,3,1);
$letter5=substr($rand_str,4,1);
$letter6=substr($rand_str,5,1);


//Creates an image from a png file. If you want to use gif or jpg images, just use the coresponding functions: imagecreatefromjpeg and imagecreatefromgif.
$image=imagecreatefrompng("./noise.png");
//$image = imagecreate(160, 45);

//Get a random angle for each letter to be rotated with.
$angle1 = rand(-20, 20);
$angle2 = rand(-20, 20);
$angle3 = rand(-20, 20);
$angle4 = rand(-20, 20);
$angle5 = rand(-20, 20);
$angle6 = rand(-20, 20);

//Get a random font. (In this examples, the fonts are located in "fonts" directory and named from 1.ttf to 10.ttf)
$font1 = rand(1, 12).".ttf";
$font2 = rand(1, 12).".ttf";
$font3 = rand(1, 12).".ttf";
$font4 = rand(1, 12).".ttf";
$font5 = rand(1, 12).".ttf";
$font6 = rand(1, 12).".ttf";

$colors[0]=array(0,0,0);
$colors[1]=array(0,0,0);
$colors[2]=array(0,0,0);
$colors[3]=array(0,0,0);
$colors[4]=array(0,0,0);
$colors[5]=array(0,0,0);
$colors[6]=array(0,0,0);



//Get a random color for each letter.
$color1=rand(0, 6);
$color2=rand(0, 6);
$color3=rand(0, 6);
$color4=rand(0, 6);
$color5=rand(0, 6);
$color6=rand(0, 6);

//Allocate colors for letters.
$bgColor = imagecolorallocate ($image, 255, 255, 255);
$textColor1 = imagecolorallocate ($image, $colors[$color1][0],$colors[$color1][1], $colors[$color1][2]);
$textColor2 = imagecolorallocate ($image, $colors[$color2][0],$colors[$color2][1], $colors[$color2][2]);
$textColor3 = imagecolorallocate ($image, $colors[$color3][0],$colors[$color3][1], $colors[$color3][2]);
$textColor4 = imagecolorallocate ($image, $colors[$color4][0],$colors[$color4][1], $colors[$color4][2]);
$textColor5 = imagecolorallocate ($image, $colors[$color5][0],$colors[$color5][1], $colors[$color5][2]);
$textColor6 = imagecolorallocate ($image, $colors[$color6][0],$colors[$color6][1], $colors[$color6][2]);

//Write text to the image using TrueType fonts.
$size = 15;

imagestring  ( $image  , rand(5,8)  , rand(5,15)  , rand(0,17)  , $letter1  , $textColor1 );
imagestring  ( $image  , rand(5,8)  , rand(30,40)  , rand(0,17)  , $letter2  , $textColor2 );
imagestring  ( $image  , rand(5,8)  , rand(55,65)  , rand(0,17)  , $letter3  , $textColor3 );
imagestring  ( $image  , rand(5,8)  , rand(80,90)  , rand(0,17)  , $letter4  , $textColor4 );
imagestring  ( $image  , rand(5,8)  , rand(105,115)  , rand(0,17)  , $letter5  , $textColor5 );
imagestring  ( $image  , rand(5,8)  , rand(130,140)  , rand(0,17)  , $letter6  , $textColor6 );

header('Content-type: image/jpeg');
//Output image to browser
imagejpeg($image);
//Destroys the image
imagedestroy($image);

Im not sure why I get marked down on this.

Is this not what stackoverflow is for

( [after some effort by one self] ask a question to get a answer from someone higher skilled.)

Then you get some guy with who thinks in his oppinion that this is a silly questions and 'click' marked down. Come on admin (markdown reason).

  • 写回答

1条回答 默认 最新

  • dongxuan2015 2014-08-14 10:09
    关注

    Just create the array, php doesn't really care what type a variable contains.

    Instead of:

    header('Content-type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
    

    just do:

    $return = array(
      'text' => $rand_str,
      'image' => $image
    );
    return $return;
    

    (assuming the code you posted is inside a function. Without knowing more on how you call the code it's hard to give a definite answer)

    There is no need to output it to the browser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看