如何使动态创建的内容可共享


How to make dynamically created content sharable

我有一个网站,生成基于用户输入的反馈使用PHP,我想给人们的选项来分享他们的反馈,但我不确定如何。

我可以使用Open Graph并用PHP填充元数据,但它看起来不太好,人们可能只想分享网站而不是他们的反馈。

所以理想情况下,我希望能够分享一些类型的HTML或动态生成的图像。

Facebook的

https://developers.facebook.com/docs/plugins/share-button

似乎没有什么好的建议。

我更喜欢使用PHP或某种类型的Javascript来做它。

谢谢!

我认为应该这样做(而不是我没有尝试过)。facebook给出了两件事,在你的网页中包含两件事首先是javascript代码,其次是html标记示例html标记在

后面
<div data-href="https://developers.facebook.com/docs/plugins/" data-layout="button_count"></div>

你必须改变data-href属性的变量,如$url,它应该包含动态生成的url如果你想共享当前的url,你可以使用$_SERVER superglobal来构造它

最好的方法是使用php imageccreate()当你输入图像的src=时你只需将GET值放在你要调用的图像的URL中

 <img src="www.domain.com/myimage.png?textoverimage=customersname" />

但是你可以用php创建url,这样人们就可以通过简单的html表单提交他们想要的图像值,就像这样。

 <form method="get">
     <imput type="text" name="customername" />
     <imput type="submit"  />   
 </form>  
 <img src='www.domain.com/myimage.png?textoverimage=<?php echo $_GET["customername"];?>' />

然后php imagecreate()将获取这些值并使用它们在图像上放置文本,从这一点来看,使图像可共享很容易。

现在您可能习惯于在<img />src中调用图像文件,但在这种情况下,您将实际调用php文件,imageccreate()将从该php文件中生成所需的图像。下面是PHP文件的样子:

<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = @imagecreatefromjpeg("http://domain.com/imagefile.jpg");
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = $_GET["textoverimage"];

$font = '/stocky.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 120, 170, $grey, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

这是我发现动态生成带有文本的图像的最佳方式。