Combining picture with php array instead of giving eachfile name - php

Hello i have this code combining jpeg file into one :
<?php
function mergeImages($images) {
$imageData = array();
$len = count($images);
$wc = ceil(sqrt($len));
$hc = floor(sqrt($len/2));
$maxW = array();
$maxH = array();
for($i = 0; $i < $len; $i++) {
$imageData[$i] = getimagesize($images[$i]);
$found = false;
for($j = 0; $j < $i; $j++) {
if ( $imageData[$maxW[$j]][0] < $imageData[$i][0] ) {
$farr = $j > 0 ? array_slice($maxW, $j-1, $i) : array();
$maxW = array_merge($farr, array($i), array_slice($maxW, $j));
$found = true;
break;
}
}
if ( !$found ) {
$maxW[$i] = $i;
}
$found = false;
for($j = 0; $j < $i; $j++) {
if ( $imageData[$maxH[$j]][1] < $imageData[$i][1] ) {
$farr = $j > 0 ? array_slice($maxH, $j-1, $i) : array();
$maxH = array_merge($farr, array($i), array_slice($maxH, $j));
$found = true;
break;
}
}
if ( !$found ) {
$maxH[$i] = $i;
}
}
$width = 0;
for($i = 0; $i < $wc; $i++) {
$width += $imageData[$maxW[$i]][0];
}
$height = 0;
for($i = 0; $i < $hc; $i++) {
$height += $imageData[$maxH[$i]][1];
}
$im = imagecreatetruecolor($width, $height);
$wCnt = 0;
$startWFrom = 0;
$startHFrom = 0;
for( $i = 0; $i < $len; $i++ ) {
$tmp = imagecreatefromjpeg($images[$i]);
imagecopyresampled($im, $tmp, $startWFrom, $startHFrom, 0, 0, $imageData[$i][0], $imageData[$i][1], $imageData[$i][0], $imageData[$i][1]);
$wCnt++;
if ( $wCnt == $wc ) {
$startWFrom = 0;
$startHFrom += $imageData[$maxH[0]][1];
$wCnt = 0;
} else {
$startWFrom += $imageData[$i][0];
}
}
return $im;
}
$im = mergeImages(array('2013101.jpg', '2013102.jpg'));
header('Content-type: image/jpg');
?>
I would like to combinining 10 images from 2013101.jpg to 2013110.jpg instead of giving each file name i would like to make array range.
please help me

You can use a simple for loop, I guess?
for ($i = 2013101; $i < 2013110; $i++) {
$im = mergeImages(array("$i.jpg", ''. $i+1 .'.jpg'));
}
This will combine 101 with 102, 102 with 103, etc. If you need to combine 101 with 102 and 103 with 104, withoue 102 with 103, you need to increment $i with 2 in the for declaration

Related

How to save merged image inside folder in PHP?

I have to merge two images into one single image through PHP code. For that, I used the GD library of PHP. By using it I have merged the images and got one single image. Now what I want is to save the merged image into a folder and want to get the folder URL.
Here is the code which I used to merge the images:-
function mergeImages($images)
{
$imageData = array();
$len = count($images);
$wc = ceil(sqrt($len));
$hc = floor(sqrt($len/2));
$maxW = array();
$maxH = array();
for($i = 0; $i < $len; $i++)
{
$imageData[$i] = getimagesize($images[$i]);
$found = false;
for($j = 0; $j < $i; $j++)
{
if ( $imageData[$maxW[$j]][0] < $imageData[$i][0] )
{
$farr = $j > 0 ? array_slice($maxW, $j-1, $i) : array();
$maxW = array_merge($farr, array($i), array_slice($maxW, $j));
$found = true;
break;
}
}
if ( !$found )
{
$maxW[$i] = $i;
}
$found = false;
for($j = 0; $j < $i; $j++)
{
if ( $imageData[$maxH[$j]][1] < $imageData[$i][1] )
{
$farr = $j > 0 ? array_slice($maxH, $j-1, $i) : array();
$maxH = array_merge($farr, array($i), array_slice($maxH, $j));
$found = true;
break;
}
}
if ( !$found )
{
$maxH[$i] = $i;
}
}
$width = 0;
for($i = 0; $i < $wc; $i++)
{
$width += $imageData[$maxW[$i]][0];
}
$height = 0;
for($i = 0; $i < $hc; $i++)
{
$height += $imageData[$maxH[$i]][1];
}
$im = imagecreatetruecolor($width, $height);
$wCnt = 0;
$startWFrom = 0;
$startHFrom = 0;
for( $i = 0; $i < $len; $i++ )
{
$tmp = imagecreatefromjpeg($images[$i]);
imagecopyresampled($im, $tmp, $startWFrom, $startHFrom, 0, 0, $imageData[$i][0], $imageData[$i][1], $imageData[$i][0], $imageData[$i][1]);
$wCnt++;
if ( $wCnt == $wc )
{
$startWFrom = 0;
$startHFrom += $imageData[$maxH[0]][1];
$wCnt = 0;
}
else
{
$startWFrom += $imageData[$i][0];
}
}
return $im;
}
$im = mergeImages(array('https://example.com/image_uploads/1634073799779802772384.jpg?response-content-disposition=inline%3B%20filename%3D1634073799779802772384&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCLDZENWTCRKLOWK%2F20220111%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220111T051751Z&X-Amz-SignedHeaders=host&X-Amz-Expires=518400&X-Amz-Signature=bb6d0d1f3d00396033f7de6faef94c8bb97a36d3dda9407ffa9f1a68a72235e5',
'https://example.com/image_uploads/1634143552519639503004.jpg?response-content-disposition=inline%3B%20filename%3D1634143552519639503004&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCLDZENWTCRKLOWK%2F20220111%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220111T051751Z&X-Amz-SignedHeaders=host&X-Amz-Expires=518400&X-Amz-Signature=6d116cc87fb784cbaa870b13dce339f27392057cf8c2bb4ad552ac7c2df64704'));
header('Content-type: image/jpg');
imagejpeg($im);
Please help me out how would I save the merged image inside the folder.

Problem with Largest Product in a Grid Algorithm PHP

I have tried to complete Euler project problem 11 but somehow I got a product that was actually above the right answer. I have tried to calculate the adjacent numbers through the use of a single dimensional array and nested for loops. What error in my code is making this occur?
Code:
$numbers = str_replace(" ","",$numbers);
$numbers = str_split($numbers, 2);
function removeZeros($numbers) {
for ($i = 0; $i < count($numbers); $i++) {
$firstChar = substr($numbers[$i], 0, 1);
if ($firstChar == "0") {
$numbers[$i] = substr($numbers[$i], 1, 1);
}
}
return $numbers;
}
function gridProduct($numbers) {
removeZeros($numbers);
$i = 0;
$product = 0;
$largestGrid = 0;
// across
for ($j = 0; $j < 20; $j++) {
for ($i = 0; $i < 17; $i++) {
$k = $i + ($j * 20);
$product = $numbers[$k] * $numbers[$k + 1] * $numbers[$k + 2] * $numbers[$k + 3];
if ($product > $largestGrid) {
$largestGrid = $product;
}
}
}
// vertically
for ($j = 0; $j < 20; $j++) {
for ($i = 0; $i < 17; $i++) {
$k = $j + ($i * 20);
$product = $numbers[$k] * $numbers[$k + 20] * $numbers[$k + 40] * $numbers[$k + 60];
if ($product > $largestGrid) {
$largestGrid = $product;
}
}
}
// diagonally right
for ($j = 0; $j < 17; $j++) {
for ($i = 0; $i < 17; $i++) {
$k = $i + ($j * 20);
$product = $numbers[$k] * $numbers[$k + 21] * $numbers[$k + 21] * $numbers[$k + 21];
if ($product > $largestGrid) {
$largestGrid = $product;
}
}
}
// diagonally left
for ($j = 19; $j > 4; $j--) {
for ($i = 0; $i < 17; $i++) {
$k = $i + ($j * 20);
$product = $numbers[$k] * $numbers[$k - 19] * $numbers[$k - 19] * $numbers[$k - 19];
if ($product > $largestGrid) {
$largestGrid = $product;
}
}
}
echo " ";
echo $largestGrid;
}
gridProduct($numbers);```

Pie chart problem does not show if data is missing

I have a PHP pie chart which uses data from Mysql to show the chart. However if one data is missing the whole chart turns to one color. For example for grading if the input is A, B, D and F(pay attention C grade input is missing) then the whole pie chart is in one color like Orange or red.
Can you please help me with this? Thanks
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('0000ff', '006600', 'ffff00','DD7C1D', 'FF3300', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = array_filter(explode('*',$data));
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>

Save image to directory in PHP?

Here I'm merging 2 images into single image.
It's showing the image successfully imagejpeg($im); but I cannot save the image in specific location.imagejpeg($im,'images/downloadFile.jpg');
Php Code:
<?php
function mergeImages($images) {
$imageData = array();
$len = count($images);
$wc = ceil(sqrt($len));
$hc = floor(sqrt($len/2));
$maxW = array();
$maxH = array();
for($i = 0; $i < $len; $i++) {
$imageData[$i] = getimagesize($images[$i]);
$found = false;
for($j = 0; $j < $i; $j++) {
if ( $imageData[$maxW[$j]][0] < $imageData[$i][0] ) {
$farr = $j > 0 ? array_slice($maxW, $j-1, $i) : array();
$maxW = array_merge($farr, array($i), array_slice($maxW, $j));
$found = true;
break;
}
}
if ( !$found ) {
$maxW[$i] = $i;
}
$found = false;
for($j = 0; $j < $i; $j++) {
if ( $imageData[$maxH[$j]][1] < $imageData[$i][1] ) {
$farr = $j > 0 ? array_slice($maxH, $j-1, $i) : array();
$maxH = array_merge($farr, array($i), array_slice($maxH, $j));
$found = true;
break;
}
}
if ( !$found ) {
$maxH[$i] = $i;
}
}
$width = 0;
for($i = 0; $i < $wc; $i++) {
$width += $imageData[$maxW[$i]][0];
}
$height = 0;
for($i = 0; $i < $hc; $i++) {
$height += $imageData[$maxH[$i]][1];
}
$im = imagecreatetruecolor($width, $height);
$wCnt = 0;
$startWFrom = 0;
$startHFrom = 0;
for( $i = 0; $i < $len; $i++ ) {
$tmp = imagecreatefromjpeg($images[$i]);
imagecopyresampled($im, $tmp, $startWFrom, $startHFrom, 0, 0, $imageData[$i][0], $imageData[$i][1], $imageData[$i][0], $imageData[$i][1]);
$wCnt++;
if ( $wCnt == $wc ) {
$startWFrom = 0;
$startHFrom += $imageData[$maxH[0]][1];
$wCnt = 0;
} else {
$startWFrom += $imageData[$i][0];
}
}
return $im;
}
$im = mergeImages(array('images/1.jpg', 'images/2.jpg'));
header('Content-type: image/jpg');
imagejpeg($im); //View image
imagejpeg($im,'images/downloadFile.jpg'); //Not Saved image
Desired Output
cannot be displayed because it contains errors.
Thanks Advance!

Piechart creation in pdf file using fpdf in php

I want to create piechart in my pdf file created using fpdf. already i had created pdf with fpdf . then i want to create pie chart in that using same table data, is there any option to create pie chart using fpdf ?
Please Help.
Thanks in advance
Try this make changes in code as per your requirements:
You can display following view file on your pdf using pdf helper.
you can use dom pdf helper download it from following link.
http://code.google.com/p/dompdf/downloads/detail?name=dompdf_0-6-0_beta3.zip
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = explode('*',$data);
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label && $data[$i] > 0)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label && $data[$i] > 0)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($data[$i] > 0)
{
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
Hope this will help you... :)

Categories