here is a nice simple function that uses preg_replace to clean a string, and make sure it returns only normal characters.
It is another way of cleaning a string, similar to the method i used in Generate Safe File Name in PHP using ereg_replace function.
Not sure really which is better i think one of them may be faster than the other. Please feel free to comment and let me know.
return only normal characters (a-z 0-9) php function using preg_replace
Showing posts with label code. Show all posts
Showing posts with label code. Show all posts
Wednesday, June 22, 2011
Friday, December 11, 2009
PHP – Automated Creation of Thumbnails
[PHP CODE]
// define the base image dir
$base_img_dir = "./";
// $QUERY_STRING =
// f(3c9b5fa6bc0fa) img_file
// w(123|15%) width of output
// h(123|10%) height of output
// x(123) max width of output
// y(123) max height of output
// t(jpg|png) type of output
// q(100) quality of jpeg
// find tags
preg_match_all("/\+*(([a-z])\(([^\)]+)\))\+*/", $QUERY_STRING,
$matches, PREG_SET_ORDER);
// empty array and set regular expressions for the check
$tags = array();
$check = array( "f" => "[0-9a-zA-Z]{13}",
"w" => "[0-9]+%?",
"h" => "[0-9]+%?",
"x" => "[0-9]+",
"y" => "[0-9]+",
"t" => "jpg|png",
"q" => "1?[0-9]{1,2}" );
// check tags and save correct values in array
for ($i=0; $i if (isset($check[$matches[$i][2]])) {
if (preg_match('/^('.$check[$matches[$i][2]].')$/',
$matches[$i][3])) {
$tags[$matches[$i][2]] = $matches[$i][3];
}
}
}
function notfound() {
header("HTTP/1.0 404 Not Found");
exit;
}
// check that filename is given
if (!isset($tags["f"])) {
notfound();
}
// check if file exists
if (!file_exists($base_img_dir.$tags["f"])) {
notfound();
}
// retrieve file info
$imginfo = getimagesize($base_img_dir.$tags["f"]);
// load image
switch ($imginfo[2]) {
case 2: // jpg
$img_in = imagecreatefromjpeg($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "jpg";
}
break;
case 3: // png
$img_in = imagecreatefrompng($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "png";
}
break;
default:
notfound();
}
// check for maximum width and height
if (isset($tags["x"])) {
if ($tags["x"] < imagesx($img_in)) {
$tags["w"] = $tags["x"];
}
}
if (isset($tags["y"])) {
if ($tags["y"] < imagesy($img_in)) {
$tags["h"] = $tags["y"];
}
}
// check for need to resize
if (isset($tags["h"]) or isset($tags["w"])) {
// convert relative to absolute
if (isset($tags["w"])) {
if (strstr($tags["w"], "%")) {
$tags["w"] = (intval(substr($tags["w"], 0, -1)) / 100) *
$imginfo[0];
}
}
if (isset($tags["h"])) {
if (strstr($tags["h"], "%")) {
$tags["h"] = (intval(substr($tags["h"], 0, -1)) / 100) *
$imginfo[1];
}
}
// resize
if (isset($tags["w"]) and isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $tags["h"];
} elseif (isset($tags["w"]) and !isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $imginfo[1] * ($tags["w"] / $imginfo[0]);
} elseif (!isset($tags["w"]) and isset($tags["h"])) {
$out_w = $imginfo[0] * ($tags["h"] / $imginfo[1]);
$out_h = $tags["h"];
} else {
$out_w = $tags["w"];
$out_h = $tags["h"];
}
// new image in $img_out
$img_out = imagecreate($out_w, $out_h);
imagecopyresized($img_out, $img_in, 0, 0, 0, 0, imagesx($img_out),
imagesy($img_out), imagesx($img_in), imagesy($img_in));
} else {
// no resize needed
$img_out = $img_in;
}
// check for a given jpeg-quality, otherwise set to default
if (!isset($tags["q"])) {
$tags["q"] = 75;
}
// returning the image
switch ($tags["t"]) {
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($img_out, "", $tags["q"]);
exit;
case "png":
header("Content-type: image/png");
imagepng($img_out);
exit;
default:
notfound();
}
[PHP CODE]
cant get the dam thing to show properly, grab the source here: Elgene
// define the base image dir
$base_img_dir = "./";
// $QUERY_STRING =
// f(3c9b5fa6bc0fa) img_file
// w(123|15%) width of output
// h(123|10%) height of output
// x(123) max width of output
// y(123) max height of output
// t(jpg|png) type of output
// q(100) quality of jpeg
// find tags
preg_match_all("/\+*(([a-z])\(([^\)]+)\))\+*/", $QUERY_STRING,
$matches, PREG_SET_ORDER);
// empty array and set regular expressions for the check
$tags = array();
$check = array( "f" => "[0-9a-zA-Z]{13}",
"w" => "[0-9]+%?",
"h" => "[0-9]+%?",
"x" => "[0-9]+",
"y" => "[0-9]+",
"t" => "jpg|png",
"q" => "1?[0-9]{1,2}" );
// check tags and save correct values in array
for ($i=0; $i
if (preg_match('/^('.$check[$matches[$i][2]].')$/',
$matches[$i][3])) {
$tags[$matches[$i][2]] = $matches[$i][3];
}
}
}
function notfound() {
header("HTTP/1.0 404 Not Found");
exit;
}
// check that filename is given
if (!isset($tags["f"])) {
notfound();
}
// check if file exists
if (!file_exists($base_img_dir.$tags["f"])) {
notfound();
}
// retrieve file info
$imginfo = getimagesize($base_img_dir.$tags["f"]);
// load image
switch ($imginfo[2]) {
case 2: // jpg
$img_in = imagecreatefromjpeg($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "jpg";
}
break;
case 3: // png
$img_in = imagecreatefrompng($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "png";
}
break;
default:
notfound();
}
// check for maximum width and height
if (isset($tags["x"])) {
if ($tags["x"] < imagesx($img_in)) {
$tags["w"] = $tags["x"];
}
}
if (isset($tags["y"])) {
if ($tags["y"] < imagesy($img_in)) {
$tags["h"] = $tags["y"];
}
}
// check for need to resize
if (isset($tags["h"]) or isset($tags["w"])) {
// convert relative to absolute
if (isset($tags["w"])) {
if (strstr($tags["w"], "%")) {
$tags["w"] = (intval(substr($tags["w"], 0, -1)) / 100) *
$imginfo[0];
}
}
if (isset($tags["h"])) {
if (strstr($tags["h"], "%")) {
$tags["h"] = (intval(substr($tags["h"], 0, -1)) / 100) *
$imginfo[1];
}
}
// resize
if (isset($tags["w"]) and isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $tags["h"];
} elseif (isset($tags["w"]) and !isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $imginfo[1] * ($tags["w"] / $imginfo[0]);
} elseif (!isset($tags["w"]) and isset($tags["h"])) {
$out_w = $imginfo[0] * ($tags["h"] / $imginfo[1]);
$out_h = $tags["h"];
} else {
$out_w = $tags["w"];
$out_h = $tags["h"];
}
// new image in $img_out
$img_out = imagecreate($out_w, $out_h);
imagecopyresized($img_out, $img_in, 0, 0, 0, 0, imagesx($img_out),
imagesy($img_out), imagesx($img_in), imagesy($img_in));
} else {
// no resize needed
$img_out = $img_in;
}
// check for a given jpeg-quality, otherwise set to default
if (!isset($tags["q"])) {
$tags["q"] = 75;
}
// returning the image
switch ($tags["t"]) {
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($img_out, "", $tags["q"]);
exit;
case "png":
header("Content-type: image/png");
imagepng($img_out);
exit;
default:
notfound();
}
[PHP CODE]
cant get the dam thing to show properly, grab the source here: Elgene
Sunday, November 8, 2009
Dont get Rick Rolled on you iPhone, Change the Password
If you’re one of the many who are jailbreaking your iPhone to get options such as tethering, make sure you change the root access password once you do.
In addition to your possibly getting Rick-Rolled
Your jailbroken phone could possibly be held for ransom
If you’ve never changed the default device password, now’s the time. Here’s how:
The app to use on the iPhone is called MobileTerminal and it’s available for free in the Cydia store.
Once you have MobileTerminal installed, launch it and you should see a prompt saying this or similar:
iPhoneName: ~ Mobile$
- At that prompt, type: passwd
- You’ll be prompted for the ‘old’ (current) password for the mobile user. Enter this as the old password: alpine
- You’ll then be prompted to enter the new password – so just type in your desired new password. Use good password principles if possible (long and stong). You will not see characters appearing on the screen as you type – that’s normal, not a concern.
- You’ll then be prompted to re-enter the new password. Do that.
- You should then be returned to the Mobile$ prompt that you started on when opening the MobileTerminal app. There’s no success message to say the password was changed – but if you’re returned to the prompt and do not get an error, the change was successful. And you’re done with change for the mobile account.
- The second primary admin account for the iPhone is called root – so now you need to change that as well.
- Type this to switch to the root user: login root
- You’ll be prompted for the root user’s current password. Enter this: alpine
- Type this to start the password change routine again: passwd
- Enter the old password for root (it is ‘alpine’, same as for the mobile user) and enter your desired new password twice, just as you did for the mobile account
Subscribe to:
Posts (Atom)
