I'm trying to biuld an eapplication for Facebook where I merge the users profile image with a background image and the name of the user. This image is a reward for those who succesfully answer a quiz.
This is the relavant code from the page where the user is direceted after submitting there answers:
if ( $totalCorrect == 10) {
echo "Congrats!";
echo "<div id='results'>$totalCorrect / 10 correct</div>";
echo '<img src="image.php">';
?>
And this is the code in image.php
<?php
// Create image instances
$im = imagecreatefromjpeg('xxx.jpg');
$black = ImageColorAllocate($im, 0, 0, 0);
//The canvas's (0,0) position is the upper left corner
//So this is how far down and to the right the text should start
$start_x = 200;
$start_y = 20;
$font = 'arial.ttf';
Imagettftext($im, 12, 0, $start_x, $start_y, $black, $font, 'text to write');
$url = "http://graph.facebook.com/".$user_profile. "/picture";
$dest = imagecreatefromjpeg($url);
// Copy and merge
imagecopymerge($im, $dest, 10, 10, 0, 0, 180, 180, 100);
//Creates the jpeg image and sends it to the browser
//100 is the jpeg quality percentage
// Output and free from memory
header('Content-Type: image/jpg');
imagejpeg($im);
imagedestroy($dest);
imagedestroy($src);
?>
If I exchange the $url to a static image the code works, but not whene i try to fetch the profile of the user. Any suggestions? Thank's in adwance!
create new php file name a.php and store following
$headers = get_headers('https://graph.facebook.com/".$user[id]."/picture',1);
$url = $headers['Location'];
on the original file, call the a.php file like below:
require_once 'a.php';
$dst = imagecreatefromjpeg($url);
imagecopymerge($im, $dest, 10, 10, 0, 0, 180, 180, 100);
hope it helps!
Related
Good day. Why is that when I try to run my code on an external page, it works perfectly. But when I used wordpress to add it on my page, it gives me strange errors. Why is that and how do I fix that?
code:
<?php
// (A) OPEN IMAGE
$img = imagecreatefromjpeg('https://images.unsplash.com/photo-1550684376-efcbd6e3f031?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80');
// (B) WRITE TEXT
$white = imagecolorallocate($img, 255, 255, 255);
$txt = "sad";
$font = realpath('arial.ttf');
//(IMAGE, FONT SIZE, TILT ANGLE, X, Y, COLOR, FONT, TEXT)
imagettftext($img, 12, 0, 253, 234, $white, $font, $txt);
// (C) OUTPUT IMAGE
header('Content-Type: image/jpeg');
imagejpeg($img);
// OR SAVE TO A FILE
// THE LAST PARAMETER IS THE QUALITY FROM 0 to 100
imagejpeg($img, "test.jpg", 100);
?>
Here's the result I got when trying it on my wordpress page:
Image here
The issue is that you are running this in a shortcode and change the header of the output, that you already created, namely the page. It depends on what you want to do, if you want to display the image in the site:
ob_start();
imagejpeg( $img, NULL, 100 );
imagedestroy( $img );
$i = ob_get_clean();
echo "<img src='data:image/jpeg;base64," . base64_encode( $i )."'>";
I got this code to work but after few weeks the code is not working ,i did try to see where the bug come from but i have no idea.
I did create image with GD library and it is created successfully but when i want to merge it with another picture it show only black screen.Any idea of how to solve the issue please .Here is my code:
Please note that the image is created and saved successfully,so there is no problem with create_image() function.
function create_image()
{
$cer_id = get_the_ID();
$cer_org_nummer = get_field('orgnummer_dqm');
$cer_today = date('Y-m-d');
$cer_org_name = get_field('foretags_namn_dqm');
$cer_org_date = $cer_org_nummer . ' | ' . $cer_today;
$im = #imagecreate(500, 450);
$yellow = imagecolorallocate($im, 255, 255, 255); // yellow
imagecolortransparent($im, $yellow);
$black = imagecolorallocate($im, 252, 195, 91);
$string = "Digital Quality Managment";
$font = FUNC_PLUGIN_D_DQM . '/font/Roboto-Regular.ttf';// black
/* imagestring($im, $font, 50, 300, $string, $black);*/
/* imagestring($im, $font, 3, 300, $cer_org_name, $black);*/
/* imagestring($im, $font, 0, 10, $cer_org_date, $black);*/
imagettftext($im, 35, 0, 60, 270, $black, $font, 'CERTIFIED');
imagettftext($im, 25, 0, 0, 320, $black, $font, $cer_org_date);
imagepng($im, "wp-content/plugins/certificates/cer_images/image_$cer_id.png");
imagedestroy($im);
}
create_image();
$image1 = "wp-content/plugins/certificates/cer_images/dqm_png_logo.png";
$image2 = "wp-content/plugins/certificates/cer_images/image_$cer_id.png";
list($width, $height) = getimagesize($image2);
$image1 = imagecreatefromstring(file_get_contents($image1));
$image2 = imagecreatefromstring(file_get_contents($image2));
imagealphablending($image1, FALSE);
imagesavealpha($image1, TRUE);
imagecopymerge($image1, $image2, 110, 50, 0, 0, $width, $height, 100);
header("Content-type: image/png");
imagepng($image1);
imagedestroy($image1);
please try the code below
This code works for me..
$pro_directory_path = get_attached_file( get_post_thumbnail_id($_REQUEST['apid']),'full' );
$mainmetadata = wp_get_attachment_metadata( get_post_thumbnail_id( $_REQUEST['apid'] ) );
$pro_img_name = $mainmetadata['file'];
$pro_img_name_extra = '1'.$mainmetadata['file'];
// $val_id is id of image(bigger image/background Image)
$directory_path = get_attached_file( $val_id,'full' );
$metadata = wp_get_attachment_metadata( $val_id );
$name = $metadata['file'];
$bgFile = $directory_path;
// We want our final image to be 76x76 size
//user $metadata to get ordiginal height width of image
// $value contain height width of wall mentioned by admin
$x = $metadata['width'];
$y = $metadata['height'];
$x_inch = $metadata['width']/96;
$y_inch = $metadata['height']/96;
$width_proption = $wall_w_inch/$x_inch;
$height_proption = $wall_h_inch/$y_inch;
$img_pos_x = ($x/2)-($resizedwidth/2);
$img_pos_y = ($y/2)-($resizedheight);
/* Code to generate custom wall images - STart */
$inn_width = ($innerimg_width / $width_proption) * 12 ;
$inn_height = ($innerimg_height / $height_proption) * 12 ;
$resizedFilename = get_template_directory().'/woocommerce/temp_images/cust_'.$pro_img_name;
$img_url_extra = get_stylesheet_directory_uri().'/woocommerce/temp_images/cust_'.$pro_img_name;
//function to resize Artist product image in desired size's (Variation sizes).
$imgData = resize_image($pro_directory_path, $inn_width, $inn_height);
imagepng($imgData, $resizedFilename);
list($resizedwidth, $resizedheight) = getimagesize($resizedFilename);
/* Code to generate custom wall images - ENDS */
$imageFile = $resizedFilename;
// dimensions of the final image
$final_img = imagecreatetruecolor($x, $y);
// Create our image resources from the files
$image_1 = imagecreatefrompng($bgFile);
$image_2 = imagecreatefrompng($imageFile);
// Enable blend mode and save full alpha channel
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
// Copy our image onto our $final_img
imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
imagecopy($final_img, $image_2, $img_pos_x, $img_pos_y, 0, 0, $resizedwidth, $resizedheight);
ob_start();
$path = __DIR__ .'/custom_wall_img/'.$name;
imagepng($final_img,$path);
$watermarkedImg = ob_get_contents(); // Capture the output
ob_end_clean(); // Clear the output buffer
I'm on my way to create a new script for my personal website. I want some kind of function where visitors can write something in an input field and then press generate image.
The script should then take a background image and insert the visitors text on top.
My question is: What is the php code to actually generate the image file and how do i connect the input data to the image?
I do not need full code examples if you don't have the time, i just need the basis practice of how to setup such a system.
Try this code:
$filename = "layout.jpg";
$im = #imagecreatefromjpeg($filename);
$font = "tahoma.ttf";
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagettftext($im, 15, 0, 50, 50, $black, $font, $_POST['message_from_user']);
header('Content-Type: image/jpeg');
imagejpeg($im, null, 100);
imagedestroy($im);
layout.jpg is background image, tahoma.ttf is font file
Both files should be placed in the same folder.
This code would generate jpg-image with user string.
More details you would get by searching "gd in php".
do something like this:
$text = 'put ur txt here';
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, 'file name here', 80);
I have been ripping my hair out with this and have tried many many solutions on here to no avail.
I am trying to add some text to an image, but all it is doing is showing my background image, is there anything glaringly obvious that I'm doing wrong here?
Thanks in adcance
<?
header('Content-Type: image/jpeg');
$fbid = $_POST['fbid'];
$background_img = $_POST['background'];
$message = $_POST['text'];
$ts = $_POST['ts'];
$filename = $fbid . "-" . $ts . ".jpg";
$image_canvas = imagecreatetruecolor(640,400);
$background = imagecreatefromjpeg($background_img);
$overlay = imagecreatefrompng("../images/image-overlay.png");
imagecopyresampled($background, $overlay, 0, 0, 0, 0, imagesx($overlay), imagesy($overlay), imagesx($overlay), imagesy($overlay));
imagefilledrectangle($image_canvas, 0,0,150,30, $background);
$white = imagecolorallocate($background, 255, 255, 255);
imagettftext($image_canvas, 25, 0, 50, 50, $white, "arial.TTF", $message);
imagejpeg($background,"../created/" . $filename, 100);
imagedestroy($background);
You're missing the canvas. Start the build with imageCreateTrueColor.
$imageCanvas = imageCreateTrueColor($width, $height);
//your code
$background = imagecreatefromjpeg($background_img);
//more of your code
imagefilledrectangle($imageCanvas, 0, 0, 150, 30, $background);
//now do the same for the text only us imag
imagettftext($imageCanvas, 25, 0, 50, 50, $white, "arial.TTF", $message);
Your merging the jpeg and the text elements on the $imageCanvas.
Have a look at this. It works, and the link to the page is below. Based on your original post, I believe this is what your looking for.
/* first composite the canvas with the background */
$background_img="../img/adfuba_october.png";
$compositeString = "composite.png";
list($width,$height) = getimagesize($background_img);
$image_canvas = imagecreatetruecolor($width,$height);
$background = imagecreatefrompng($background_img);
imagecopyresampled($image_canvas,$background,0,0,0,0,$width,$height,$width,$height);
/* now add the text */
$fontPath = "path/to/your/fontFile/ARIAL.TTF";
$fontSize = 24;
$percent = 0.25;
$txt_x = abs($width*$percent);
$txt_y = abs($height*$percent);
$color = "008844";
$message = "This is User Text";
imageTTFtext($image_canvas, $fontSize, 0, $txt_y, $txt_y, $color, $fontPath, $message);
/* now generate the file */
imagepng($image_canvas, $compositeString, 0) or die("error saving png");
?>
<p>This is a composite image:<br><img src="<?php echo $compositeString;?>"></p>
You can see the composite image here.
Couple things to keep in mind. The path to your TrueType font face should be absolute even if the TrueType font file is located in the same directory as your script.
Also, the canvas is your background object, then you layer images or text over top the canvas.
Finally, (and you may have figured this out) your layered elements are order dependent form canvas to text. Meaning canvas -> background -> another graphic -> then the text. Otherwise, you could end up covering up an element you meant to render in front. Hope that helps.
I am overlay some text on an image (that is already on my server) using GD. I am trying to save the new image on the server.
Below is my code but all it does is show the new image to the browser it doesnt save it to the server.
<?
//PHP's GD class functions can create a variety of output image
//types, this example creates a jpeg
header("Content-Type: image/jpeg");
$im = imagecreatefromjpeg($image_full);
$black = ImageColorAllocate($im, 255, 255, 255);
$copy_1_x = 10;
$copy_1_y = 20;
$copy_2_x = 10;
$copy_2_y = 20;
$copy_3_x = 10;
$copy_3_y = 20;
//writes text to image
Imagettftext($im, 12, 0, $copy_1_x, $copy_1_y, $black, 'verdana.ttf', $main_number);
Imagettftext($im, 12, 0, $copy_2_x, $copy_2_y, $black, 'verdana.ttf', $prime_number);
Imagettftext($im, 12, 0, $copy_3_x, $copy_3_y, $black, 'verdana.ttf', $legal);
//Creates the jpeg image and sends it to the browser
//100 is the jpeg quality percentage
Imagejpeg($im, '', 100);
ImageDestroy($im);
?>
Thanks #Ignacio
Imagejpeg($im, '/path_to_folder/imagename.jpg', 100);
Found at http://php.net/manual/en/function.imagejpeg.php