PHP Snippets
« 1 2 3 4 » ... 10imagecopyresamplebicubic
Information
(PHP Version >= 4.0.1)Functions used in this snippet: imagepalettecopy, round, imagecolorsforindex, imagecolorat, imagesetpixel, imagecolorclosest.
Description
int imagecopyresamplebicubic( int &dst_img, int &src_img, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
This function will resize images using a Bicubic method. This makes the resized images look fabulous, but as a result is extremely slow. It takes the same arguements as imagecopyresized.
I must note that I did NOT write this function. I found it on php.net at http://www.php.net/manual/en/function.imagecopyresampled.php#44490
Snippet
<?php
function imagecopyresamplebicubic(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
imagepalettecopy($dst_img, $src_img);
$rX = $src_w / $dst_w;
$rY = $src_h / $dst_h;
$w = 0;
for($y = $dst_y; $y < $dst_h; $y++)
{
$ow = $w; $w = round(($y + 1) * $rY);
$t = 0;
for($x = $dst_x; $x < $dst_w; $x++)
{
$r = $g = $b = 0; $a = 0;
$ot = $t; $t = round(($x + 1) * $rX);
for($u = 0; $u < ($w - $ow); $u++)
{
for($p = 0; $p < ($t - $ot); $p++)
{
$c = imagecolorsforindex($src_img, imagecolorat($src_img, $ot + $p, $ow + $u));
$r += $c['red'];
$g += $c['green'];
$b += $c['blue'];
$a++;
}
}
imagesetpixel($dst_img, $x, $y, imagecolorclosest($dst_img, $r / $a, $g / $a, $b / $a));
}
}
}
?>
Example
imagecopyresamplebicubic Can be used in the following way:
<?php
$img = imagecreatefrompng('someimage'); // image is 750x750
$dest = imagecreatetruecolor(100, 100); // create an empty image
imagecopyresamplebicubic($dest, $img, 0, 0, 0, 0, 100, 100, 750, 750);
header('Content-type: image/png');
imagepng($dest);
?>
image_watermark
Information
(PHP Version >= 3)Functions used in this snippet: imagecreatefromjpeg, imagesx, imagesy, imagecreatefrompng, imagecreatetruecolor, imagealphablending, imagecopy, min.
Description
resource image_watermark( resource img, resource wmg )
Creates a watermark layover image over an image. The watermark image will be repeated on both the x and y axis. Using an image that will work as a seamless repeating image is best.
If the watermark is in PNG format and has its alpha transparceny set, the final watermark layover will be the same opacity as the watermark image.
Snippet
<?php
function image_watermark($img, $wmg)
{
$ix = imagesx($img);
$iy = imagesy($img);
$wx = imagesx($wmg);
$wy = imagesy($wmg);
$fmg = imagecreatetruecolor($ix, $iy);
imagealphablending($fmg, true);
imagecopy($fmg, $img, 0, 0, 0, 0, $ix, $iy);
for($sx = 0; $sx < $ix; $sx += $wx)
{
for($sy = 0; $sy < $iy; $sy += $wy)
{
imagecopy($fmg, $wmg, $sx, $sy, 0, 0, min($ix - $sx, $wx), min($iy - $sy, $wy));
}
}
return $fmg;
}
?>
Example
image_watermark Can be used in the following way:
<?php
// make our image into a php resource
$img = imagecreatefromjpeg('yourimage.jpg');
// make our watermark into a php resource
$wmg = imagecreatefrompng('your_watermark.png');
// apply the watermark
$fmg = image_watermark($img, $wmg);
// send the image to the browser
imagejpeg($fmg);
?>
find_functions
Information
(PHP Version >= 4)Functions used in this snippet: preg_replace, preg_match_all, in_array, implode.
Description
str find_functions( str str )
Finds every function in the string str. It returns a string of the functions seperated by commas with no spaces.
Snippet
<?php
function find_functions($str)
{
$str = preg_replace('/function ([a-z0-9_]+)\s?\(/ims','',$str);
preg_match_all('/([a-z0-9_]+)\s?\(/ims',$str,$matches);
$reserved = array('if', 'foreach', 'for', 'do', 'while', 'array', 'print', 'switch', 'echo');
$ret = array();
foreach($matches[1] as $key=>$function)
{
if(!in_array($function, $reserved))
{
$ret[] = $function;
$reserved[] = $function;
}
}
return implode(',',$ret);
}
?>
Example
find_functions Can be used in the following way:
<?php
$str = '$var = str_replace("haha", "wee", "haha wee");
print_r($_SERVER);
echo substr ($_SERVER["REMOTE_ADDR"], 0, strpos($_SERVER["REMOTE_ADDR"], ".") -1);
echo pi();';
echo find_functions($str);
// returns the following
// str_replace,print_r,substr,strpos,pi
?>
array_natcase
Information
(PHP Version >= 3)Functions used in this snippet: natcasesort, array_reverse.
Description
array array_natcase( str key, array array[, str ascdesc] )
Sorts the associative array array by the associative array key key with natcasesort. It maintains all keys and can be sorted ascending or descending by setting ascdesc to either asc, or desc. The function returns the newly array sorted array.
Snippet
<?php
function array_natcase($key, $array, $ascdesc='asc')
{
$temp = array();
$final = array();
foreach($array as $id=>$value)
{
$temp[$id] = $value[$key];
}
natcasesort($temp);
foreach($temp as $id=>$value)
{
$final[$id] = $array[$id];
}
if($ascdesc{0} === 'd')
{
$final = array_reverse($final, true);
}
return $final;
}
?>
Example
array_natcase Can be used in the following way:
<?php
$array = array(
'a'=>array('x'=>10, 'other'=>'egh'),
'q'=>array('x'=>9, 'other'=>'blah'),
'7'=>array('x'=>1, 'other'=>'rawr'),
'6'=>array('x'=>22, 'other'=>'abcd'));
$array = array_natcase('x', $array);
//outputs the array in the following order
// 7, q, a, 6
?>
more_nums
Information
(PHP Version >= 3)Functions used in this snippet: strlen, str_replace.
Description
bool more_nums( mixed str )
Returns true if the given string str contains more numbers than characters. Returns false if not.
Snippet
<?php
function more_nums($str){
$len = strlen($str);
$str = str_replace(array(0,1,2,3,4,5,6,7,8,9), '', $str);
if( ( strlen($str) / $len ) < 0.5)
{
return true;
}
return false;
}
?>
Example
more_nums Can be used in the following way:
<?php
$str = 'abcd123';
if(more_nums($str))
{
echo 'More Numbers';
}
else
{
echo 'Less Numbers';
}
$str = 'ab12345cd6';
if(more_nums($str))
{
echo 'More Numbers';
}
?>
« 1 2 3 4 » ... 10