0

I just wrote this code to generate an image

<?php

$canvas = imagecreatetruecolor(800, 350);

$pink = imagecolorallocate($canvas, 255, 105, 180);
$white = imagecolorallocate($canvas, 255, 255, 255);
$green = imagecolorallocate($canvas, 132, 135, 28);

imagefill($canvas, 0, 0, $white); // BACKGROUND

function drawlinebox($x1, $y1, $x2, $y2, $height, $color){
    global $canvas;
    imagesetthickness ( $canvas, 1 );
    for ($i=1; $i < $height; $i++){
        imageline( $canvas, $x1, $y1, $x2, $y2, $color );
        $y1++; $y2++;
    }
}

drawlinebox(20, 20, 780, 300, 30, $green);

drawlinebox(20, 300, 780, 20, 30, $pink);

// Output and free from memory
header('Content-Type: image/png');

imagepng($canvas, NULL, 9);

imagedestroy($canvas);

?>

but I also want this image to saved automatically on the server. Think of it as a cron job. Which creates images, then saves the image on the sever for later use and inserts the saved image location in the DB.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ameer
  • 761
  • 1
  • 6
  • 19

1 Answers1

6

As stated in the manual, imagepng()'s second parameter is $filename which allows you to store the result in a file.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I want to directly store the image in MySQL, without storing in File (even not temporarely), How can it possible – Thamilan S Jun 19 '13 at 12:13
  • @Mani it's possible by using `ob_start()` in combination with `imagepng()`, see http://www.php.net/manual/en/function.imagepng.php#53021. But it's probably not a good idea - there are many arguments against storing images in the database: [Storing Images in DB - Yea or Nay?](http://stackoverflow.com/q/3748) – Pekka Jun 19 '13 at 12:18