doumei1203 2015-12-28 16:05
浏览 38
已采纳

gd库预览图像

I have used gd library to write text on image and save one normal and one small into a folder. Now I want to give the user a preview of the generated before saving it to file.

This is the code I did to test this:

<script src="https://code.jquery.com/jquery-2.1.4.min.js">   </script>

<form action="" method="post">
<p>Slide Text:
 <textarea name="slide_text" rows="10" cols="50"></textarea>

</p>
 <p>
  Font Size:
  <select name="font_size" class="font_size"></select>
 </p>    
 <p>
  <input type="submit" value = "Write"/>
 </p>


 <script>
  $(function(){
  var $select = $(".font_size");
  for (i=1;i<=50;i++){
    $select.append($('<option></option>').val(i).html(i + ' ' + 'px'))
   }
  });
</script>


<?php 

if(isset($_POST['slide_text'])) {
//treat the values of the form

$text = $_POST['slide_text'];

$image = imagecreatefrompng('template/template.png');

imagealphablending($image, true);

$color = imagecolorallocate($image, 49,117, 175);

$font = 'font/arialbd.ttf';

$font_size = $_POST['font_size'];

$start_x = 50;

$start_y = 175;

$max_width = 578;

$newname = time();
$random = rand(100,999);
$name = $newname.$random;

imagettftext($image, $font_size, 0, $start_x, $start_y, $color, $font, $text); 


imagepng($image);


}

?>

When I submit the form, I get this output instead of the image:

‰PNG IHDRЕXHQ‰ IDATxœìÝyŒeW~ößïlw}û«½ª÷nv÷°»¹Íp8䬙k4K‘2¶%ì ÁQÀðA²8È‚ ‰Ø‘mÉ–œÈ##F³¯îäpí}«®®½êíïnçœ_þxÕÍæÚ›êfñ|@²›UïÝwî[îûÞsÎý|éb™&@Dýá8Žã8ŽsˆˆÀìN·ÊqÇqœíOdšÀØÑÿÒ5ÿuÇqǹe[qb”4\‡ã8Žã8ï;áæk8Žã8Žóþ% ×Ãá8Žã8ÎûN Œº8ܼ ÇqÇqn¿QÒpC*Žã8Žã¼ÜŠã8Žã8M\àpÇqç}'îtÇqçÆZ2ÖZKŒ!0DηNà‰Hk,1D)˜«Ý}•Žã8ŽssC",¬í%…5VJúRp@ya†i‘çšV =_‰k3‡±VkB06J+.p8Žã8ÎM#€aZ¬v‡º0Èy%P'´6ý¬(´EŽ [-ù¯ˆŒ¡Ašw©!,‡*•üNïÇ_8ÇqçæX¢aVt“çÖ‚`l¼Ô"Ȥ¹îg…é™f%ò•ˆµÑͺI(ûRI~§wô6sÃqÇqn”6¶ŸæƒD‡J”Qñ%‚µÖjIa ˆ@p²PX(ÀZk9ò/G¡¯„ÑX’ý´ÕKÖÛÃ8T…¶lnÌ ÓY®=%·ÓpÃqÇqnœµ”š!4K~5”ÆP?É5An¨°$*ÎcIr!˘Ö9¢%Ú规X


Is there a way to to convert this into an image without using header('Content-Type: image/png'); ?

The only way that I can make this work is to put all the code that generates the image into a file and add header('Content-Type: image/png'), then add this file as the action of the form above.

  • 写回答

1条回答 默认 最新

  • doulidai6316 2015-12-28 16:10
    关注

    You can inline the image

    ob_start();
    
    imagepng($image);
    $img_data=ob_get_clean();
    imagedestroy($image);
    
    echo '<img src="data:image/png;base64,'.base64_encode($img_data).' />';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示