Original title:
to read and count the occurrence of only limited characters of A,G,C,T present in the input text file, for example only 100 out of 500 and draw a thin verticular rectangle barcode images using gd
It read and counts only first 10 characters and draw a barcode image, instead of specified 100 characters.
<?php
$file="co3.txt";
$handle=fopen($file, 'r');
$A=0;
$G=0;
$C=0;
$T=0;
$img = imagecreate(850,80);
$white = imagecolorallocate($img, 255,255,255);
$green=imagecolorallocate($img, 0, 128, 0);
$black=imagecolorallocate($img, 0, 0, 0);
$red=imagecolorallocate($img, 255, 0, 0);
$blue=imagecolorallocate($img, 0, 0, 255);
$x1=40;
$y1=40;
$x2=43;
$y2=80;
$contents = '';
#while(( ($contents=fread( $handle, 100)) !='')) {
while(( ($contents=fread($handle, 100)) )) {
for ($i=0; $i<=100; $i++)
{
if($contents[$i] == 'A')
{
$A++;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $green);
$x1 = $x1+6;
$x2 = $x2+6;
}
else
if($contents[$i] == 'G')
{
$G++;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $black);
$x1 = $x1+6;
$x2 = $x2+6;
}
else
if($contents[$i] == 'C')
{
$C++;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $blue);
$x1 = $x1+6;
$x2 = $x2+6;
}
else
if($contents[$i] == 'T')
{
$T++;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
$x1 = $x1+6;
$x2 = $x2+6;
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
}
}
}
?>
I'm guessing you only not so much the first 10 letters, but up to the point when a T is found. Then, the misplaced snippet producing the image ouptut this image. The loop may continue up to 100, producing possibly a few more images (with each new T), but I don't know PHP behavior in such cases, since some of the HTTP response is readily written...
Alternatively you may be seeing the last of these images. At any rate, by fixing the misplaced
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
things should start to look more as expected.
Related
PHP code:
<?php
ob_start();
session_start();
$_SESSION['secure'];
header('content-type:image/png');
$text="hello";
$font_size=25;
$width=200;
$height=200;
$image=imagecreate($width, $height);
imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
for($x=1; $x <= 30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black);
}
imagettftext($image, $font_size, 0, 15, 30, $black, 'consolas.ttf', $text);
$imgSrc="out.png";
imagejpeg($image, $imgSrc);
?>
When the image is generated, only lines are drawn but text("hello") is not drawn on it.
The font is also in the same directory, I have also kept it in another directory and added the complete path no luck.
I'm debugging this issue and found that font file is not accessible.
I have checked by turning on debugging as below:
error_reporting(E_ALL);
ini_set("display_errors", 1);
And commented lines below.
header('content-type:image/png');
imagepng($image);
Got error:
Warning: imagettftext(): Could not find/open font in
/var/www/html/index.php on line 24
Soultion:
Path to font file should be realpath on server. So the code should be:
imagettftext($image, $font_size, 0, 15, 30, $black, realpath('consolas.ttf'), $text);
Also change in render image without giving $imgSrc and changing it to
imagepng($image);
instead of
$imgSrc="out.png";
imagejpeg($image, $imgSrc);
Finally complete code will look like:
ob_start();
session_start();
$_SESSION['secure'];
header('content-type:image/png');
$text = "hello";
$font_size = 25;
$width = 200;
$height = 200;
$image = imagecreate($width, $height);
imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
for ($x = 1; $x <= 30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black);
}
imagettftext($image, $font_size, 0, 15, 30, $black, realpath('Consolas.ttf'), $text);
imagepng($image);
I have successfully outputted text (consisting of 2-3 digit numbers) onto an image using the PHP GD imaging library. Next I would like to position this text and center it under labels included in the image. I have it working now by looking at the image and hardcoding position values, but this is not ideal as the numbers vary and may not be the same length each time the code is run.
Here is a simplified version of what I have so far:
<?php
$font = 'arial.ttf';
$fontsize = 60;
$value1 = $_GET['r'];
$value2 = $_GET['rm'];
$value3 = $_GET['w'];
$image = imagecreatefrompng('image.png');
$fontcolor = imagecolorallocate($image, 255, 255, 255);
$x1 = 80;
$x2 = 160;
$x3 = 280;
$y = 1050;
imagettftext($image, $fontsize, 0, $x1, $y, $fontcolor, $font, $value1);
imagettftext($image, $fontsize, 0, $x2, $y, $fontcolor, $font, $value2);
imagettftext($image, $fontsize, 0, $x3, $y, $fontcolor, $font, $value3);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
I believe that I need to use imagettfbbox, but how do I position the box according to the label in the image?
Is it possible to set a primary position for each label (as they will never move) and center according to that no matter the length of the number? For example, if a 4 digit number was entered it would appear in the same place as a 2 digit number centered under its label.
Box sizing was giving me strange results, so I solved this by creating a function that calculates the number of digits in each number and sends a position variable back.
function position($value, $pos) {
$length = strlen((string)$value);
return $pos - (20 * $length);
}
$value1 = $_GET['r'];
$value2 = $_GET['rm'];
$value3 = $_GET['w'];
$x1 = position($value1, 110);
$x2 = position($value2, 310);
$x3 = position($value3, 545);
$y = 1050;
imagettftext($image, $fontsize, 0, $x1, $y, $fontcolor, $font, $value1);
imagettftext($image, $fontsize, 0, $x2, $y, $fontcolor, $font, $value2);
imagettftext($image, $fontsize, 0, $x3, $y, $fontcolor, $font, $value3);
I am trying to read an image from a location in my server and then draw lines on it and then overwrite the image in the location.
My code is as follows:
function drawShapes($src_path, $json)
{
echo "---inside draw Sharpes-------";
$x1= $json['x1'];
$y1= $json['y1'];
$x2= $json['x2'];
$y2= $json['y2'];
$x3= $json['x3'];
$y3= $json['y3'];
$x4= $json['x4'];
$y4= $json['y4'];
$type = exif_imagetype($src_path);
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3, // [] png
);
if (!in_array($type, $allowedTypes)) {
return false;
}
switch ($type) {
case 1 :
$im = imageCreateFromGif($src_path);
break;
case 2 :
$im = imageCreateFromJpeg($src_path);
break;
case 3 :
$im = imageCreateFromPng($src_path);
break;
}
if (!$im)
return false;
imagesetthickness($im, 5);
$color = imagecolorallocate($im, 255, 255, 255);
echo $color;
imageline($im, $x1, $y1, $x2, $y2, $color);
imageline($im, $x2, $y2, $x3, $y3, $color);
imageline($im, $x3, $y3, $x4, $y4, $color);
imageline($im, $x4, $y4, $x1, $y1, $color);
header("Content-type: image/jpeg");
imagejpeg($im,$src_path);
imagedestroy($im);
}
Here $src_path= "uploads/case.jpg"- uploads is a folder inside my solution & case.jpg is the image file name.
But I am getting image missing icon as output. What is the mistake I do?
What is the solution to it? Thank you.
You are echoing data before you send the headers.
If you want to overwrite the image in the location and then display it in the browser, maybe you can work with this adjustment:
<?php
function drawShapes($src_path, $json)
{
//echo "---inside draw Sharpes-------";
$x1= $json['x1'];
$y1= $json['y1'];
$x2= $json['x2'];
$y2= $json['y2'];
$x3= $json['x3'];
$y3= $json['y3'];
$x4= $json['x4'];
$y4= $json['y4'];
$type = exif_imagetype($src_path);
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3, // [] png
);
if (!in_array($type, $allowedTypes)) {
return false;
}
switch ($type) {
case 1 :
$im = imageCreateFromGif($src_path);
break;
case 2 :
$im = imageCreateFromJpeg($src_path);
break;
case 3 :
$im = imageCreateFromPng($src_path);
break;
}
if (!$im)
return false;
imagesetthickness($im, 5);
$color = imagecolorallocate($im, 255, 255, 255);
//echo $color;
imageline($im, $x1, $y1, $x2, $y2, $color);
imageline($im, $x2, $y2, $x3, $y3, $color);
imageline($im, $x3, $y3, $x4, $y4, $color);
imageline($im, $x4, $y4, $x1, $y1, $color);
imagejpeg($im,$src_path);
imagedestroy($im);
$fp = fopen($src_path, 'rb');
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($src_path));
fpassthru($fp);
exit;
}
Here is an example what I would like to do:
Here is the result:
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr = 50)
{
// Load image
$img = imagecreatefromjpeg($img_src);
// Transparent red
$red = imagecolorallocatealpha($img, 255, 0, 0, $tr);
// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
// Save the image (overwrite)
imagejpeg($img, $img_src);
imagedestroy($img);
}
You need to use http://php.net/manual/en/function.imagefilledrectangle.php, passing a color created with http://www.php.net/manual/en/function.imagecolorallocatealpha.php.
As you can see, the example for http://php.net/manual/en/function.imagefilledrectangle.php is pratically what to you want to do.
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr = 100)
{
// Load image
$img = imagecreatefromjpeg($img_src);
// Transparent red
$red = imagecolorallocatealpha($img, 255, 0, 0, $tr);
// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
// Don't forget to output a correct header
header('Content-Type: image/jpg');
// Save the image (overwrite)
imagejpeg($img);
imagedestroy($img);
}
$img_src = 'test.jpg';
$x1= 500;
$y1= 450;
$x2 = 370;
$y2=180;
red_rectangle($img_src,$x1,$y1,$x2,$y2);
I'm using imagettftext to make a bar graph and at the top of each bar I want to put the value.
I have the following variables for each bar (which are really rectangles)
$x1
$y1
$x2
$y2
$imagesx
$imagesy
$font_size
Also, The fontsize should decrease as the string length increases.
Do it like this. Remember to place the font file "arial.ttf" in current directory:
<?php
// Create a 650x150 image and create two colors
$im = imagecreatetruecolor(650, 150);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// Set the background to be white
imagefilledrectangle($im, 0, 0, 649, 149, $white);
// Path to our font file
$font = './arial.ttf';
//test it out
for($i=2;$i<10;$i++)
WriteTextForMe($im, $font, str_repeat($i, $i), -140 + ($i*80), 70 + rand(-30, 30), -160 + (($i+1)*80), 150, $black);
//this function does the magic
function WriteTextForMe($im, $font, $text, $x1, $y1, $x2, $y2, $allocatedcolor)
{
//draw bars
imagesetthickness($im, 2);
imagerectangle($im, $x1, $y1, $x2, $y2, imagecolorallocate($im, 100,100,100));
//draw text with dynamic stretching
$maxwidth = $x2 - $x1;
for($size = 1; true; $size+=1)
{
$bbox = imagettfbbox($size, 0, $font, $text);
$width = $bbox[2] - $bbox[0];
if($width - $maxwidth > 0)
{
$drawsize = $size - 1;
$drawX = $x1 + $lastdifference / 2;
break;
}
$lastdifference = $maxwidth - $width;
}
$size--;
imagettftext($im, $drawsize, 0, $drawX, $y1 - 2, $allocatedcolor, $font, $text);
}
// Output to browser
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
It uses imagettfbbox function to get width of the text, then loops over the font size to get correct size, centers it and displays it.
So, it outputs the following:
You can calculate the center of your text by using PHP's imagettfbbox-function:
// define text and font
$text = 'some bar label';
$font = 'path/to/some/font.ttf';
// calculate text size (this needs to be adjusted to suit your needs)
$size = 10 / (strlen($text) * 0.1);
// calculate bar center
$barCenter = $x1 + ($x2 - $x1) / 2;
// calculate text position (centered)
$bbox = imagettfbbox($size, 0, $font, $text);
$textWidth = $bbox[2] - $bbox[0];
$positionX = $textWidth / 2 + $barCenter;
$positionY = $y1 - $size;
EDIT: Updated the code to do all the work for you.