BigToach.com

PHP Snippets

« 1 2 3 4 5 »

image_inverse

Information

(PHP Version >= 4.0.6)
Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecolorat, imagesetpixel, imagecolorallocate.

Description

int image_inverse( int img )

Makes the image img have all of its colors inversed.

*Note: This is a fairly slow function so it would be best to do it once and save the file for multiple viewings.

Snippet

<?php
function image_inverse($img){
    
$x imagesx($img);
    
$y imagesy($img);
    
$dest imagecreatetruecolor($x$y);
    for(
$i=0$i<$x$i++){
        for(
$j=0$j<$y$j++){
            
$rgb imagecolorat($img$i$j);
            
$r = ($rgb >> 16) & 0xFF;
            
$g = ($rgb >> 8) & 0xFF;
            
$b $rgb 0xFF;
            
imagesetpixel($dest$i$jimagecolorallocate($dest, (255 $r), (255 $g), (255 $b)));
        }
    }
    return 
$dest;
}
?>

Example

image_inverse Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('someimage.jpg'); // create image

$img image_inverse($img); // inverses image

header('Content-type: image/jpeg'); // send header

imagejpeg($img); // output image to browser

?>

Added on Jul 6th at 8 pm by Scott - 0 Comments
Updated on Aug 3rd at 4 am.


rotate_image

Information

(PHP Version >= 3)
Functions used in this snippet: imagerotate.

Description

int rotate_image( int img[, int degrees[, bool cw]] )

Uses the image img and rotates it according to degrees clockwise. If cw is set to false the function rotates the images in a counter clockwise motion.

Snippet

<?php
function rotate_image($img$degrees='90'$cw=true){
    
$degrees $cw $degrees : (360 $degrees);
    switch(
$degrees){
        case 
'90':
            
$img imagerotate($img900);
        break;
        case 
'180':
            
$img imagerotate($img1800);
        break;
        case 
'270':
            
$img imagerotate($img2700);
        break;
    }
    return 
$img;
}
?>

Example

rotate_image Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('somefile.jpg'); // load image

$img rorate_image($img); // rotates it 90 clockwise

$img rotate_image($img180); // rotates it 180 clockwise

$img rotate_image($img90false); // rotates it 90 counter clockwise

header('Content-type: image/jpeg'); // set header

imagejpeg($img); // send image to browser

?>

Added on Jul 6th at 6 am by Scott - 0 Comments


image_flip

Information

(PHP Version >= 4.0.6)
Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecopy, imagedestroy.

Description

int image_flip( int img[, str type] )

Flips the image img to the direction of type

Snippet

<?php
function image_flip($img$type=''){
    
$width  imagesx($img);
    
$height imagesy($img);
    
$dest   imagecreatetruecolor($width$height);
    switch(
$type){
        case 
'':
            return 
$img;
        break;
        case 
'vert':
            for(
$i=0;$i<$height;$i++){
                
imagecopy($dest$img0, ($height $i 1), 0$i$width1);
            }
        break;
        case 
'horiz':
            for(
$i=0;$i<$width;$i++){
                
imagecopy($dest$img, ($width $i 1), 0$i01$height);
            }
        break;
        case 
'both':
            for(
$i=0;$i<$width;$i++){
                
imagecopy($dest$img, ($width $i 1), 0$i01$height);
            
            }
            
$buffer imagecreatetruecolor($width1);
            for(
$i=0;$i<($height/2);$i++){
                
imagecopy($buffer$dest000, ($height $i -1), $width1);
                
imagecopy($dest$dest0, ($height $i 1), 0$i$width1);
                
imagecopy($dest$buffer0$i00$width1);
            }
            
imagedestroy($buffer);
        break;
    }
    return 
$dest;
}
?>

Example

image_flip Can be used in the following way:

<?php
$img 
imagecreatefromjpeg('somefile.jpg'); // create an image
$img image_flip($img'vert'); // flips image vertically

$img image_flip($img'horiz'); // flips image horizontally

$img image_flip($img'both'); // flips image both ways

header('Content-type: image/jpeg'); // send header
imagejpeg($img); // send image to browser
?>

Added on Jul 6th at 5 am by Scott - 0 Comments


get_ext

Information

(PHP Version >= 3)
Functions used in this snippet: substr, strrpos.

Description

str get_ext( str str[, bool ext] )

If ext is left as false, this function returns str with its file extension taken away.
If ext is set to true, only the extension is returned.

Snippet

<?php
function get_ext($str,$ext=false){
    if(
$ext){
        return 
substr($str,strrpos($str,'.')+1);
    }
    return 
substr($str,0,strrpos($str,'.'));
}
?>

Example

get_ext Can be used in the following way:

<?php
echo get_ext('somefile.php'); // somefile

echo get_ext('package.zip'true// zip
?>

Added on Jul 6th at 5 am by Scott - 0 Comments


parse_links

Information

(PHP Version >= 3.0.9)
Functions used in this snippet: ceil, preg_match_all, strlen, substr, preg_match, str_replace, preg_replace.

Description

str parse_links( str str[, int len[, str mid]] )

Adds around any links inside of a area of text. It works with [url], [url=site], and just plain http://site.com.

Snippet

<?php
function parse_links($str$len=25$mid='...'){
    
$left ceil(0.6666 $len);
    
$right $len $left;
    
preg_match_all('/(?<!=|\]|\/)((https?|ftps?|irc):\/\/|' '(www([0-9]{1,3})?|ftp)\.)([0-9a-z-]{1,25}' '[0-9a-z]{1}\.)([^\s&\[\{\}\]]+)/ims'$str$matches);
    foreach(
$matches[0] as $key=>$value){
        
$temp $value;
        if(
strlen($value) > ($len strlen($mid) + 2)){
            
$value substr($value0$left) . $mid substr($value,(-$right));
        }
        
$temp = !preg_match('/:\/\//'$temp) ? (substr($temp03) === 'ftp' 'ftp://' $temp 'http://' $temp) : $temp;
        
$temp $temp === $matches[0][$key] && $value === $matches[0][$key] ? '' '=' $temp;
        
$str str_replace($matches[0][$key],'[url' $temp ']' $value '[/url]'$str);
    }
    
$str preg_replace('/\[url=(?!http|ftp|irc)/ims''[url=http://'$str);
    
$str preg_replace('/\[url\](.+?)\[\/url\]/ims','<a href="$1" title="$1">$1</a>',$str);
    
$str preg_replace('/\[url=(.+?)\](.+?)\[\/url\]/ims''<a href="$1" title="$1">$2</a>'$str);
    return 
$str;
}
?>

Example

parse_links Can be used in the following way:

<?php
echo parse_links('http://www.site.com'); 
// <a href="http://www.site.com">http://www.site.com</a>

echo parse_links('www.cropp.in'); 
// <a href="http://www.cropp.in">www.cropp.in</a>

echo parse_links('ftp://somesite.com');
// <a href="ftp://somesite.com">ftp://somesite.com</a>

echo parse_links('[url]www.cropp.in[/url]');
// <a href="http://www.cropp.in">www.cropp.in</a>

echo parse_links('[url=www.cropp.in]Cropp.in[/url]');
// <a href="http://www.cropp.in">Cropp.in</a>

echo parse_links'http://www.somesite.com/with/a/really/long/url/link' );
// <a href="http://www.somesite.com/with/a/really/long/url/link"> http://www.some...url/link</a>
?>

Added on Jul 6th at 1 am by Scott - 1 Comment
Updated on Jul 6th at 1 am.


« 1 2 3 4 5 »