0

I want to create collage of photos with PHP. I created a test code on my localhost as follows:

<style type="text/css">
body{background:url('images/Winter.jpg'); }
#collage:after{content:"";clear:both;display:block;}
}

</style>

<?php

ob_start();
$dir = "images";

if($fp = opendir($dir))
{
  while($file = readdir($fp))
  {
    if('jpg'==(pathinfo($file, PATHINFO_EXTENSION)))
    {
      $style = "style='float:left; -webkit-transform:rotate(".mt_rand(2, 30)."deg); border:solid 5px #eee;'";
       $ht = "height='".mt_rand(100, 300)."'";
    echo "<div class='img_div' $style>";
    echo "<img src='$dir/$file' $ht >";
    echo "<div style='background:#eee;font-size:20px;'>hi</div>";
    echo '</div>';
     }
   }
}
closedir($fp);
?>

It generated the output which I want but now I want the user to be able to download it as an image file. How do I do it?

0

2 Answers 2

1

If you want that you will have to create a new blank image using something like php's GD library and position your images into that instead of printing and positioning them using html/css

(I believe I read somewhere that in the future you will be able to display html/css on a html5 canvas, if that's true you'd be able to extract an image using that, but for now I don't think that will be possible)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks jeroen i'll try gd library, but its bit painful to use it. I was thinking to use imagick for it but i don't know how to install and use it.
1

Short of using the new HTML5 canvas feature, that will let the browser do all the work of merging the mosaic into one single image for you (but is it supported by many browsers yet ? I honestly don't know.),

you can also create that image on the server's side, using php's GD library. That was, I think, the only way before HTML5 and may still be the best way for a while (as I said, I don't know exactly what HTML5 implementations are worth to this day)

A good place to start would be there: http://php.net/manual/en/ref.image.php (read the many examples by visitors, on that page and on the function-specific pages, they can be a valuable education).

Now, this method has one inconvenient (in addition to you having to do the whole work): it taxes your server's CPU and slows down its response. That's ok if you have only a few visitors at a time. But otherwise, your collage should be pre-calculated once and for all, so the server doesn't have to redo the job for each visit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.