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 )."'>";
Related
I want to create/generate an image with PHP, adding text into it, but without saving it into the FTP, I'd like to load it on the site by changing the content type to image/png, the same way I did it with ASP.NET:
Response.ContentType = "image/png";
rImage.Save(Response.OutputStream, ImageFormat.Png);
I found a function called file_put_contents, but I'm not so sure this is what I'm looking for. If you know how to do something like this, please tell me.
I'm trying this code, but it fails to load the image, and shows the browser's default error image.
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
Below example will regenerate an image with text over it:
<?php
header("Content-type: image/png"); // Set the header so that browser can output the image
$string = 'Print this text on Image'; // Text to be added on the image
$im = imagecreatefrompng("images/button1.png"); // Take base image
$orange = imagecolorallocate($im, 220, 210, 60); // Allocate a color for an image
imagestring($im, 3, 0, 0, $string, $orange); // Write the string at the top left
imagepng($im); // Create new image
imagedestroy($im); // destroy object
?>
Reference: http://php.net/manual/en/image.examples-png.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’m having problems with gd library in PHP when I try to do a imagecratefrompng. I’m running a script where the user input a text and it is added to a pre-created image. Problem is, when I output the image the image show as broken.
Can anyone help pointing if something is wrong with my script/image?
The image is a PNG, 600x956, 220kb file size.
GD Library is enabled. PNG, JPEG, GIF support are enabled.
Here is the code.
// Text inputed by user
$text = $_POST['text'];
// Postion of text inputed by the user
$text_x = 50;
$text_y = 817;
// Color of the text
$text_color = imagecolorallocate($img, 0, 0, 0);
// Name of the file (That is in the same directory of the PHP file)
$nomeDaImagem = "Example";
$img = imagecreatefrompng($nomeDaImagem);
//Text is retrieved by Post method
imagestring($img, 3, $text_x, $text_y, $text, $text_color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
There are a number of issues with your script:
You attempt to allocate the colour to the image, before you have actually created the image.
Your string to be written is in the variable $nome, but you are printing $text.
You don't check if $_POST['text'] exists, which may result in a Notice-level error.
You don't check if the file exists, which may result in a Warning-level error.
Here's an example of your code, fixed:
// Text inputed by user
$nome = isset($_POST['text']) ? $_POST['text'] : "<Nothing to write>";
// Postion of text inputed by the user
$text_x = 50;
$text_y = 817;
// Name of the file (That is in the same directory of the PHP file)
$nomeDaImagem = "Example";
$img = file_exists($nomeDaImagem)
? imagecreatefrompng($nomeDaImagem)
: imagecreate(imagefontwidth(3)*strlen($nome)+$text_x,imagefontheight(3)+$text_y);
// Color of the text
$text_color = imagecolorallocate($img, 0, 0, 0);
//Text is retrieved by Post method
imagestring($img, 3, $text_x, $text_y, $nome, $text_color);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
Read more:--
http://php.net/manual/en/function.imagecreatefrompng.php
http://www.php.net/manual/en/function.imagecreatefromstring.php
or try this
<?php
function LoadPNG($imgname)
{
/* Attempt to open */
$im = #imagecreatefrompng($imgname);
/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG('bogus.image');
imagepng($img);
imagedestroy($img);
?>
I'm using Creating image function to create an image with exact text message using the following code
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
The output should be like this
Now my question is there any way i can write image over it not only text
so that if i've flag image at same path (flag.gif ) and i would like to write it just after my $message to be like this
so is this possible and how could be ! ~ thanks a lot
Update
based on #MarcB idea of using imagecopy function
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
the output is not true color WHY :(
ANY help about this new problem ~ thanks
With the help of Surreal Dreams and Marc B
This one works fine
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromjpeg("myimage.jpg"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
Output
I've should have learned the following functions
imagecopy
imagecreatefromjpeg
imagecreatefromgif
imagecopy() should properly deal with differences between images' palettes; however, the GIF format does not support more than 256 colors, and neither does GD when it works with palette-based images. If 256 palette entries already exist when GD tries to use a new color, GD will pick the closest match, which can produce the results you see.
To avoid this problem, you should use imagecreatetruecolor() to create a 24-bit true-color image in memory. You can then use imagecopy() to insert each GIF image (including the background) and imagepng() to generate PNG output, which is better for line art than JPEG, offers better compression than GIF, and can support more than 256 colors.
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!