Gorilla3D Primative Teachings
Subscribe to Feed:
Code for Scaling Watermarks for Images in PHP with GD
Tuesday, October 28, 2008
Had to do watermarking which, by itself is relatively easy. But then there was an issue with the ratio of the watermark and image size. So below is how to watermark with an image thats way too big for a small watermark.
0 comments
<?php //------------------------------------------------------------------------------ //-- Image //------------------------------------------------------------------------------ $filename = 'images/beagle01-lg.jpg'; $image = imagecreatefromjpeg($filename); //------------------------------------------------------------------------------ //-- Watermark //------------------------------------------------------------------------------ $wfilename = 'images/watermark.png'; $watermark = imagecreatefrompng($wfilename); //------------------------------------------------------------------------------ //-- Marked Image //------------------------------------------------------------------------------ // Grab the extension $extension = $pathinfo['extension']; // Give this file a random name //------------------------------------------------------------------------------ //-- Resample Watermark //-- Scale the water mark to a ratio of $ratio of the orginal image //-- TODO: Downward scaling for watermarks that are too large for smaller images //------------------------------------------------------------------------------ $ratio_limit = 3; // Ratio 1:3 $width = $watermark_width; $height = $watermark_height; $ratio = 1.0; while(1) { //-- TODO: Im sure there is a formula for this that doesn't need a loop //-- The image to watermater ratio is too small. ($width * $ratio >= $image_width or $height * $ratio >= $image_height)) { break; } else { $width = $width * $ratio; $height = $height * $ratio; $ratio += 0.1; } } $resampled = imagecreatetruecolor($newwidth, $newheight); imagealphablending($resampled, false); imagesavealpha($resampled, true); imagefilledrectangle($resampled, 0, 0, $newwidth, $newheight, imagecolorallocatealpha($resampled, 0, 0, 0, 127)); imagecopyresampled($resampled, $watermark, 0, 0, 0, 0, $newwidth, $newheight, $watermark_width, $watermark_height); $watermark = $resampled; //------------------------------------------------------------------------------ //-- Apply Watermark to bottom right of image //------------------------------------------------------------------------------ imagealphablending($image,true); $dst_w = imagesx($image); $dst_h = imagesy($image); imagealphablending($watermark,true); $src_w = imagesx($watermark); $src_h = imagesy($watermark); imagecopy($image, $watermark, ($dst_w-$src_w), ($dst_h-$src_h), 0, 0, $src_w, $src_h); imagejpeg($markedname, $filename, 85); ?>
0 comments
PHP & Firefox 3 AJAX Image Upload
Friday, August 15, 2008
PHP + Firefox3 AJAX Image Upload
Here is a sample dump of using firefox3's getAsDataURL to push to php and using GD to process it into an image and then save to file.
0 comments
Here is a sample dump of using firefox3's getAsDataURL to push to php and using GD to process it into an image and then save to file.
/* Ajax Functions */ function Ajax(url, options) { if (typeof XMLHttpRequest!='undefined') {var page = new XMLHttpRequest();} else if (window.createRequest) { var page = window.createRequest();} else if (window.ActiveXObject) { var page = new ActiveXObject('MSXML2.XMLHTTP.3.0');} else { alert('Ooops, we failed to process your request. Tell us your browser please!'); } if (page) { if (options.method == 'post') { page.open('POST', url, true); page.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded") } else { page.open('GET', url, true); } page.onreadystatechange = function() { if (page.readyState == 4 && page.status == 200) { if ('function' == typeof options.onComplete) options.onComplete(page.responseText); } } if (options.method == 'post') { page.send(options.data); } else { page.send(null); } } else { alert('There has been a problem processing your request'); } } function writeImage() { var imgdata = $('imagefile').files.item(0).getAsDataURL(); var imgsize = $('imagefile').files.item(0).fileSize; var imgname = $('imagefile').files.item(0).fileName; Ajax("post.image.php", {"method":"post", "data": {"data":imgdata, "size":imgsize, "name":imgname}, "onComplete": function(image) { alert('uploaded'); }}); }
<input type="file" id="imagefile" name="imagefile" onchange="writeImage()"/>
<?php $uploadfile = 'temp.jpg'; //-- Parse the data $img_str = $_POST['data']; $img_str = $img_str[1]; //-- Create image from string $image = imagecreatefromstring($img_str); //-- Save to file imagejpeg($image, $uploadfile, 85); ?>
0 comments
Modular C++ Web Server / Framework
Sunday, August 10, 2008
I am working on a modular cpp web server to allow compile c++ shared / dynamic libraries. This way if anyone is ever wanting to challenge themselves, they can do so by writing a module for the deamon using the framework.
Bare-Bone sample library
Compile the dynamic lib
How does one then use this library on the server, well... I'll eventually make a configuration file, but for now you ave to directly edit the webserver.
That correlates to http://localhost/helloworld and outputs "Hello World from a c++ sample"
1 comments
Bare-Bone sample library
#include <stdio.h> #include "HttpModule.h" class hello : public HttpModule { public: static char *get() { char *temp = "Hello World from a c++ sample"; return temp; } }; // the class factories extern "C" HttpModule* create() { return new hello; } extern "C" void destroy(HttpModule* p) { delete p; }
Compile the dynamic lib
# compile into a shared library g++ -fPIC -c hello.cpp g++ -shared -o libhello.so hello.o # Using g++ on intel mac # g++ -dynamiclib -o libhello.so hello.o
How does one then use this library on the server, well... I'll eventually make a configuration file, but for now you ave to directly edit the webserver.
module_map http_modules [] = { {"/helloworld", "./libhello.so"} };
That correlates to http://localhost/helloworld and outputs "Hello World from a c++ sample"
1 comments
Gorilla Db - (Improved) MySQLi Wrapper
Sunday, August 3, 2008
I've updated my mysqli wrapper to handle binds better and easier to new people trying to use prepared statements. I've also added Non-Filtered binds, because I'll also be release my Table class to help with simple queries you always end up running.
Db is a connection manager, it will use the connection you first create throughout your application. You can continue to call Db:init() and as long as a connection has already been made, you can run queries. Best of all It has prepared statements thanks to Wrapping MySQLi. For documentation and download please visit Gorilla Labs
1 comments
Db is a connection manager, it will use the connection you first create throughout your application. You can continue to call Db:init() and as long as a connection has already been made, you can run queries. Best of all It has prepared statements thanks to Wrapping MySQLi. For documentation and download please visit Gorilla Labs
//-- Prepared Statements // Execute a query $db->prepare('SELECT * FROM blog_entries WHERE id=?')->bind(23)->query(); // or $db->prepare(SELECT * FROM blog_entries WHERE id=?); $db->bind(23); $db->query(); // you can also stack binds $db->prepare('SELECT * FROM blog_entries WHERE id=? and title=?') ->bind(23)->bind('Hello World')->query(); $db->prepare('SELECT * FROM blog_entries WHERE id=? and title=?') $entry = $db->fetchOne(); // you can do can use them all in the same parameter $db->prepare('SELECT * FROM events WHERE name LIKE ? OR name LIKE ? and location LIKE ?') ->bind('%My%', '%Event%', '%San%') ->query(); // you can do can use an array and if they have keys you can assign you ?'s to them $db->prepare('SELECT * FROM events WHERE name LIKE ?arg1 OR name LIKE ?arg2 and location LIKE ?') ->query(); // a cleaner example notice they can be in any order but you probably shouldn't $db->prepare('SELECT * FROM events WHERE name LIKE ?arg1 OR name LIKE ?arg2 and location LIKE ?') ->query(); // and yes keys can be used multiple times $db->prepare('SELECT * FROM events WHERE name LIKE ?arg1 OR name LIKE ?arg1 and location LIKE ?') ->query();
1 comments
Wordpress Plugin Development: Not so bad?
Saturday, August 2, 2008
When I had tried Drupal for size the admin area was so complex I'd never recommend it to a sane person. Wordpress has a much more simple admin area, but whats impressed me is how simple their API is. I wish it was more documented, but its given me a good run.
Wordpress's Front-end (What everyone else see) is crap. But its Back-end is not half bad. I get access to their editor, users and so on.
So later this week I'll post a really clean plugin so you can all agree with me :)
0 comments
Wordpress's Front-end (What everyone else see) is crap. But its Back-end is not half bad. I get access to their editor, users and so on.
So later this week I'll post a really clean plugin so you can all agree with me :)
0 comments