Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass php generated image into html meta tag

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;
like image 641
Garret Kaye Avatar asked Aug 28 '16 07:08

Garret Kaye


People also ask

How do you add a meta image in HTML?

The <meta> tag defines metadata about an HTML document. Metadata is data (information) about data. <meta> tags always go inside the <head> element, and are typically used to specify character set, page description, keywords, author of the document, and viewport settings.

How do I change meta tags in PHP?

You can define your different meta tags on each of your pages by this: $meta['keywords'] = "Keyoword, more words, ..."; $meta['description'] = "..."; <? php include('header.

What is meta data in PHP?

Metadata ¶ The metadata describes the columns found in the result set. All metadata sent by MySQL is accessible through the mysqli interface. The extension performs no or negligible changes to the information it receives. Differences between MySQL server versions are not aligned.

Is meta tag one word?

or met·a·tag a tag in HTML that is inserted at the top of a web page chiefly to describe its content and provide keywords for use by search engines.


1 Answers

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);
?>
like image 117
Reeno Avatar answered Oct 26 '22 04:10

Reeno