So my wife had a desire to post some photos we have in our gallery into a comment on myspace, not to argue the quality or value of myspace I started to attempt to give her instructions. I came to two roadblocks…
1. img tags look intimating, she doesn’t want have to remember all that.
2. Simply hot linking the image from my gallery install doesn’t work because myspace chokes when you have an img tag with too long of a URL.
So I solved this problem like this…
1. create a .htaccess file that uses mod_rewrite to shorten the URLs to the pictures (Since the standard install has URLs to the images such as http://example.com/photos/main.php?g2_view=core.DownloadItem&g2_itemId=110&g2_serialNumber=2)
2. create a small PHP file that takes the URL to the photo and spits back the “myspace code” (shutter) to paste into a comment.
The setup…
I first created a new folder off of the root (’/') of my website and called it ‘myspace’. This creates a URL path like http://example.com/myspace.
The .htaccess…
Next, in the myspace/ directory I created a .htaccess file that uses mod_rewrite to shorten the urls to the photos using the g2_itemId.
RewriteEngine On
RewriteRule ^([0-9]+).jpg /photos/main.php?g2_view=core.DownloadItem&g2_itemId=$1&g2_serialNumber=1 [QSA,L]
What this does is take any request to http://example.com/myspace/1234.jpg and translate it in the server to http://example.com/photos/main.php?g2_view=core.DownloadItem&g2_itemId=1234&g2_serialNumber=1 thus bypassing the character limit for a URL in myspace.
Next up I had to create a simple PHP script that our generate the tag automatically for her. The simplest way I could come up with was having her right click (well Ctrl-Click, we use macs
) on the image in the gallery and do a “copy image address” then paste the copied URL into the script from there. There probably was the route of creating a plugin for gallery, but I really didn’t want to spend more time than it took me to eat lunch, so lets get onto the PHP code…
(I cannot get wordpress to not mangle the PHP code, so used the linked version below)
So what this does is actually quite simple. It starts off with a form with an input box to paste the URL to the image that was copied earlier and when you submit the form it spits back the string of HTML to copy into myspace and displays the photo below it. Oh and I named the file index.php so that when she only has to remember the http://example.com/myspace URL when she wants to post photos. I could have done the rewrite portion in PHP also and kept this all down to one script, but since these images have the potential to get a large number of hits from being posted on myspace I chose to let apache do all the heavy lifting alone which it is good at.
Now since wordpress probably mangled the source to both of files I posted you can download the raw forms here and here.
Post a Comment