why this show only the background image, and not the text?
Without backgroundimage it's work...
$code = rand(1000, 99999);
$_SESSION["code"] = $code;
$font = '../fonts/captcha.ttf';
$y = 30;
$x = 10;
$width = 100; //Image width in pixels
$height = 40; //Calculating image height
$image = ImageCreate($width, $height);
$img = imagecreatefromgif('img/captcha.gif');
$bgtext = imagecolorallocate($img, 145,145,145);
ImageTTFText($image, 16, 0, $y, $y, $bgtext, $font, $code);
header('Content-type: image/png');
imagegif($img);
imagedestroy($img);
Related
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.
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.
I use the following code to create an EAN13 barcode. The barcode is created without any issues, but when I scan it it is not recognized.
What is the problem?
include('php-barcode.php');
$font = 'fonts/GSMT.TTF';
$fontSize = 16; // GD1 in px ; GD2 in point
$marge = 10; // between barcode and hri in pixel
$x = 80; // barcode center
$y = 80; // barcode center
$height = 50; // barcode height in 1D ; module size in 2
$width = 2; // barcode height in 1D ; not use in 2D
$angle = 0;
$code = "111110001001"; // barcode, of course ;)
$type = "ean13"; //
$im = imagecreatetruecolor(180, 180);
$black = ImageColorAllocate($im,0x00,0x00,0x00);
$white = ImageColorAllocate($im,0xff,0xff,0xff);
imagefilledrectangle($im, 0, 0, 180, 180, $white);
$data = Barcode::gd($im, $black, $x, $y, $angle, $type, array('code'=>$code), $width, $height);
if ( isset($font) ){
$box = imagettfbbox($fontSize, 0, $font, $data['hri']);
$len = $box[2] - $box[0];
Barcode::rotate(-$len / 2, ($data['height'] / 2) + $fontSize + $marge, $angle, $xt, $yt);
imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $black, $font, $data['hri']);
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
Im using Ajax Crop from enter link description here and I'd like to add a watermark to the script. The following is my script.
WATERMARK
$image_path = "watermark.png";
function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = 500; $height = 100;
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);
$pos_x = $width - $w_width;
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 100);
imagedestroy($im);
unlink($oldimage_name);
return true;
}
UPLOAD AND RESIZE PART
function resizeThumb($arr){
$date = md5(time());
$arr['temp_uploadfile'] = $arr['img_src'];
$arr['new_uploadfile'] = $arr['uploaddir'].strtolower($date).'.jpg';
asidoImg($arr);
exit;
}
I tried adding
$arr = watermark_image($arr['temp_uploadfile'],
$arr['uploaddir'].strtolower($date).'.jpg');
in place of
$arr['new_uploadfile'] = $arr['uploaddir'].strtolower($date).'.jpg';
but this didnt work.
Could someone help me?
Download the files and test
It turns out the script uses ASIDO and it has a built in WATERMARK TOOL.
In func.php I done the following.
function asidoImg2($arr){
include('asido/class.asido.php');
asido::driver('gd');
$height = $arr['height'];
$width = $arr['width'];
$x = $arr['x'];
$y = $arr['y'];
// process
$i1 = asido::image($arr['temp_uploadfile'], $arr['new_uploadfile']);
// fit and add white frame
if($arr['thumb'] === true){
Asido::Crop($i1, $x, $y, $width, $height);
asido::watermark($i1, 'watermark.png', ASIDO_WATERMARK_MIDDLE_LEFT, ASIDO_WATERMARK_SCALABLE_ENABLED, 1);
}
else{
Asido::Frame($i1, $width, $height, Asido::Color(255, 255, 255));
asido::watermark($i1, 'watermark.png', ASIDO_WATERMARK_MIDDLE_LEFT, ASIDO_WATERMARK_SCALABLE_ENABLED, 1);
}
// always convert to jpg
Asido::convert($i1, 'image/jpg');
$i1->Save(ASIDO_OVERWRITE_ENABLED);
$data = array(
'photo'=> $arr['new_uploadfile']
);
// echo $user_id;
// delete old file
echo $data['photo'];
}
I have successfully resized images using GD library. I resize any image to 350 x 250, the problem is tat some pictures don't look good (stretch) when they are resized as i am resizing them to a fixed size. I have a space of 350 x 250 where resize picture needs to be fit, I don't mind if the pic size is smaller than 350 x 250 as long as it does not stretch. How do i solve this problem?
$save = "$directory/" . $file_name; //This is the new file you saving
$file = "$directory/" . $file_name; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 350;
if ($width > $height) {
$y = 0;
$x = ($width - $height) / 2;
$smallestSide = $height;
} else {
$x = 0;
$y = ($height - $width) / 2;
$smallestSide = $width;
}
$diff = $width / $modwidth;
$modheight = 250;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $save, 100) ;
Try using this function I've written some time ago:
public function resize($img, $width, $height, $stretch = false)
{
$temp = imagecreatetruecolor($width, $height);
imagealphablending($temp, true);
imagesavealpha($temp, true);
$bg = imagecolorallocatealpha($temp, 0, 0, 0, 127); // Background color
imagefill($temp, 0, 0, $bg);
if ($stretch)
{
imagecopyresampled($temp, img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));
}
else
{
if (imagesx($img) <= $width && imagesy($img) <= $height)
{
$fwidth = imagesx($img);
$fheight = imagesy($img);
}
else
{
$wscale = $width / imagesx($img);
$hscale = $height / imagesy($img);
$scale = min($wscale, $hscale);
$fwidth = $scale * imagesx($img);
$fheight = $scale * imagesy($img);
}
imagecopyresampled($temp,
$img,
($width - $fwidth) / 2, ($height - $fheight) / 2,
0, 0,
$fwidth, $fheight,
imagesx($img), imagesy($img)
);
}
return $temp;
}
if you say not to stretch the image, it will calculate a new size making it fit your new size.
use it as:
...
$image = imagecreatefromjpeg($file);
$resized = resize($image, 350, 250, false); // false = don't stretch
imagejpeg($resized, $save, 100);
...
now store $resized on the disk using imagepng() for example.