Last active
September 20, 2016 22:56
-
-
Save acegreen/5af573d71b885a9b7cd7 to your computer and use it in GitHub Desktop.
combine images in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
foreach($_POST as $data) { | |
//$data = $_POST['data']; | |
$file = 'snapshot-'.md5(uniqid()) . '.png'; | |
$result=(array)(json_decode($data,true)); | |
$chart['plot']=$result["charts"][0]["panes"][0]["content"]; | |
$chart['rightaxis']=$result["charts"][0]["panes"][0]["rightAxis"]["content"]; | |
$chart['timeaxis']=$result["charts"][0]["timeAxis"]["content"]; | |
$mainSeriesText=$result["charts"][0]["panes"][0]["mainSeriesText"]; | |
foreach($chart as $name=>$val) { | |
$uri = substr($val,strpos($val,",")+1); | |
file_put_contents('images/'.$name.$file, base64_decode($uri)); | |
} | |
} | |
//define the width and height of our images | |
//take create image resources out of the 3 pngs we want to merge into destination image | |
$a = imagecreatefrompng('images/plot'.$file); | |
$b = imagecreatefrompng('images/rightaxis'.$file); | |
$c = imagecreatefrompng('images/timeaxis'.$file); | |
list($width, $height, $type, $attr) = getimagesize('images/plot'.$file); | |
define("WIDTHP", $width); | |
define("HEIGHTP", $height); | |
list($width, $height, $type, $attr) = getimagesize('images/rightaxis'.$file); | |
define("WIDTHR", $width); | |
define("HEIGHTR", $height); | |
list($width, $height, $type, $attr) = getimagesize('images/timeaxis'.$file); | |
define("WIDTHT", $width); | |
define("HEIGHTT", $height); | |
// Create MainSeriesText Image based on plot size | |
$d = imagecreate(WIDTHP, 100); | |
$background_color = imagecolorallocatealpha($d, 255, 255, 255, 127); | |
$text_color = imagecolorallocate($d, 92, 92, 102); | |
$fontfile = 'images/fonts/HelveticaNeue.ttf'; | |
imagettftext($d, 28, 0, 20, 40, $text_color, $fontfile, $mainSeriesText); | |
define("WIDTHMST", imagesx($d)); | |
define("HEIGHTMST", imagesy($d)); | |
$dest_image = imagecreatetruecolor(WIDTHP + WIDTHR, HEIGHTP + HEIGHTT); | |
//make sure the transparency information is saved | |
imagesavealpha($dest_image, true); | |
//create a white background (127 means fully transparent) | |
$white = imagecolorallocate($dest_image, 255, 255, 255); | |
//fill the image with a white background | |
imagefill($dest_image, 0, 0, $white); | |
//copy each png file on top of the destination (result) png | |
imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTHP, HEIGHTP); | |
imagecopy($dest_image, $b, WIDTHP, 0, 0, 0, WIDTHR, HEIGHTR); | |
imagecopy($dest_image, $c, 0, HEIGHTP, 0, 0, WIDTHT, HEIGHTT); | |
imagecopy($dest_image, $d, 0, 0, 0, 0, WIDTHMST, HEIGHTMST); | |
//send the appropriate headers and output the image in the browser | |
imagepng($dest_image,'images/'.$file); | |
//destroy all the image resources to free up memory | |
imagedestroy($a); | |
imagedestroy($b); | |
imagedestroy($c); | |
imagedestroy($d); | |
imagedestroy($dest_image); | |
unlink('images/plot'.$file); | |
unlink('images/rightaxis'.$file); | |
unlink('images/timeaxis'.$file); | |
// return the filename | |
echo '/images/'.$file; exit; | |
?> |
you don't need to add the width of the time axis and you don't need to add the height of the right axis. should get you closer to what you expect
@samadsajanlal Ah rookie mistake, how did I not see that!!! thanks for the comment. Works perfectly!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The outcome is too big, the Canvas is suppose to be the side of the 3 images combined both width and height. But it seems to exceed that as can be seen by all the white space.