In my project i must extract 3 x variables from JSON, create a small bit of HTML markup, and after that I need to create a PNG file with then (its like a google stars rating). But i don't know how to genarate the PNG file with all 3 x variables. Verr important, the PNG file must be saved in my folder project location.
$url = "https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJqSMdglkgoFMRvvPikAEYJ8w&fields=name,rating,user_ratings_total&key=AIzaSyC16f4wVfi0lJgR1AbqVYkp80o7HAIpKC8";
$json = file_get_contents($url);
$jsonObj = json_decode($json, true);
$name = $jsonObj['result']['name'] . '</br>';
$rating = $jsonObj['result']['rating'] . '</br>';
$total_reviews = $jsonObj['result']['user_ratings_total'];
?>
<span class ='number'><?php echo $rating?></span>
<?php
echo "<span class='stars'>";
for ( $i = 1; $i <= 5; $i++ ) {
if ( round( $rating - .25 ) >= $i ) {
echo "<i class='fa fa-star'></i>";
} elseif ( round( $rating + .25 ) >= $i ) {
echo "<i class='fa fa-star-half-o'></i>";
} else {
echo "<i class='fa fa-star-o'></i>";
}
}
echo '</span>';?>
</br>
<span class ='text' align = 'center'><?php echo $total_reviews?> Total Reviews</span>
Step 1: Create an HTML file with an img in it.
Step 2: Set the src attribute of the img to a php file. For example myimage.php
Step 3: Create the myimage.php file. And fill it with something like this:
<?php
// Set the content-type, so that the browser knows that the php file represents an image
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// 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
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text. This line is most important to you i think. 10 is the x position on the image. The second 20 is the y. See: https://www.php.net/manual/en/function.imagettftext.php
imagettftext($im, 20, 0, 10, 20, $black, $font, $yourJsonVariableHere);
// Using output the image and clear memory.
imagepng($im);
imagedestroy($im);
?>
You can add multiple imagettftext() calls to display texts on other positions. Hopefully this gets you started! To save the image. You can save the image by passing a name as the second parameter of imagepng. It will then save the image at the same spot as where the script is.
edit: To load your star images, you can first use the imagecreatefrompng() method to load your star image, and then a call to the imageCopyMerge() function to copy a star onto you resulting image ($im). I know this is not a complete solution, but this must get you closer to what you want.
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 am trying to combine two pieces of code in one. The first part convert the given .jpg image to .png and put it to IMG folder with the same name just different extension.
The second part of the code should print "text" over the picture while taking the picture from the folder IMG (the .png image created by the FIRST PART).
I am doing something wrong obviously as what I get printed in the end is just the source of the picture instead of using the < img src= part in the end.
I have tried to search for some identical issues here, but with no luck.
//FIRST PART
$file = basename($picture, ".jpg");
$file_create = "img/$file.png";
$image = ImageCreateFromJPEG("$picture");
ImageJpeg($image, "img/$file.png");
ImageDestroy($image);
//SECOND PART
header("Content-type: image/png");
$imgPath = "$file_create";
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "test";
$fontSize = 3;
$x = 115;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
$final=imagejpeg($image);
print "<img src='img/$final' width=150 height=150 valign=middle title='$item' alt='$item'>";
Any ideas?
Here is a good tutorial for same
http://www.phpforkids.com/php/php-gd-library-adding-text-writing.php
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 doing my project in CodeIgniter. I am trying to call captcha.php file in index file.This captcha file in folder files. Currently, the captcha image is not viewing. I need to display random numbers in image.
captcha.php
<?php
session_start();
$string = '';
for ($i = 0; $i < 5; $i++) {
$string .= chr(rand(97, 122));
}
$_SESSION['random_number'] = $string;
$dir = 'fonts/';
$image = imagecreatetruecolor(165, 50);
// random number 1 or 2
$num = rand(1,2);
if($num==1)
{
$font = "PT_SANS-BOLD_1.TTF"; // font style
}
else
{
$font = "PT_SANS-ITALIC_1.TTF";// font style
}
// random number 1 or 2
$num2 = rand(1,2);
if($num2==1)
{
$color = imagecolorallocate($image, 113, 193, 217);// color
}
else
{
$color = imagecolorallocate($image, 163, 197, 82);// color
}
$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font, $_SESSION['random_number']);
header("Content-type: image/png");
imagepng($image);
?>
in html file
<img src="<?= base_url();?>files/captcha.php" alt="" id="captcha" />
This is not an answer to your question but alternative you could opt for.
So many captcha scripts are already available may be you can use some of that instead of coding it again.
Check this
If you do go for this option then do make sure that fonts are placed in right places.
i used captcha coding in my form.the following is the coding of captcha creation
<?php
session_start();
$string = '';
for ($i = 0; $i < 5; $i++)
{
$string .= chr(rand(97, 122));
}
$_SESSION['rand_code'] = $string;
$dir = 'fonts/';
$image = imagecreatetruecolor(150,60) or die('Cannot Initialize new image ');
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 200, 100, 90);
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,50,150,100,$black);
imagettftext ($image, 30, 0, 10, 40, $color, $dir."BauhausMedium.ttf", $_SESSION['rand_code']);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
?>
In other page i call this code using img tag.
but the problem is, In that page i want captcha image in textbox.i used $_session['rand_code']; but i displays previous session value that is previous captcha image value.
i want current image value in that page?
You are accessing the captcha code after you load the form, because the image in the form actually triggers the captcha code.
You should only check the captcha after the form is submitted.
One way to go is to create the random code and save it in the session at the page that will have the img tag. And at the page that the image is created just read the session.