9

Im trying to pass a merged image from php into a html meta tag (for a twitter summary card if you're wondering) but the data of the image is not being passed. When I run this code I get no errors from html or php:

PHP

$dest = imagecreatefromjpeg('http://www.website.com/Some-images/'.$postID.'.jpg'); 
$src = imagecreatefromjpeg('http://www.website.com/media/dog.jpg');



imagealphablending($dest, false);
imagesavealpha($dest, true);


imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); 

HTML

<meta name="twitter:image" content="'.$dest.'">

I'm not 100% sure that you can even pass a raw image at all into the content attribute of the meta tag but I'm thinking there should be a way to do this and I'm also thinking that this is what is causing the image to not show. I'm open to an html/css solution if a php solution is not possible. I've been stuck on this for a while so any suggestions and input you might have will be mighty appreciated. Thank You!

EDIT

I should add that this is a php script so the html is being created like this:

$html = '

<html>
<head>

<meta name="twitter:image" content="'.$dest.'">

</head>
<body>

</body>
</html>

';

echo $html;
miken32
  • 42,008
  • 16
  • 111
  • 154
Garret Kaye
  • 2,412
  • 4
  • 21
  • 45
  • If you put `var_dump($dest);` into your code, do you see the value of `$dest` showing, or nothing at all? – Martin Aug 28 '16 at 11:01
  • @MartinParkin yeah I get the output: "resource(3) of type (gd)" – Garret Kaye Aug 28 '16 at 14:59
  • I think your problem is that you're loading the resource into the webpage, but you're never actually displaying it (i.e, have no `` tag.) – Kal Zekdor Sep 06 '16 at 22:16
  • This won't work. 'imagecopymerge' returns the image data, which has to be saved to a file with 'imagejpg', this file then has to be referenced in the HTML. Alternative: You reference the PHP file in HTML and output the file without saving (also with 'imagejpg', but without the second parameter). – Reeno Sep 06 '16 at 22:17
  • Link about Twitter cards for anyone else who is unfamiliar: https://dev.twitter.com/cards/markup Looks like you'll need to break this into two bits, one html page which has the reference to the image url, and one php script which directly outputs the image (plenty of questions on SO on how to do that). So, basically, what @Reeno said. – Kal Zekdor Sep 06 '16 at 22:31

3 Answers3

11

This won't work. 'imagecopymerge' returns an image resource, which has to be sent to the browser as an image or saved to the server hard disk a file with 'imagejpeg'. If it's directly sent to the browser (first option), this PHP file then has to be referenced in the HTML.

So basically, in your HTML, reference to a PHP file, with your postid parameter:

<meta name="twitter:image" content="image.php?postid='.$postID.'">

In the file image.php create your file and output it (you should also add some validation code for $_GET['postid'] here):

<?php
$dest = imagecreatefromjpeg('http://www.website.com/Some-images/'.$_GET['postid'].'.jpg'); 
$src = imagecreatefromjpeg('http://www.website.com/media/dog.jpg');

imagealphablending($dest, false);
imagesavealpha($dest, true);

imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); 

header('Content-Type: image/jpg');
imagejpeg($dest);
?>
Reeno
  • 5,720
  • 11
  • 37
  • 50
  • Thanks a million for this I wish I could give you more points but as you can see I'm kinda running low. This method works great! – Garret Kaye Sep 06 '16 at 23:55
6

You could use base64

$dest = imagecreatefromjpeg('http://www.website.com/Some-images/'.$postID.'.jpg'); 
$src = imagecreatefromjpeg('http://www.website.com/media/dog.jpg');

imagealphablending($dest, false);
imagesavealpha($dest, true);

imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); 

$img = base64_encode($dest);

then use that string in the tag

<meta name="twitter:image" content="data:image/png;base64,<?php echo $img; ?>">
Sam Battat
  • 5,725
  • 1
  • 20
  • 29
1

If you want to place the contents of a PHP variable into some HTML you need to echo the variable into the HTML attribute by placing it inside a PHP code block.

Something like this should achieve the aim:

<meta name="twitter:image" content="<?php echo $dest; ?>">
Martin
  • 16,093
  • 1
  • 29
  • 48
  • Thank you for the suggestion but I tried your solution and still no dice :( I have also edited my question to give more information – Garret Kaye Aug 28 '16 at 08:02