Turns out I wasn't able to modify rookwood101's script to fit my needs, so I had to recreate it. Now I'm just testing my new feature. Please try liking/unliking and give feedback!
For the curious ones, the final aim of this project is an almost automatic adding of the like button to every post. Currently, I just have to click on my Chrome extension after I posted and it gives me the code. This could evolve into a one-click feature!
You may see the source 4 posts down. Happy liking!
Last edited by LS97 (2011-10-23 16:20:57)
Offline
It works well!
Code, pwease?
Offline
Works on Firefox
Offline
Source Code
Setup of folder:
- create a main folder called whatever you want
- create subfolder named 'likes'
- add font.ttf, likeBG.png and unlikeBG.png to the main folder
- add index.php to the main folder!
font.ttf: download
likeBG: download
unlikeBG: download
index.php contents:
<?
/* Post-Liker:
An appliccation which allows you to dynamically like and unlike posts on the Scratch forums!
*/
function printImage($hasAlreadyLiked, $likesCount) {
// Print small like/unlike image
if($hasAlreadyLiked === true) {
$img = imagecreatefrompng('unlikeBG.png');
} else {
$img = imagecreatefrompng('likeBG.png');
}
$text_colour = imagecolorallocate($img, 1, 1, 180);
$font = realpath('font.ttf');
imagettftext($img, 11, 0, 10, imagefontheight(7), $text_colour, $font, $likesCount . " likes");
$whit = imagecolorallocate($img, 0, 0, 0);
imagepng($img);
imagecolortransparent($img, $whit);
header('Content-type: image/png');
imagedestroy($img);
}
$id = $_GET['id'];
if(is_numeric($id) === false) {
// Post ID is not a number
die;
}
if($_GET['mode'] == 'img') { // Display image with like count
// Find likes count
if(file_exists('likes/' . $id)) {
$arr = explode('/', file_get_contents('likes/' . $id));
if($arr[0] === "") {
unset($arr[0]);
}
$likes = count($arr);
// Find whether user has liked yet
if(in_array($_SERVER['REMOTE_ADDR'], $arr)) {
$hasliked = true;
} else {
$hasliked = false;
}
} else {
$likes = 0;
$hasliked = false;
}
printImage($hasliked, $likes);
}
elseif($_GET['mode'] == 'like') { // Add/remove like from database
// Check whether like exists
if(file_exists('likes/' . $id)) {
// Check whether user has already liked
$arr = explode('/', file_get_contents('likes/' . $id));
if(in_array($_SERVER['REMOTE_ADDR'], $arr)) {
$arr = array_diff($arr, array($_SERVER['REMOTE_ADDR']));
file_put_contents('likes/' . $id, implode('/', $arr));
if(count($arr) === 0) {
unlink('likes/' . $id);
}
} else {
$arr[] = $_SERVER['REMOTE_ADDR'];
file_put_contents('likes/' . $id, implode('/', $arr));
}
} else {
file_put_contents('likes/' . $id, $_SERVER['REMOTE_ADDR']);
}
// Redirect to post
header('Location: http://scratch.mit.edu/forums/viewtopic.php?pid=' . $id . '#p' . $id);
}Last edited by LS97 (2011-10-23 16:21:26)
Offline
LS97 wrote:
Turns out I wasn't able to modify rookwood101's script to fit my needs, so I had to recreate it. Now I'm just testing my new feature. Please try liking/unliking and give feedback!
![]()
http://modshare2.tk/likes/?id=969321&mode=img
For the curious ones, the final aim of this project is an almost automatic adding of the like button to every post. Currently, I just have to click on my Chrome extension after I posted and it gives me the code. This could evolve into a one-click feature!
You may see the source 4 posts down. Happy liking!
Your's works!
Unlike rookwood101's, unfortunately.
Offline
roijac wrote:
make the edges transparent
![]()
working
Right, edges. On it.
Offline
Cool! Sorry I couldn't help, I was on holiday
yours seems to work just fine, and it does it in a different way, without requiring a mysql database!
Offline
Works perfectly, and is quite fast.
(You still don't like databases, do you?
)
Offline
works on chrome os

Offline
Works perfectly!

Offline
Nice.
*likes*
Offline
One-click bookmarklet:
javascript:(prompt("Copy this:", "[url=http://modshare2.tk/likes/?id="+(window.location+"").replace("http://scratch.mit.edu/forums/viewtopic.php?id=", "")+"&mode=like][img]http://modshare2.tk/likes/?id="+(window.location+"").replace("http://scratch.mit.edu/forums/viewtopic.php?id=", "")+"&mode=img[/img][/url]"))Offline