Facebook sharer, thumb is showed instead of large image - php

I have set up a contest, the contestants are asked to share their results on facebook.
I have the following button to share the results:
print('<a class="facebook stemspeler" style="color:#eec920; margin-right:15px;" target="_blank" onclick="return !window.open(this.href, \'Facebook\', \'width=1200,height=300\')" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.***.com%2Fallstars&picture=http%3A%2F%2Fwww.***.com%2Ftypo.php?id='.$makeUniq.'&title=tet&caption=test&quote=test&description=test">‌​Share</a>');
The $makeUniq makes sure that facebook fetches the results instead of caching the results the first time (When link stays the same it caches, now it doesn't because link changes every request).
The typo.php is the file that houses the image. It takes a standard template and transforms the contest inputs to a image overlay.
<?php
session_start();
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// Output to browser
header('Content-Type: image/png');
// Create a 300x150 image
//WHITE BACKGROUND COLOR
$im = imagecreatetruecolor(1000, 1000);
$im = imagecreatefrompng("images/test.png");
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
$test = imagecolorallocate($im, 255, 255, 0);
// Set the background to be white
imagefilledrectangle($im, 0, 0, 900, 990, $test);
// Path to our font file
$font = 'fonts/DINPro-Medium.ttf';
$fontSize = 24;
$angle = 0;
$x = 80;
$y = 150;
// Write it
imagettftext($im, $fontSize, $angle, $x, $y, $white, $font, ' : n');
$x = 80;
$y = 200;
// Write it
imagettftext($im, $fontSize, $angle, $x, $y, $white, $font, 'k : ');
$x = 80;
$y = 250;
// Write it
imagettftext($im, $fontSize, $angle, $x, $y, $white, $font, 'Ler :n');
$x = 80;
$y = 300;
// Write it
imagettftext($im, $fontSize, $angle, $x, $y, $white, $font, 'Mouwer : ');
$x = 80;
$y = 350;
// Write it
imagettftext($im, $fontSize, $angle, $x, $y, $white, $font, 'Rwer n');
$x = 80;
$y = 400;
// Write it
imagettftext($im, $fontSize, $angle, $x, $y, $white, $font, 'Rechtk : ');
$x = 80;
$y = 450;
// Write it
imagettftext($im, $fontSize, $angle, $x, $y, $white, $font, 'Cir : ');
imagepng($im);
imagedestroy($im);
?>
Up till here everything works like its supposed to be.
The problem is that when the facebook share functionality opens, facebook keeps showing the image as thumbnail instead of large image.
And the result I'm aiming for is the large image:
I have tried to change resolution many times, and there are several posts on the internet that are close to describe this problem.
Anybody has any idea how I could fix this problem?
Thanks in advance!

You can try to specify these OG elements:
og:image:width
og:image:height
these tags helps crawlers to obtain you image size before dowload it. Maybe you're getting a smaller image preview because crawler didn't get the right size at first and now you're getting preview from a cache.
EDIT:
Of course you should be sure that you're using an image that is at least 1200 x 630 px

The recommended picture size for a shared link on Facebook is 1200x630. If the picture is too small, Facebook will center crop it and only show a thumbnail of the picture. I believe de minimum size is 600x315, but the result might not be as good on high resolution devices. If you want it to look good and show the full size picture on all devices, use 1200x630px pictures.
You can find a references to those best practices
here.
EDIT:
Your problem might be due to the fact that the Facebook crawler caches the info it crawls, so your old picture might still be used by FB even if you fixed its resolution. Make sure to go here, to force the Facebook crawler to re-crawl you website and get the new data or simply empty its cache for a particular URL to force it to recrawl your website next time you share the link. Otherwise FB probably still use you old picture it already has in its cache.

Related

php Creating Multiple Image files with unique serial numbers getting error

I have a function set up to create multiple images with unique serial numbers on each of them. The problem is that its creating a small square on the browser and not redirecting the browser after saving back to the dashboard
header("Content-type: image/png");
$imgPath = 'background1.png';
$string = $serialNumber;
$directory = "serial_image/".$string.".png";
$image = imagecreatefrompng($imgPath);
$color = imagecolorallocate($image, 0, 0, 0);
$font = 'arial.ttf';
$fontSize = 13;
$x = 20;
$y = 212;
imagettftext($image, $fontSize, 0, $x, $y, $color, $font, $string);
imagepng($image, $directory);
I call the function to create 10 images each with the unique serial number. They are created but then the browser stops and just shows this square.
I have tried ob_start() and ob_flush() with no luck
I think its a header issue.
Can someone please help with this one.

How to remove reference to ttf file after using imagettftext?

I'm currently developing a captcha system for the site I'm currently developing for a school project.. here: http://rjtestsystem.atwebpages.com/
Here's the code, err, prototype I came up with so far:
<?php
if (isset($_GET['METHOD']) && $_GET['METHOD']=='captcha')
{ session_start();
$code=rand(1000,9999);
$_SESSION["code"] = $code;
session_write_close();
$font = rand(1,4).".ttf";
if ($font == "1.ttf")
$font_size = 20;
else if ($font == "2.ttf")
$font_size = 25;
else if ($font == "3.ttf")
$font_size = 30;
else if ($font == "4.ttf")
$font_size = 35;
$angle = 0;
$width = 120;
$height = 60;
$bounding_box = imagettfbbox($font_size, $angle, $font, $code);
$textwidth = abs($bounding_box[4] - $bounding_box[0]);
$textheight = abs($bounding_box[5] - $bounding_box[1]);
$x = ($width - $textwidth) / 2;
$y = (($height*3/4) + $textheight) / 2;
$image = imagecreatetruecolor($width, $height);
$text_background = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 230, 230, 230);
imagefill($image, 0, 0, $text_background);
imageline($image, 0, $y-rand(0,10), $width, $y-rand(0,10), $color);
imageline($image, 0, $y, $width, $y, $color);
imageline($image, 0, $y+rand(0,10), $width, $y-rand(0,20), $color);
imageline($image, $x+rand(0,$width/2), 0, $x+rand($width/2,$width), $height, $color);
imageline($image, $x+rand(0,$width/2), $height, $x+rand($width/2,$width), 0, $color);
for($i=0;$i<400;$i++)
{ imagesetpixel($image,rand()%$width,rand()%$height,$color);
}
imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $code);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
exit();
}
?>
<img src = "<?php echo $_SERVER['PHP_SELF']; ?>?METHOD=captcha">
The code seems to be working fine, but when I try to move the files elsewhere, the system would give me a message that the file is being used by another program. It seems that the references to the TTF files are still there even after executing the code. I've tried to look for a way to stop this behavior but I can't seem to get my searches right. Well, the problem goes away once I restart the server, but it's a hassle when debugging. Is there a way or proper method to remove the references / file handle / etc to the TTF files or close this process as soon as it's finished? If possible, I'd like to keep using this code.
I have only a year of experience in Php so please bear with my rudimentary way of coding and asking questions. If anyone can lead me to the right discussions, please do. Thank you.
I'm using WAMP server version 2.4.

Add Dynamic Text to images using GD Library or any other plugin in PHP

I want to make an app where user can see their meaning of name. I am done with the php part its just I am stuck how to output it to good looking image. I have used PHP image magician library before. But I am still not able to get it in the format I wanted.
Any help will be useful.
CHECK the image format below
https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDPCj8lZUELWsXD&w=470&h=246&url=http%3A%2F%2Fs3.alegra.me%2Fname%2Fnombre.php%3Fnombre%3Djayanta&cfs=1&upscale
$im = imagecreatefromjpeg('xc7ci4ptuxmgy3nd0brz.jpg');
$font="tahoma.ttf";
$red = imagecolorallocate($im, 255,0,0);
$string="JAYANTA";
$last_string= substr( $string, 1 );
$temp_x = 105;
$down_string[0]="JUST";
$down_string[1]="AMUSING";
$down_string[2]="YOUNG";
$down_string[3]="AMUSING";
$down_string[4]="NICE";
$down_string[5]="TOUGH";
$down_string[6]="AMUSING";
$bbox = imagefttext($im, 80, 0,90, 190, $red, $font, $string);
$temp_y=205; //y cord. of $down_string
for($x=1;$x < count($down_string);$x++){
for($y=0;$y < count($down_string);$y++){
$bbox = imagefttext($im, 12, 0, $temp_x, $temp_y, $red, $font, $down_string[$y][$x]);
$temp_x += 56+$y*1.8;
}
$temp_x = 105;
$temp_y+=15;
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
As you can see, ther is a lot of problems, but the solution is in the imagefttext function.

How we can change the font opacity and shadowing in imagettftext function?

I am creating a Text-Image on given template in which all parameter are dynamic,Its working fine! and creating image like,my php script is,
<?php
// To fetch template info from database
$template_query = mysql_query("SELECT * FROM templates WHERE templateID = '".$fetch['templateID']."'");
$temp_data = mysql_fetch_assoc($template_query);
//create and save images
$temp = '../'. $temp_data['blank_templates'];
//check image type
$image_extension = strtolower(substr(strrchr($temp_data['blank_templates'], "."), 1));
$im = imagecreatefromjpeg($temp);
$black = hexdec($temp_data['font_color']);
// Replacing path by your own font path
$font = '..'.$temp_data['font_file_upload'];
// Break it up into pieces 125 characters long
$no_of_characters_line = $temp_data['no_of_characters_line'];
$lines = explode('|', wordwrap($message, $no_of_characters_line, '|'));
// Starting Y position and X position
$y = $temp_data['position_from_top'];
$x = $temp_data['position_from_left'];
$font_size = $temp_data['font_size'];
$rotation_angle = $temp_data['rotation'];
$line_height = $temp_data['line_height'];
foreach ($lines as $line)
{
imagettftext($im, $font_size,$rotation_angle, $x, $y, $black, $font, $line);
// Increment Y so the next line is below the previous line
$y += $line_height;
}
$id = uniqid();
$save = '../messagesimage/'.$id. '.'.$image_extension;
$path_save = substr($save, 3);
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im,$save);
imagedestroy($im);
Which is creating image like..!
Now I want to add the ability to change font opacity and shadowing dynamically,Is it possible ? If yes then please help me to do this ..
Thanks in Advance
Wow, you've been waiting a while.
To give your text some transparency, you'll need to define your text color with an alpha channel.
$black = imageallocatecoloralpha(0,0,0,16);
To give your text some shadow
$shadow = imageallocatecoloralpha(0,0,0,64); // more transparent
imagettftext($im, $font_size,$rotation_angle, $x+1, $y+1, $shadow, $font, $line);
imagettftext($im, $font_size,$rotation_angle, $x, $y, $black, $font, $line);
Even later, but it seems the current answer is incorrect.
To define a color with transparency, you use the function imagecolorallocatealpha():
$black = imagecolorallocatealpha($image, 0, 0, 0, 50);
Shadows, like Michael pointed out, are quite simple(Just use the correct function name, and make sure you add the image object). However, if you want a more shadowy looking shadow, blur it a bit:
$shadow = imagecolorallocatealpha($im, 0, 0, 0, 50);
//Draw shadow text
imagettftext($im, $font_size, 0, $x, $y, $shadow, $fontn, $text);
//Blur
imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
//Draw text
imagettftext($im, $font_size, 0, $x, $y, $white, $fontn, $text);
After applying the above shadow method to an image, it renders similar to this:

Create White Box around Text GD-Lib

i want to add a white box around some text i add to an image via GD-Lib.
but i don't know how to do this best.
Here is my current code:
<?php
$textImg = imagecreatefromjpeg($tempImage);
$black = imagecolorallocate($textImg, 0, 0, 0);
$font = 'lib/verdana.ttf';
// Add the text
imagettftext($textImg, 20, 0, imagesx($textImg)*$textData['x']/100, imagesy($textImg)*$textData['y']/100, $black, $font, $textData['text']);
imagejpeg($textImg,$tempImage,$jpegQuality);
?>
I hope you can help me out.
You can use imagettfbbox() to get the coordinates of the bounding box by passing the same settings you use for the text itself (same text, font and size etc).
Once you have these coordinates you can use imagerectangle() to draw a border around the text, or you can use imagefilledrectangle() to draw a solid rectangle. Be sure to call it before you render the text with imagettftext()
A basic example is below but will need some tweaking as most of it is from memory and I suspect the $x and $y calculation could be done better as it probably doesn't work with varying canvas sizes as it is now. However, it demonstrates the principle.
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// The text to draw
$text = 'Testing';
// Replace path by your own font path
$font = 'verdana.ttf';
// Add the text
$bbox = imagettfbbox(20, 0, $font, $text);
$x = $bbox[1] + (imagesx($im) / 2) - ($bbox[4]);
$y = $bbox[3] + (imagesy($im) / 2) - ($bbox[5]);
imagerectangle($im, 0, 0, $x, $y, $white);
imagettftext($im, 20, 0, 0, 20, $white, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);

Categories