line break problem with MultiCell in FPDF - php

I am using java port of fpdf. I am encountering fowwlowing errors.
1).When i call multicell 2 times every time the text is printed on a new line.
MultiCell(0, 1, "abcd", currentBorders, Alignment.LEFT, false); //prints on one line
MultiCell(0, 1, "efg", currentBorders, Alignment.LEFT, false); //prints on next line
I want that there is no line break after the call to multicell. How can i do it?
2)If i do the following thing then some part of my string gets printed on one line and some on next.
MultiCell(getStringWidth(myString), 1, myStringcurrentBorders, Alignment.LEFT, false);
3)If i do the following thing then there are many blank lines after the line on which myString is printed. It works correctly if i use one 1 ans second parameter
MultiCell(0, myFontSize, "123456", currentBorders, Alignment.LEFT, false);
What is the problem?

I would get the current Y position before writing the MultiCell and then move the "cursor" back to that Y position after the MultiCell generation. Like this:
$current_y = $pdf->GetY();
$current_x = $pdf->GetX();
$cell_width = 50;
MultiCell($cell_width, 1, "abcd", currentBorders, Alignment.LEFT, false);
$pdf->SetXY($current_x + $cell_width, $current_y);
$current_x = $pdf->GetX();
MultiCell($cell_width, 1, "abcd", currentBorders, Alignment.LEFT, false);
Something like that.

I created a new method called MultiAlignCell. It takes the same parameters as MultiCell but with the added ln field from Cell. You can add it to your extended FPDF class.
/**
* MultiCell with alignment as in Cell.
* #param float $w
* #param float $h
* #param string $text
* #param mixed $border
* #param int $ln
* #param string $align
* #param boolean $fill
*/
private function MultiAlignCell($w,$h,$text,$border=0,$ln=0,$align='L',$fill=false)
{
// Store reset values for (x,y) positions
$x = $this->GetX() + $w;
$y = $this->GetY();
// Make a call to FPDF's MultiCell
$this->MultiCell($w,$h,$text,$border,$align,$fill);
// Reset the line position to the right, like in Cell
if( $ln==0 )
{
$this->SetXY($x,$y);
}
}

I have modified the MultiCell method, it works as the above answer, and you can use the method in the same way as the Cell method.
function MultiCell($w, $h, $txt, $border=0, $ln=0, $align='J', $fill=false)
{
// Custom Tomaz Ahlin
if($ln == 0) {
$current_y = $this->GetY();
$current_x = $this->GetX();
}
// Output text with automatic or explicit line breaks
$cw = &$this->CurrentFont['cw'];
if($w==0)
$w = $this->w-$this->rMargin-$this->x;
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
$s = str_replace("\r",'',$txt);
$nb = strlen($s);
if($nb>0 && $s[$nb-1]=="\n")
$nb--;
$b = 0;
if($border)
{
if($border==1)
{
$border = 'LTRB';
$b = 'LRT';
$b2 = 'LR';
}
else
{
$b2 = '';
if(strpos($border,'L')!==false)
$b2 .= 'L';
if(strpos($border,'R')!==false)
$b2 .= 'R';
$b = (strpos($border,'T')!==false) ? $b2.'T' : $b2;
}
}
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$ns = 0;
$nl = 1;
while($i<$nb)
{
// Get next character
$c = $s[$i];
if($c=="\n")
{
// Explicit line break
if($this->ws>0)
{
$this->ws = 0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$i++;
$sep = -1;
$j = $i;
$l = 0;
$ns = 0;
$nl++;
if($border && $nl==2)
$b = $b2;
continue;
}
if($c==' ')
{
$sep = $i;
$ls = $l;
$ns++;
}
$l += $cw[$c];
if($l>$wmax)
{
// Automatic line break
if($sep==-1)
{
if($i==$j)
$i++;
if($this->ws>0)
{
$this->ws = 0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
}
else
{
if($align=='J')
{
$this->ws = ($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
$this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
}
$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
$i = $sep+1;
}
$sep = -1;
$j = $i;
$l = 0;
$ns = 0;
$nl++;
if($border && $nl==2)
$b = $b2;
}
else
$i++;
}
// Last chunk
if($this->ws>0)
{
$this->ws = 0;
$this->_out('0 Tw');
}
if($border && strpos($border,'B')!==false)
$b .= 'B';
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$this->x = $this->lMargin;
// Custom Tomaz Ahlin
if($ln == 0) {
$this->SetXY($current_x + $w, $current_y);
}
}

in my case I didn't create any method, I just set X and Y and then at the end of the line I reset. It works perfectly too.
$pdf->SetFont('times', 'B', 10);
$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->MultiCell($etiquetas_largura, $etiquetas_altura, $campos[$linha]['B1COD'], 0, 'L', 0, 0, $x, $y, true, 0, false, true, 0);
$y = $y + 5;
$pdf->SetFont('times', '', 10);
$pdf->MultiCell($etiquetas_largura, $etiquetas_altura, $campos[$linha]['B1DESC'], 0, 'L', 0, 0, $x, $y, true, 0, false, true, 0);
// resete x y
$pdf->SetXY($x + $etiquetas_largura, $y - 5);

#Muhammad Abdul Rahim and #tomazahlin have have provided good methods. The problem they only sort out the line break problem in a single cell. They don't match the height of the subject cell with other cells in the same row. Using GetY() gets complicated if you are dealing with dynamic tables. The simplest solution I have found is identifying the column likely to have overflowing text and using it as a reference.
$l=strlen($string_of_reference_cell);
$h = ceil($l/$cell_width*1.5)*preferred_normal_height;//1.5 is a loading for allowance`depending on font size.
$pdf->cell(20,$h,$string,1,0);
$pdf->MultiAlignCell(50,5,$string_of_reference_cell,1,0);// 5 is the preferred normal height
$pdf->Cell(23,$h,$string,1,1);
When the pdf is generated, if the string of MultiAlignCell is longer than the cell width, a line break is generated. The resulting height is twice(5 x 2 = 10). The height of ten is asigned to $h. Hence the other cells take the height of $h as well and the entire row gets a uniform height.

Related

Fpdf - set background color for row

I need set lightgrey background color for row. I use multicell view for my PDF.
My code is:
$countRow = 0;
foreach ($arrPeriod as $key=>$val) {
if($countRow % 2 == 0){
$this->setFillColor(230,230,230);
$this->SetTextColor(0,0,0);
}else{
$this->setFillColor(255,255,255);
$this->SetTextColor(0,0,0);
}
$this->Row([
$val['lead_name'],
$val['content'],
$val['date_due']
]
);
$countRow++;
}
I have problem that not full column has lightgrey background:
My Row function is:
function Row($data)
{
//Calculate the height of the row
$nb = 0;
for ($i = 0; $i < count($data); $i++) {
$nb = max($nb,$this->GetMultiCellHeight($this->widths[$i], $data[$i]));
}
$h = 5 * $nb;
//Issue a page break first if needed
$this->CheckPageBreak($h);
//Draw the cells of the row
for ($i = 0; $i < count($data); $i++) {
$w = $this->widths[$i];
$a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
//Save the current position
$x = $this->GetX();
$y = $this->GetY();
//Draw the border
$this->Rect($x, $y, $w, $h);
//Set font
if ($i == 0 || $i == 2) {
$this->SetFont('Arial', 'B', 10);
} else {
$this->SetFont('Arial', '', 10);
}
//Print the text
$this->MultiCell($w, 4.5, $data[$i], 0, $a, true);
//Put the position to the right of the cell
$this->SetXY($x + $w, $y);
}
//Go to the next line
$this->Ln($h);
}
How can I fix it and fill correct my row?
You already calculate the maximum number of cells/height in a column per row ($nb / $h).
So just draw the background in your Rect() call instead of the MulitCell():
$this->Rect($x, $y, $w, $h, true);
In any case you should check the caluclation, too: You calculate the height by 5 * $nb but the cell height in your MultiCell() call is only 4.5. This will shift of when you have more lines.

How can I optimize this image "edge detection" algorithm?

I have a function that, given an image with a transparent background and an unknown object in it, finds the top, left, right and bottom boundaries of the object. The purpose is so that I can simply draw a box around the boundaries of the object. I'm not trying to detect the actual edges of the object - just the top most, bottom most, etc.
My function works well, but is slow because it scans every single pixel in the image.
My question is: Is there a faster, more efficient way to detected the upper-most, left-most, right-most, and bottom-most non-transparent pixel in an image, using stock PHP/GD functionality?
There's a catch that affects the options: the object in the image may have transparent parts. For example, if it's an image of a non-filled shape.
public static function getObjectBoundaries($image)
{
// this code looks for the first non white/transparent pixel
// from the top, left, right and bottom
$imageInfo = array();
$imageInfo['width'] = imagesx($image);
$imageInfo['height'] = imagesy($image);
$imageInfo['topBoundary'] = $imageInfo['height'];
$imageInfo['bottomBoundary'] = 0;
$imageInfo['leftBoundary'] = $imageInfo['width'];
$imageInfo['rightBoundary'] = 0;
for ($x = 0; $x <= $imageInfo['width'] - 1; $x++) {
for ($y = 0; $y <= $imageInfo['height'] - 1; $y++) {
$pixelColor = imagecolorat($image, $x, $y);
if ($pixelColor != 2130706432) { // if not white/transparent
$imageInfo['topBoundary'] = min($y, $imageInfo['topBoundary']);
$imageInfo['bottomBoundary'] = max($y, $imageInfo['bottomBoundary']);
$imageInfo['leftBoundary'] = min($x, $imageInfo['leftBoundary']);
$imageInfo['rightBoundary'] = max($x, $imageInfo['rightBoundary']);
}
}
}
return $imageInfo;
}
Function calls in PHP are expensive. Calling imagecolorat() per pixel will absolutely ruin performance. Efficient coding in PHP means finding a built-in function that can somehow do the job. The following code makes use of the palette GD functions. At a glance it might not be intuitive but the logic is actually pretty simple: the code keeps copying the image a line of pixels at a time until it notices that it requires more than one colors to represent them.
function getObjectBoundaries2($image) {
$width = imagesx($image);
$height = imagesy($image);
// create a one-pixel high image that uses a PALETTE
$line = imagecreate($width, 1);
for($y = 0; $y < $height; $y++) {
// copy a row of pixels into $line
imagecopy($line, $image, 0, 0, 0, $y, $width, 1);
// count the number of colors in $line
// if it's one, then assume it's the transparent color
$count = imagecolorstotal($line);
if($count > 1) {
// okay, $line has employed more than one color so something's there
// look at the first color in the palette to ensure that our initial
// assumption was correct
$firstColor = imagecolorsforindex($line, 0);
if($firstColor['alpha'] == 127) {
$top = $y;
} else {
// it was not--the first color encountered was opaque
$top = 0;
}
break;
}
}
if(!isset($top)) {
// image is completely empty
return array('width' => $width, 'height' => $height);
}
// do the same thing from the bottom
$line = imagecreate($width, 1);
for($y = $height - 1; $y > $top; $y--) {
imagecopy($line, $image, 0, 0, 0, $y, $width, 1);
$count = imagecolorstotal($line);
if($count > 1) {
$firstColor = imagecolorsforindex($line, 0);
if($firstColor['alpha'] == 127) {
$bottom = $y;
} else {
$bottom = $height - 1;
}
break;
}
}
$nonTransparentHeight = $bottom - $top + 1;
// scan from the left, ignoring top and bottom parts known to be transparent
$line = imagecreate(1, $nonTransparentHeight);
for($x = 0; $x < $width; $x++) {
imagecopy($line, $image, 0, 0, $x, $top, 1, $nonTransparentHeight);
$count = imagecolorstotal($line);
if($count > 1) {
$firstColor = imagecolorsforindex($line, 0);
if($firstColor['alpha'] == 127) {
$left = $x;
} else {
$left = 0;
}
break;
}
}
// scan from the right
$line = imagecreate(1, $nonTransparentHeight);
for($x = $width - 1; $x > $left; $x--) {
imagecopy($line, $image, 0, 0, $x, $top, 1, $nonTransparentHeight);
$count = imagecolorstotal($line);
if($count > 1) {
$firstColor = imagecolorsforindex($line, 0);
if($firstColor['alpha'] == 127) {
$right = $x;
} else {
$right = $width - 1;
}
break;
}
}
return array('width' => $width, 'height' => $height, 'topBoundary' => $top, 'bottomBoundary' => $bottom, 'leftBoundary' => $left, 'rightBoundary' => $right);
}
I think you could test the 4 sides one after an other, stopping as soon as a pixel is found.
For the top boundary (untested code) :
// false so we can test it's value
$bound_top = false;
// The 2 loops have 2 end conditions, if end of row/line, or pixel found
// Loop from top to bottom
for ($y = 0; $y < $img_height && $bound_top === false; $y++) {
// Loop from left to right (right to left would work to)
for ($x = 0; $x < $img_width && $bound_top === false; $x++) {
if (imageColorAt($img, $x, $y) != 2130706432) {
$bound_top = $y;
}
}
}
After the loops, if $bound_top is still false, don't bother checking the other sides, you checked all pixels, the image is empty. If not, just do the same for the other sides.
Not every pixel needs to be examined. The following code checks columns from left to right to get leftBoundary, right to left to get rightBoundary, rows from top to bottom (while excluding pixels we've already checked) to get topBoundary, and similarly for bottomBoundary.
function get_boundary($image)
{
$imageInfo = array();
$imageInfo['width'] = imagesx($image);
$imageInfo['height'] = imagesy($image);
for ($x = 0; $x < $imageInfo['width']; $x++) {
if (!is_box_empty($image, $x, 0, 1, $imageInfo['height'])) {
$imageInfo['leftBoundary'] = $x;
break;
}
}
for ($x = $imageInfo['width']-1; $x >= 0; $x--) {
if (!is_box_empty($image, $x, 0, 1, $imageInfo['height'])) {
$imageInfo['rightBoundary'] = $x;
break;
}
}
for ($y = 0; $y < $imageInfo['height']; $y++) {
if (!is_box_empty($image, $imageInfo['leftBoundary'], $y, $imageInfo['rightBoundary']-$imageInfo['leftBoundary']+1, 1)) {
$imageInfo['topBoundary'] = $y;
break;
}
}
for ($y = $imageInfo['height']-1; $y >= 0; $y--) {
if (!is_box_empty($image, $imageInfo['leftBoundary'], $y, $imageInfo['rightBoundary']-$imageInfo['leftBoundary']+1, 1)) {
$imageInfo['bottomBoundary'] = $y;
break;
}
}
return $imageInfo;
}
function is_box_empty($image, $x, $y, $w, $h)
{
for ($i = $x; $i < $x+$w; $i++) {
for ($j = $y; $j < $y+$h; $j++) {
$pixelColor = imagecolorat($image, $i, $j);
if ($pixelColor != 2130706432) { // if not white/transparent
return false;
}
}
}
return true;
}

Size and position of one image in another via PHP

I have two images(small and big). Big one contains a small one. Like if the small one is a photo and a big one is a page from the photo album.
How do I get coordinates of that small image in the big one using PHP? And also I need to know the size of that image in big one...so just a(x,y) coordinate of any angle and sizes of sides of that presentation of the small image...
(x,y, width, height)
I've already asked the question like that and got a brilliant answer (here) but I've forgot to mention over there that the size of a small image could be different from the the size of that image in the big image...
And also if it is possible to deal with a presentation of that small image in the big image can have something covering one of its angles... Like in this example:
Small image:
Big image:
Small image always has just a rectangular shape.
Alright, this answer does not perfectly answer the question, but it should give you a good start! I know I repeat myself in the code, but my goal was simply to get something working so you can build on it, this isn't production code!
Preconditions
Starting with the large picture:
We need to find as best as possible the position of this other picture:
I decided to break the process into many substeps, which you could improve or remove depending on what you want the code to do.
For testing purposes, I did test my algorithm on different input images so you'll see a variable defining what file to load...
We start with:
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
$largeFilename = "large.jpg";
$small = imagecreatefromjpeg("small.jpg");
$large = imagecreatefromjpeg($largeFilename);
and
imagedestroy($small);
imagedestroy($large);
$time_end = microtime_float();
echo "in " . ($time_end - $time_start) . " seconds\n";
To have a good idea on our performances. Luckily, most of the algorithm was pretty fast so I didn't have to optimize more.
Background Detection
I started by detecting the background color. I assumed that the background color would be the color most present in the picture. To do this, I only counted how many references of each color I could find in the large picture, sort it with decending values and took the first one as the background color (should allow the code to be adaptable if you changed the source pictures)
function FindBackgroundColor($image)
{
// assume that the color that's present the most is the background color
$colorRefcount = array();
$width = imagesx($image);
$height = imagesy($image);
for($x = 0; $x < $width; ++$x)
{
for($y = 0; $y < $height; ++$y)
{
$color = imagecolorat($image, $x, $y);
if(isset($colorRefcount[$color]))
$colorRefcount[$color] = $colorRefcount[$color] + 1;
else
$colorRefcount[$color] = 1;
}
}
arsort($colorRefcount);
reset($colorRefcount);
return key($colorRefcount);
}
$background = FindBackgroundColor($large); // Should be white
Partitionning
My first step was to try to find all the regions where non background pixels were. With a little padding, I was able to group regions into bigger regions (so that a paragraph would be a single region instead of multiple individual letters). I started with a padding of 5 and got good enough results so I stuck with it.
This is broken into multiple function calls, so here we go:
function FindRegions($image, $backgroundColor, $padding)
{
// Find all regions within image where colors are != backgroundColor, including a padding so that adjacent regions are merged together
$width = imagesx($image);
$height = imagesy($image);
$regions = array();
for($x = 0; $x < $width; ++$x)
{
for($y = 0; $y < $height; ++$y)
{
$color = imagecolorat($image, $x, $y);
if($color == $backgroundColor)
{
continue;
}
if(IsInsideRegions($regions, $x, $y))
{
continue;
}
$region = ExpandRegionFrom($image, $x, $y, $backgroundColor, $padding);
array_push($regions, $region);
}
}
return $regions;
}
$regions = FindRegions($large, $background, 5);
Here, we iterate on every pixel of the picture, if its background color, we discard it, otherwise, we check if its position is already present in a region we found, if that's the case, we skip it too. Now, if we didn't skip the pixel, it means that it's a colored pixel that should be part of a region, so we start ExpandRegionFrom this pixel.
The code to check if we're inside a region is pretty simple:
function IsInsideRegions($regions, $x, $y)
{
foreach($regions as $region)
{
if(($region["left"] <= $x && $region["right"] >= $x) &&
($region["bottom"] <= $y && $region["top"] >= $y))
{
return true;
}
}
return false;
}
Now, the expanding code will try to grow the region in each direction and will do so as long as it found new pixels to add to the region:
function ExpandRegionFrom($image, $x, $y, $backgroundColor, $padding)
{
$width = imagesx($image);
$height = imagesy($image);
$left = $x;
$bottom = $y;
$right = $x + 1;
$top = $y + 1;
$expanded = false;
do
{
$expanded = false;
$newLeft = ShouldExpandLeft($image, $backgroundColor, $left, $bottom, $top, $padding);
if($newLeft != $left)
{
$left = $newLeft;
$expanded = true;
}
$newRight = ShouldExpandRight($image, $backgroundColor, $right, $bottom, $top, $width, $padding);
if($newRight != $right)
{
$right = $newRight;
$expanded = true;
}
$newTop = ShouldExpandTop($image, $backgroundColor, $top, $left, $right, $height, $padding);
if($newTop != $top)
{
$top = $newTop;
$expanded = true;
}
$newBottom = ShouldExpandBottom($image, $backgroundColor, $bottom, $left, $right, $padding);
if($newBottom != $bottom)
{
$bottom = $newBottom;
$expanded = true;
}
}
while($expanded == true);
$region = array();
$region["left"] = $left;
$region["bottom"] = $bottom;
$region["right"] = $right;
$region["top"] = $top;
return $region;
}
The ShouldExpand methods could have been written in a cleaner fashion, but I went for something fast to prototype with:
function ShouldExpandLeft($image, $background, $left, $bottom, $top, $padding)
{
// Find the farthest pixel that is not $background starting at $left - $padding closing in to $left
for($x = max(0, $left - $padding); $x < $left; ++$x)
{
for($y = $bottom; $y <= $top; ++$y)
{
$pixelColor = imagecolorat($image, $x, $y);
if($pixelColor != $background)
{
return $x;
}
}
}
return $left;
}
function ShouldExpandRight($image, $background, $right, $bottom, $top, $width, $padding)
{
// Find the farthest pixel that is not $background starting at $right + $padding closing in to $right
$from = min($width - 1, $right + $padding);
$to = $right;
for($x = $from; $x > $to; --$x)
{
for($y = $bottom; $y <= $top; ++$y)
{
$pixelColor = imagecolorat($image, $x, $y);
if($pixelColor != $background)
{
return $x;
}
}
}
return $right;
}
function ShouldExpandTop($image, $background, $top, $left, $right, $height, $padding)
{
// Find the farthest pixel that is not $background starting at $top + $padding closing in to $top
for($x = $left; $x <= $right; ++$x)
{
for($y = min($height - 1, $top + $padding); $y > $top; --$y)
{
$pixelColor = imagecolorat($image, $x, $y);
if($pixelColor != $background)
{
return $y;
}
}
}
return $top;
}
function ShouldExpandBottom($image, $background, $bottom, $left, $right, $padding)
{
// Find the farthest pixel that is not $background starting at $bottom - $padding closing in to $bottom
for($x = $left; $x <= $right; ++$x)
{
for($y = max(0, $bottom - $padding); $y < $bottom; ++$y)
{
$pixelColor = imagecolorat($image, $x, $y);
if($pixelColor != $background)
{
return $y;
}
}
}
return $bottom;
}
Now, to see if the algorithm was succesful, I added some debug code.
Debug Rendering
I created a second image to store debug info and store it on disk so I could later see my progress.
Using the following code:
$large2 = imagecreatefromjpeg($largeFilename);
$red = imagecolorallocate($large2, 255, 0, 0);
$green = imagecolorallocate($large2, 0, 255, 0);
$blue = imagecolorallocate($large2, 0, 0, 255);
function DrawRegions($image, $regions, $color)
{
foreach($regions as $region)
{
imagerectangle($image, $region["left"], $region["bottom"], $region["right"], $region["top"], $color);
}
}
DrawRegions($large2, $regions, $red);
imagejpeg($large2, "regions.jpg");
I could validate that my partitioning code was doing a decent job:
Aspect Ratio
I decided to filter out some regions based on aspect ratio (the ratio between the width and the height). Other filtering could be applied such as average pixel color or something, but the aspect ratio check was very fast so I used it.
I simply defined a "window" where regions would be kept, if their aspect ration was between a minimum and maximum value;
$smallAspectRatio = imagesx($small) / imagesy($small);
function PruneOutWrongAspectRatio($regions, $minAspectRatio, $maxAspectRatio)
{
$result = array();
foreach($regions as $region)
{
$aspectRatio = ($region["right"] - $region["left"]) / ($region["top"] - $region["bottom"]);
if($aspectRatio >= $minAspectRatio && $aspectRatio <= $maxAspectRatio)
{
array_push($result, $region);
}
}
return $result;
}
$filterOnAspectRatio = true;
if($filterOnAspectRatio == true)
{
$regions = PruneOutWrongAspectRatio($regions, $smallAspectRatio - 0.1 * $smallAspectRatio, $smallAspectRatio + 0.1 * $smallAspectRatio);
DrawRegions($large2, $regions, $blue);
}
imagejpeg($large2, "aspectratio.jpg");
By adding the DrawRegions call, I now paint in blue the regions that are still in the list as potential positions:
As you can see, only 4 position remains!
Finding the Corners
We're almost done! Now, what I'm doing is looking at the colors in the four corners from the small picture, and try to find the best matching pixel in the corners of the remaining regions. This code has the most potential to fail so if you have to invest time in improving the solution, this code would be a good candidate.
function FindCorners($large, $small, $regions)
{
$result = array();
$bottomLeftColor = imagecolorat($small, 0, 0);
$blColors = GetColorComponents($bottomLeftColor);
$bottomRightColor = imagecolorat($small, imagesx($small) - 1, 0);
$brColors = GetColorComponents($bottomRightColor);
$topLeftColor = imagecolorat($small, 0, imagesy($small) - 1);
$tlColors = GetColorComponents($topLeftColor);
$topRightColor = imagecolorat($small, imagesx($small) - 1, imagesy($small) - 1);
$trColors = GetColorComponents($topRightColor);
foreach($regions as $region)
{
$bottomLeft = null;
$bottomRight = null;
$topLeft = null;
$topRight = null;
$regionWidth = $region["right"] - $region["left"];
$regionHeight = $region["top"] - $region["bottom"];
$maxRadius = min($regionWidth, $regionHeight);
$topLeft = RadialFindColor($large, $tlColors, $region["left"], $region["top"], 1, -1, $maxRadius);
$topRight = RadialFindColor($large, $trColors, $region["right"], $region["top"], -1, -1, $maxRadius);
$bottomLeft = RadialFindColor($large, $blColors, $region["left"], $region["bottom"], 1, 1, $maxRadius);
$bottomRight = RadialFindColor($large, $brColors, $region["right"], $region["bottom"], -1, 1, $maxRadius);
if($bottomLeft["found"] && $topRight["found"] && $topLeft["found"] && $bottomRight["found"])
{
$left = min($bottomLeft["x"], $topLeft["x"]);
$right = max($bottomRight["x"], $topRight["x"]);
$bottom = min($bottomLeft["y"], $bottomRight["y"]);
$top = max($topLeft["y"], $topRight["y"]);
array_push($result, array("left" => $left, "right" => $right, "bottom" => $bottom, "top" => $top));
}
}
return $result;
}
$closeOnCorners = true;
if($closeOnCorners == true)
{
$regions = FindCorners($large, $small, $regions);
DrawRegions($large2, $regions, $green);
}
I tried to find the matching color by increasing "radially" (its basically squares) from the corners until I find a matching pixel (within a tolerance):
function GetColorComponents($color)
{
return array("red" => $color & 0xFF, "green" => ($color >> 8) & 0xFF, "blue" => ($color >> 16) & 0xFF);
}
function GetDistance($color, $r, $g, $b)
{
$colors = GetColorComponents($color);
return (abs($r - $colors["red"]) + abs($g - $colors["green"]) + abs($b - $colors["blue"]));
}
function RadialFindColor($large, $color, $startx, $starty, $xIncrement, $yIncrement, $maxRadius)
{
$result = array("x" => -1, "y" => -1, "found" => false);
$treshold = 40;
for($r = 1; $r <= $maxRadius; ++$r)
{
$closest = array("x" => -1, "y" => -1, "distance" => 1000);
for($i = 0; $i <= $r; ++$i)
{
$x = $startx + $i * $xIncrement;
$y = $starty + $r * $yIncrement;
$pixelColor = imagecolorat($large, $x, $y);
$distance = GetDistance($pixelColor, $color["red"], $color["green"], $color["blue"]);
if($distance < $treshold && $distance < $closest["distance"])
{
$closest["x"] = $x;
$closest["y"] = $y;
$closest["distance"] = $distance;
break;
}
}
for($i = 0; $i < $r; ++$i)
{
$x = $startx + $r * $xIncrement;
$y = $starty + $i * $yIncrement;
$pixelColor = imagecolorat($large, $x, $y);
$distance = GetDistance($pixelColor, $color["red"], $color["green"], $color["blue"]);
if($distance < $treshold && $distance < $closest["distance"])
{
$closest["x"] = $x;
$closest["y"] = $y;
$closest["distance"] = $distance;
break;
}
}
if($closest["distance"] != 1000)
{
$result["x"] = $closest["x"];
$result["y"] = $closest["y"];
$result["found"] = true;
return $result;
}
}
return $result;
}
As you can see, I'm no PHP expert, I didn't know there was a built in function to get the rgb channels, oops!
Final Call
So now that the algorithm ran, let's see what it found using the following code:
foreach($regions as $region)
{
echo "Potentially between " . $region["left"] . "," . $region["bottom"] . " and " . $region["right"] . "," . $region["top"] . "\n";
}
imagejpeg($large2, "final.jpg");
imagedestroy($large2);
The output (which is pretty close to the real solution):
Potentially between 108,380 and 867,827
in 7.9796848297119 seconds
Giving this picture (the rectangle between 108,380 and 867,827 is drawn in green)
Hope this helps!
My solution work if there is no color (except white and black around the image, but you can modify the script to get it work differently)
$width = imagesx($this->img_src);
$height = imagesy($this->img_src);
// navigate through pixels of image
for ($y = 0; $y < $height; $y++) {
for ($x=0; $x < $width; $x++) {
list($r, $g, $b) = imagergbat($this->img_src, $x, $y);
$black = 0.1;
$white = 0.9;
// calculate if the color is next to white or black, if not register it as a good pixel
$gs = (($r / 3) + ($g / 3) + ($b / 3);
$first_pixel = array();
if ($gs > $white && $gs < $black) {
// get coordinate of first pixel (left top)
if (empty($first_pixel))
$first_pixel = array($x, $y);
// And save last_pixel each time till the last one
$last_pixel = array($x, $y);
}
}
}
And you get the coordinates of your image. You have just to crop it after this.

How to create Double bar diagram using fpdf php?

I'm using FPDF in my php project. I would like to have PDF version Double bar diagram like above image in my project. There's a way that FPDF can create Pie chart and Bar diagram in http://www.fpdf.org/en/script/script28.php. But it's not double bar diagram like what I want to get. Anyone have an idea how to create Double bar diagram using FPDF in PHP?
Many Thanks !!!
Probably you mean "COLUMN CHARTS"
It seems that there is no method to create column charts, so I tried to adapt the existing bar chart. Unfortunately I have no time to develop it further.
try this (making the necessary changes):
<?php
require('diag/sector.php');
class PDF_Diag extends PDF_Sector {
var $legends;
var $wLegend;
var $sum;
var $NbVal;
function ColumnChart($w, $h, $data, $format, $color=null, $maxVal=0, $nbDiv=4)
{
// RGB for color 0
$colors[0][0] = 155;
$colors[0][1] = 75;
$colors[0][2] = 155;
// RGB for color 1
$colors[1][0] = 0;
$colors[1][1] = 155;
$colors[1][2] = 0;
// RGB for color 2
$colors[2][0] = 75;
$colors[2][1] = 155;
$colors[2][2] = 255;
// RGB for color 3
$colors[3][0] = 75;
$colors[3][1] = 0;
$colors[3][2] = 155;
$this->SetFont('Courier', '', 10);
$this->SetLegends($data,$format);
// Starting corner (current page position where the chart has been inserted)
$XPage = $this->GetX();
$YPage = $this->GetY();
$margin = 2;
// Y position of the chart
$YDiag = $YPage + $margin;
// chart HEIGHT
$hDiag = floor($h - $margin * 2);
// X position of the chart
$XDiag = $XPage + $margin;
// chart LENGHT
$lDiag = floor($w - $margin * 3 - $this->wLegend);
if($color == null)
$color=array(155,155,155);
if ($maxVal == 0)
{
foreach($data as $val)
{
if(max($val) > $maxVal)
{
$maxVal = max($val);
}
}
}
// define the distance between the visual reference lines (the lines which cross the chart's internal area and serve as visual reference for the column's heights)
$valIndRepere = ceil($maxVal / $nbDiv);
// adjust the maximum value to be plotted (recalculate through the newly calculated distance between the visual reference lines)
$maxVal = $valIndRepere * $nbDiv;
// define the distance between the visual reference lines (in milimeters)
$hRepere = floor($hDiag / $nbDiv);
// adjust the chart HEIGHT
$hDiag = $hRepere * $nbDiv;
// determine the height unit (milimiters/data unit)
$unit = $hDiag / $maxVal;
// determine the bar's thickness
$lBar = floor($lDiag / ($this->NbVal + 1));
$lDiag = $lBar * ($this->NbVal + 1);
$eColumn = floor($lBar * 80 / 100);
// draw the chart border
$this->SetLineWidth(0.2);
$this->Rect($XDiag, $YDiag, $lDiag, $hDiag);
$this->SetFont('Courier', '', 10);
$this->SetFillColor($color[0],$color[1],$color[2]);
$i=0;
foreach($data as $val)
{
//Column
$yval = $YDiag + $hDiag;
$xval = $XDiag + ($i + 1) * $lBar - $eColumn/2;
$lval = floor($eColumn/(count($val)));
$j=0;
foreach($val as $v)
{
$hval = (int)($v * $unit);
$this->SetFillColor($colors[$j][0], $colors[$j][1], $colors[$j][2]);
$this->Rect($xval+($lval*$j), $yval, $lval, -$hval, 'DF');
$j++;
}
//Legend
$this->SetXY($xval, $yval + $margin);
$this->Cell($lval, 5, $this->legends[$i],0,0,'C');
$i++;
}
//Scales
for ($i = 0; $i <= $nbDiv; $i++)
{
$ypos = $YDiag + $hRepere * $i;
$this->Line($XDiag, $ypos, $XDiag + $lDiag, $ypos);
$val = ($nbDiv - $i) * $valIndRepere;
$ypos = $YDiag + $hRepere * $i;
$xpos = $XDiag - $margin - $this->GetStringWidth($val);
$this->Text($xpos, $ypos, $val);
}
}
function SetLegends($data, $format)
{
$this->legends=array();
$this->wLegend=0;
$this->NbVal=count($data);
}
}
$pdf = new PDF_Diag();
$pdf->AddPage();
$data[0] = array(470, 490, 90);
$data[1] = array(450, 530, 110);
$data[2] = array(420, 580, 100);
// Column chart
$pdf->SetFont('Arial', 'BIU', 12);
$pdf->Cell(210, 5, 'Chart Title', 0, 1, 'C');
$pdf->Ln(8);
$valX = $pdf->GetX();
$valY = $pdf->GetY();
$pdf->ColumnChart(110, 100, $data, null, array(255,175,100));
//$pdf->SetXY($valX, $valY);
$pdf->Output();
?>

Auto Font Size For Text (GD via PHP)

There is a space of x*y for text to go on $im (GD Image Resource) how can I choose a font size (or write text such that) it does not overflow over that area?
I think you look for the imagettfbbox function.
I used that some years ago for a script generating localized buttons for a Web interface. I actually resized buttons if the text didn't fit in the template, to keep text size consistent, but you can try to reduce the text size until the text fits.
If you are interested, I can paste some snippets of my code (or give it right away).
[EDIT] OK, here is some extracts of my code, someday I will clean it up (make it independent of target app, give samples) and make it public as a whole.
I hope the snippets make sense.
// Bounding boxes: ImageTTFBBox, ImageTTFText:
// Bottom-Left: $bb[0], $bb[1]
// Bottom-Right: $bb[2], $bb[3]
// Top-Right: $bb[4]; $bb[5]
// Top-Left: $bb[6], $bb[7]
define('GDBB_TOP', 5);
define('GDBB_LEFT', 0);
define('GDBB_BOTTOM', 1);
define('GDBB_RIGHT', 2);
#[ In class constructor ]#
// Get size in pixels, must convert to points for GD2.
// Because GD2 assumes 96 pixels per inch and we use more "standard" 72.
$this->textSize *= 72/96;
$this->ComputeTextDimensions($this->textSize, FONT, $this->text);
#[ Remainder of the class (extract) ]
/**
* Compute the dimensions of the text.
*/
function ComputeTextDimensions($textSize, $fontFile, $text)
{
$this->textAreaWidth = $this->imageHSize - $this->marginL - $this->marginR;
$this->textAreaHeight = $this->imageVSize - $this->marginT - $this->marginB;
// Handle text on several lines
$this->lines = explode(NEWLINE_CHAR, $text);
$this->lineNb = count($this->lines);
if ($this->lineNb == 1)
{
$bb = ImageTTFBBox($textSize, 0, $fontFile, $text);
$this->textWidth[0] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
$this->maxTextWidth = $this->textWidth[0];
$this->textHeight[0] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
}
else
{
for ($i = 0; $i < $this->lineNb; $i++)
{
$bb = ImageTTFBBox($textSize, 0, $fontFile, $this->lines[$i]);
$this->textWidth[$i] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
$this->maxTextWidth = max($this->maxTextWidth, $this->textWidth[$i]);
$this->textHeight[$i] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
}
}
// Is the given text area width too small for asked text?
if ($this->maxTextWidth > $this->textAreaWidth)
{
// Yes! Increase button size
$this->textAreaWidth = $this->maxTextWidth;
$this->imageHSize = $this->textAreaWidth + $this->marginL + $this->marginR;
}
// Now compute the text positions given the new (?) text area width
if ($this->lineNb == 1)
{
$this->ComputeTextPosition(0, $textSize, $fontFile, $text, false);
}
else
{
for ($i = 0; $i < $this->lineNb; $i++)
{
$this->ComputeTextPosition($i, $textSize, $fontFile, $this->lines[$i], false);
}
}
}
/**
* Compute xText and yText (text position) for the given text.
*/
function ComputeTextPosition($index, $textSize, $fontFile, $text, $centerAscDesc)
{
switch ($this->textAlign)
{
case 'L':
$this->xText[$index] = $this->marginL;
break;
case 'R':
$this->xText[$index] = $this->marginL +
$this->textAreaWidth - $this->textWidth[$index];
break;
case 'C':
default:
$this->xText[$index] = $this->marginL +
($this->textAreaWidth - $this->textWidth[$index]) / 2;
break;
}
if ($centerAscDesc)
{
// Must compute the difference between baseline and bottom of BB.
// I have to use a temporary image, as ImageTTFBBox doesn't use coordinates
// providing offset from the baseline.
$tmpBaseline = 5;
// Image size isn't important here, GD2 still computes correct BB
$tmpImage = ImageCreate(5, 5);
$bbt = ImageTTFText($tmpImage, $this->textSize, 0, 0, $tmpBaseline,
$this->color, $fontFile, $text);
// Bottom to Baseline
$baselinePos = $bbt[GDBB_BOTTOM] - $tmpBaseline;
ImageDestroy($tmpImage);
$this->yText[$index] = $this->marginT + $this->textAreaHeight -
($this->textAreaHeight - $this->textHeight) / 2 - $baselinePos + 0.5;
}
else
{
// Actually, we want to center the x-height, ie. to keep the baseline at same pos.
// whatever the text is really, ie. independantly of ascenders and descenders.
// This provide better looking buttons, as they are more consistent.
$bbt = ImageTTFBBox($textSize, 0, $fontFile, "moxun");
$tmpHeight = $bbt[GDBB_BOTTOM] - $bbt[GDBB_TOP];
$this->yText[$index] = $this->marginT + $this->textAreaHeight -
($this->textAreaHeight - $tmpHeight) / 2 + 0.5;
}
}
/**
* Add the text to the button.
*/
function DrawText()
{
for ($i = 0; $i < $this->lineNb; $i++)
{
// Increase slightly line height
$yText = $this->yText[$i] + $this->textHeight[$i] * 1.1 *
($i - ($this->lineNb - 1) / 2);
ImageTTFText($this->image, $this->textSize, 0,
$this->xText[$i], $yText, $this->color, FONT, $this->lines[$i]);
}
}
I've taken the class by PhiLho above and expanded it to dynamically scale individual lines of text to fit within the bounding box. Also I wrapped it with a call so you can see how it functions. This is pretty much copy-pasta, just change your variables and it should work out of the box.
<?
header("Content-type: image/png");
define('GDBB_TOP', 5);
define('GDBB_LEFT', 0);
define('GDBB_BOTTOM', 1);
define('GDBB_RIGHT', 2);
class DrawFont {
function DrawFont($details) {
// Get size in pixels, must convert to points for GD2.
// Because GD2 assumes 96 pixels per inch and we use more "standard" 72.
$this->textSizeMax = $details['size'];
$this->font = $details['font'];
$this->text = $details['text'];
$this->image = $details['image'];
$this->color = $details['color'];
$this->shadowColor = $details['shadowColor'];
$this->textAlign = "C";
$this->imageHSize = $details['imageHSize'];
$this->imageVSize = $details['imageVSize'];
$this->marginL = $details['marginL'];
$this->marginR = $details['marginR'];
$this->marginT = $details['marginT'];
$this->marginB = $details['marginB'];
$this->ComputeTextDimensions($this->font, $this->text);
}
/**
* Compute the dimensions of the text.
*/
function ComputeTextDimensions($fontFile, $text)
{
$this->textAreaWidth = $this->imageHSize - $this->marginL - $this->marginR;
$this->textAreaHeight = $this->imageVSize - $this->marginT - $this->marginB;
// Handle text on several lines
$this->lines = explode(' ', $text);
$this->lineNb = count($this->lines);
if ($this->lineNb == 1)
{
$this->textSize[0] = $this->textSizeMax;
$bb = ImageTTFBBox($this->textSize[0], 0, $fontFile, $text);
$this->textWidth[0] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
$this->maxTextWidth = $this->textWidth[0];
$this->textHeight[0] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
$this->textSize[0] = $this->textSizeMax;
while ($this->textWidth[0] > $this->textAreaWidth && $this->textSize[0] > 1) {
$this->textSize[0]--;
$bb = ImageTTFBBox($this->textWidth[$i], 0, $fontFile, $text);
$this->textWidth[0] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
$this->maxTextWidth = $this->textWidth[0];
$this->textHeight[0] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
}
}
else
{
for ($i = 0; $i < $this->lineNb; $i++)
{
$this->textSize[$i] = $this->textSizeMax;
$bb = ImageTTFBBox($this->textSize[$i], 0, $fontFile, $this->lines[$i]);
$this->textWidth[$i] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
$this->maxTextWidth = max($this->maxTextWidth, $this->textWidth[$i]);
$this->textHeight[$i] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
while ($this->textWidth[$i] > $this->textAreaWidth && $this->textSize[$i] > 1) {
$this->textSize[$i]--;
$bb = ImageTTFBBox($this->textSize[$i], 0, $fontFile, $this->lines[$i]);
$this->textWidth[$i] = $bb[GDBB_RIGHT] - $bb[GDBB_LEFT];
$this->maxTextWidth = max($this->maxTextWidth, $this->textWidth[$i]);
$this->textHeight[$i] = $bb[GDBB_BOTTOM] - $bb[GDBB_TOP];
}
}
}
/*
// Is the given text area width too small for asked text?
if ($this->maxTextWidth > $this->textAreaWidth)
{
// Yes! Increase button size
$this->textAreaWidth = $this->maxTextWidth;
$this->imageHSize = $this->textAreaWidth + $this->marginL + $this->marginR;
}
*/
// Now compute the text positions given the new (?) text area width
if ($this->lineNb == 1)
{
$this->ComputeTextPosition(0, $this->textSize[0], $fontFile, $text, false);
}
else
{
for ($i = 0; $i < $this->lineNb; $i++)
{
$this->ComputeTextPosition($i, $this->textSize[$i], $fontFile, $this->lines[$i], false);
}
}
}
/**
* Compute xText and yText (text position) for the given text.
*/
function ComputeTextPosition($index, $textSize, $fontFile, $text, $centerAscDesc)
{
switch ($this->textAlign)
{
case 'L':
$this->xText[$index] = $this->marginL;
break;
case 'R':
$this->xText[$index] = $this->marginL +
$this->textAreaWidth - $this->textWidth[$index];
break;
case 'C':
default:
$this->xText[$index] = $this->marginL +
($this->textAreaWidth - $this->textWidth[$index]) / 2;
break;
}
if ($centerAscDesc)
{
// Must compute the difference between baseline and bottom of BB.
// I have to use a temporary image, as ImageTTFBBox doesn't use coordinates
// providing offset from the baseline.
$tmpBaseline = 5;
// Image size isn't important here, GD2 still computes correct BB
$tmpImage = ImageCreate(5, 5);
$bbt = ImageTTFText($tmpImage, $this->textSizeMax, 0, 0, $tmpBaseline,
$this->color, $fontFile, $text);
// Bottom to Baseline
$baselinePos = $bbt[GDBB_BOTTOM] - $tmpBaseline;
ImageDestroy($tmpImage);
$this->yText[$index] = $this->marginT + $this->textAreaHeight -
($this->textAreaHeight - $this->textHeight) / 2 - $baselinePos + 0.5;
}
else
{
// Actually, we want to center the x-height, ie. to keep the baseline at same pos.
// whatever the text is really, ie. independantly of ascenders and descenders.
// This provide better looking buttons, as they are more consistent.
$bbt = ImageTTFBBox($textSize, 0, $fontFile, "moxun");
$tmpHeight = $bbt[GDBB_BOTTOM] - $bbt[GDBB_TOP];
$this->yText[$index] = $this->marginT + $this->textAreaHeight -
($this->textAreaHeight - $tmpHeight) / 2 + 0.5;
}
}
/**
* Add the text to the button.
*/
function DrawText()
{
$this->maxTextHeight = 0;
// find maxTextHeight
for ($i = 0; $i < $this->lineNb; $i++)
{
if ($this->textHeight[$i] > $this->maxTextHeight) {
$this->maxTextHeight = $this->textHeight[$i];
}
}
for ($i = 0; $i < $this->lineNb; $i++)
{
// Increase slightly line height
$yText = $this->yText[$i] + $this->maxTextHeight * 1.1 *
($i - ($this->lineNb - 1) / 2);
ImageTTFText($this->image, $this->textSize[$i], 0,
$this->xText[$i]+2, $yText+2, $this->shadowColor, $this->font, $this->lines[$i]);
ImageTTFText($this->image, $this->textSize[$i], 0,
$this->xText[$i], $yText, $this->color, $this->font, $this->lines[$i]);
}
}
}
// Script starts here
$im = imagecreatefromjpeg("/home/cvgcfjpq/public_html/fb/img/page_template.jpg");
$color = imagecolorallocate($im, 235, 235, 235);
$shadowColor = imagecolorallocate($im, 90, 90, 90);
$details = array("image" => $im,
"font" => "OldSansBlack.ttf",
"text" => $_GET['name'],
"color" => $color,
"shadowColor" => $shadowColor,
"size" => 40,
"imageHSize" => 200,
"imageVSize" => 250,
"marginL" => 5,
"marginR" => 5,
"marginT" => 5,
"marginB" => 5);
$dofontobj =& new DrawFont($details);
$dofontobj->DrawText();
imagepng($im);
imagedestroy($im);
unset($px);
?>

Categories