PHP image cannot be displayed - php

I'm trying to create a dynamic image in php with some text in it but it says the image cannot be displayed because it contains errors. Here's my code
<html>
<head>
</head>
<body>
<?php
$width = $_POST["width"];
$height = $_POST["height"];
$text = $_POST["text"];
$border = $_POST["border"];
$cornerangle = $_POST["angle"];
putenv('GDFONTPATH=' . realpath('.'));
header ('Content-Type: image/png');
$img = imagecreatetruecolor($width, $height);
$font = 'verdana';
$font_size = 20;
$angle = 45;
$text_box = imagettfbbox($font_size,$angle,$font,$text);
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[3]-$text_box[1];
//coord text
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
$textcolor = imagecolorallocate($img,255,255,255);
imagettftext($img, $font_size, 0, $x, $y, $textcolor, $font, $text);
imagepng($img);
imagedestroy($img);
?>
</body>
</html>
I have the font file (verdana.ttf) in the same folder as my php file. I tried $font = 'verdana.ttf'; and got the same error.

You need to have the font in your web server and call it.
$font = 'path/verdana.ttf';
i tested your code, and work fine in my server.

Related

How can I add HTML code on same capcha file in PHP?

I have created a captcha for my login system. It works and gives me captcha (random) code on the image but if I add HTML code above the captcha code the captcha doesn't work.
<h1> This is my HTML code </h1>
<?php
header("Content-type: image/jpeg");
$imgPath = 'background.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 253, 253, 253);
$string = rand();
$fontSize = 14;
$x = 30;
$y = 15;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
?>
How can I execute successfully HTML code in the same capcha.php file?

Display image from imagettftext

I have code to generate an image:
<?php
ini_set('display_errors', 1);
$members = json_decode(file_get_contents('https://discord.com/api/guilds/828207694284193802/widget.json'), true)['members'];
$membersCount = 0;
foreach ($members as $member) {
if ($member['status'] == 'online') {
$membersCount++;
}
}
$img = imagecreatetruecolor(380, 80);
$white = imagecolorallocate($img, 255,255,255);
putenv('GDFONTPATH=' . realpath('.'));
$font = "arial.ttf";
$txt = "ONLINE: " . $membersCount;
$image_width = imagesx($img);
$image_height = imagesy($img);
$text_box = imagettfbbox(30,0, $font,$txt);
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[7]-$text_box[1];
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
imagettftext($img, 30, 0, $x, $y, $white, $font, $txt);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>
And I want to display a result on my forum:
<img src="http://s52518.web101.svpj.pl/banners/counter_dc.php" alt="" />
But the result is:
Or an empty box:
My PHP code is working well:
I want add the image in HTML to my sidebar/box (I don't know how it's named). It's a Discord online users counter.

PHP error when creating image using imagecreatetruecolor

I got a simple HTML that calls a image generated from a PHP file but the image is not being displayed. The HTML part is:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="imgcaptcha.php"/>
</body>
</html>
imgcaptcha.php is:
<?php
//error_reporting(E_ALL);
//header('Content-Type: image/png');
//Generates rando string
function geraStringAleatoria($length = 10) {
$charSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$stringAleatoria = '';
for ($i = 0; $i < $length; $i++) {
$stringAleatoria .= $charSet[rand(0, strlen($charSet) - 1)];
}
return $stringAleatoria;
}
//Generate the captcha image
function geraImagemCaptcha($text = 'good'){
// Set the content-type
$width = 200;
$height = 30;
// Create the image
$im = imagecreatetruecolor($width, $height) or die('display nao instalado');;
// 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, $width-1, $height-1, $white);
//ADD NOISE - DRAW background squares
$square_count = 6;
for($i = 0; $i < $square_count; $i++){
$cx = rand(0,$width);
$cy = (int)rand(0, $width/2);
$h = $cy + (int)rand(0, $height/5);
$w = $cx + (int)rand($width/3, $width);
imagefilledrectangle($im, $cx, $cy, $w, $h, $white);
}
//ADD NOISE - DRAW ELLIPSES
$ellipse_count = 5;
for ($i = 0; $i < $ellipse_count; $i++) {
$cx = (int)rand(-1*($width/2), $width + ($width/2));
$cy = (int)rand(-1*($height/2), $height + ($height/2));
$h = (int)rand($height/2, 2*$height);
$w = (int)rand($width/2, 2*$width);
imageellipse($im, $cx, $cy, $w, $h, $grey);
}
// Replace path by your own font path
$font = 'flushout.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
}
geraImagemCaptcha(geraStringAleatoria());
?>
I'm using PHP 7
Tried already commenting out header(); call and add error_reporting(E_ALL); but no error message was displayed. Tried also many other different font.ttf files according to this, also didn't worked, so I guess that the problem is in another part of the code rather than the font because I tried many sites and also some other .ttf files I got from other programs installed. The problem is happening in both all browsers I tested. What could be missing in the code? Btw, I got this generate captcha image from here.

Watermarking: Display the same text on multiple parts of the image

I want to add a text to a image. The text should be displayed in multiple areas of the image (not just one).
For example I want to watermark with a text stack. Stack should be displayed in the image at least 8 times in different areas in the image.
I just learned about imagestring() and imagettftext(), but these two only displays my text on a single spot.
Image is not fixed size, so i cannot specify exact and multiple location in advance. It should work on all sizes of images
<?php
/*
image.php
*/
header("Content-type: image/jpeg");
$imgPath = 'olximage.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "stack overflow";
$fontSize = 3;
$x = 15;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
$x = 15;
$y = 175;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
?>
Thanks in advance
For example:
<?php
/*
image.php
*/
header("Content-type: image/jpeg");
$imgPath = 'olximage.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "stack overflow";
$fontSize = 3;
$imageHeight = imagesy($image);
$distanceY = 10;
$maxImageStrings = max(8, $imageHeight / $distanceY);
$x = 15;
for ($i = 0; $i < $maxImageStrings; $i++) {
$y = $i * $distanceY;
imagestring($image, $fontSize, $x, $y, $string, $color);
}
imagejpeg($image);
You can finetune calculations for your needs.
I'm using Imagick extension for same. If you want to go with this then follow detail:
PHP:
// Create objects
$image = new Imagick('image.png');
$watermark = new Imagick();
// Watermark text
$text = 'Copyright';
// Create a new drawing palette
$draw = new ImagickDraw();
$watermark->newImage(140, 80, new ImagickPixel('none'));
// Set font properties
$draw->setFont('Arial');
$draw->setFillColor('grey');
$draw->setFillOpacity(.5);
// Position text at the top left of the watermark
$draw->setGravity(Imagick::GRAVITY_NORTHWEST);
// Draw text on the watermark
$watermark->annotateImage($draw, 10, 10, 0, $text);
// Position text at the bottom right of the watermark
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
// Draw text on the watermark
$watermark->annotateImage($draw, 5, 15, 0, $text);
// Repeatedly overlay watermark on image
for ($w = 0; $w < $image->getImageWidth(); $w += 140) {
for ($h = 0; $h < $image->getImageHeight(); $h += 80) {
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $w, $h);
}
}
// Set output image format
$image->setImageFormat('png');
// Output the new image
header('Content-type: image/png');
echo $image;
although there are plenty of command-line examples to be found on the ImageMagick website, so that is where we shall begin.

imagecreatefrompng makes imported image block-ish?

I'm trying to use the following PHP in order to apply randomly generated text files to an image. (I'm just using a random image right now.)
<?php
header ("Content-type: image/png");
$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}
//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>
However, when I run that code the imported image just becomes very blockish.
Here is the image I am testing with:
http://i.stack.imgur.com/LhNkv.png
And here is how it actually appears:
http://i.stack.imgur.com/AAcHZ.png
My research shows that "imagecreate" generates a "palette" image - and I thought that might have something to do with my error, but I have seen plenty of examples where the base image is by no means distorted.
Thanks in advance for your insights.
(Ugh. It wouldn't let me post images but let me upload them just fine?)
Update
Changing the code to:
<?php
header ("Content-type: image/png");
$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}
//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
//$x = imagesx($im) - $width ;
//$y = imagesy($im) - $height;
//$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
//$textColor = imagecolorallocate ($im, 0, 0,0);
//imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>
Produces the same block-like effects as above, except now no text is being written to the image, either (obviously?).
Could be an alpha blending problem. Try adding these before saving the image:
imagealphablending($im, true);
imagesavealpha($im, true);

Categories