Jane and Jon

hounddog32

« Photos and Blogs | Main | Wedding »

July 04, 2005

Polaroid-o-nizer

I saw polaroid-o-nizer a few days ago, and it clicked with an idea for a website layout with "photos" down one side. As download squad said, it's a shame it gives out jpegs not transparent pngs. I had a hack about with the source and tried to make it work with pngs. Now my knowledge of the php image functions is at least limited, so I've spent half my time with loads of tabs open on php.net, looking for answers. Of course, it's relatively easy to get a png output, but making the alpha transparency work is tough. The original script uses lots of palette based functions (rather than true colour or 24 bit functions, which are necessary for the alpha trans for the pngs. I swapped them for the other functions, but there are still problems. The functions ImageCopy and ImageCopyMerge do very similar things, except the latter can handle transparency. Except I can't make it do what I want it to. I'm getting all kinds of weirdness with different bit types of png and no idea how to make it work. If anyone has any ideas, post me a comment. My code is below, after the break.
I have adapted the code to use my own inputs from the file, so i will not work on its own. To use it, I guess you'll need to cut and paste a bit. # Code adapted from http://www.polaroidonizer.nl.eu.org/ # errors corrected and GET/POST removed. # randomising functions called. define("VERSION", "0.5.7"); define("CREATIONDATE", "June 8th, 2005"); header("PoN-Version: " . VERSION); error_reporting(0); $data = array_merge($_GET, $_POST); $bg[0] = 255; $bg[1] = 255; $bg[2] = 255; # calls a random number with normal distribution, mean = 0, stdev = 15 $data['angle'] = gauss_ms(0.0,15.0) ; # uses photo url stripped above $data['photo'] = $imgurl; $info = getimagesize($data['photo']); if(!$info) { $errmsg['photo'] = "Incorrect URL or linked file does not exist:"; } elseif(!in_array($info[2], array(1, 2, 3))) { $errmsg['photo'] = "Unknown filetype. Use GIF, JPEG or PNG only:"; } elseif (($info[0] - $data['x']) < 180 || ($info[1] - $data['y']) < 180) { $errmsg['photo'] = "Imageresolution is too low:"; } elseif ($info[0] >= 2000 || $info[1] >= 2000) { $errmsg['photo'] = "Imageresolution is too high"; } if ($info[0] > $info[1]) { $data['x'] = ($info[0]-$info[1])/2; } else { $data['y'] = ($info[1]-$info[0])/2; } $text = "test"; $text = strip_tags(stripslashes(str_replace(" ", "_", trim($text)))); if(empty($text)) { $text = "Error: no comment"; } elseif(strlen($text) > 100) { $text = "Error: Text/remark is too long:"; } # checks on whether data has been entered has been entered removed $text = trim(strip_tags(stripslashes(str_replace("_", " ", $data['text'])))); $polaroid = imagecreatetruecolor(246, 300); imagealphablending($polaroid, true); imagesavealpha($polaroid, true); $bg = imagecolorallocate($polaroid, $bg[0], $bg[1], $bg[2]); imagefill($polaroid, 0, 0, $bg); $trans = imagecolortransparent($polaroid, $bg); $scale = round(($info[0] > $info[1]) ? (200 / $info[1]) : (200 / $info[0]), 4); if ($info[2] == 1) { $data['photo'] = imagecreatefromgif(stripslashes($data['photo'])); } elseif ($info[2] == 2) { $data['photo'] = imagecreatefromjpeg(stripslashes($data['photo'])); } elseif ($info[2] == 3) { $data['photo'] = imagecreatefrompng(stripslashes($data['photo'])); } $tmp = imagecreatetruecolor(200, 200); imagecopyresampled($tmp, $data['photo'], 0, 0, $data['x'], $data['y'], floor($info[0] * $scale), floor($info[1] * $scale), $info[0], $info[1]); imagecopymerge($polaroid, $tmp, 20, 18, 0, 0, 200, 200,100); $frame = imagecreatefrompng("images/frame2.png"); imagecopy($polaroid, $frame, 0, 0, 0, 0, 245, 301); $text = wordwrap($text, 25, "||", 1); $text = explode("||", $text); $black = imagecolorallocate($polaroid, 0, 0, 0); // Set the enviroment variable for GD putenv('GDFONTPATH=' . realpath('.')); $text_pos_y = array(235, 250, 265); for ($i = 0; $i < 3; $i++) { $width = imagettfbbox(10, 0, "annifont.ttf", $text[$i]); $text_pos_x = (196 - $width[2])/2 + 20; imagettftext($polaroid, 10, 0, $text_pos_x, $text_pos_y[$i], $black, "annifont.ttf", $text[$i]); } $trans = imagecolortransparent($polaroid, $bg); $polaroid = imagerotate($polaroid, $data['angle'], $bg ); header("Content-type: image/png"); imagepng($polaroid, "",90); imagedestroy($polaroid); exit; ?>

Technorati tags:

Posted by hounddog32 at July 4, 2005 11:23 PM

Trackback Pings

TrackBack URL for this entry:
http://www.hounddog32.com/mt-static/mt-tb.cgi/25

Comments

Posted by: Anonymous at July 4, 2005 11:23 PM

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Remember me?