BigToach.com

PHP Snippets

« 1 2 3 4 » ... 11

sendmail

Information

(PHP Version >= 3)
Functions used in this snippet: trim, preg_math, rand, strip_tags, preg_replace, chunk_split, base64_encode, is_array, file_exists, pathinfo, file_get_contents, mail, implode.

Description

boolean sendmail( string to, string subject, string body, mixed file, string from, string additional )

Simply put, this function sends mail. It checks to see if the body is in html format, if so it will send it with the correct headers to be received as html. It also sends a plain text alternative for older browsers.

If the optional file is supplied, the function will send this file (or files if file is of type array) as an attachment.

You can also specify who the email should be from as well as the reply-to address.

If you need additional settings, you can pass them as mail headers in the additional parameter.

Snippet

<?php
function sendmail($to$subject$body$file=NULL$from='noreply@yourdomain.com'$additional='')
{
    
$boundary 'SPROUT-19_16_18_15_21_20';
    
$headers = array();
    
$headers[] = 'From: ' $from;
    
$headers[] = 'Reply-To: ' $from;
    if(
trim($additional) !== '')
    {
        
$headers[] = $additional;
    }
    
$headers[] = 'MIME-Version: 1.0';
    
$headers[] = 'Content-Type: multipart/mixed; boundary="' $boundary '"';
    
$headers[] = 'Content-Transfer-Encoding: 7bit' "\r\n";
    
$headers[] = "> This is a multi-part message in MIME format.\r\n";
    
$headers[] = '--' $boundary;
    
// if there is html in the message, send it with the right settings and an alternative for crappy browsers
    
if(preg_match('/\<(html|body)/ims'$body))
    {
        
$htmlbound 'SPROUT-' rand(100,9999);
        
$headers[] = 'Content-Type: multipart/alternative; boundary="' $htmlbound '"' "\r\n";
        
$headers[] = '--' $htmlbound;
        
$headers[] = 'Content-Type: text/plain; charset="iso-8859-1"';
        
$headers[] = 'Content-Transfer-Encoding: 7bit' "\r\n";
        
$headers[] = strip_tags(preg_replace('/\<head\>(.+?)\<\/head\>/ims'''$body)) . "\r\n\r\n";
        
$headers[] = '--' $htmlbound;
        
$headers[] = 'Content-Type: text/html; charset="iso-8859-1"';
        
$headers[] = 'Content-Transfer-Encoding: base64' "\r\n";
        
$headers[] = chunk_split(base64_encode($body));
        
$headers[] = "\r\n\r\n--" $htmlbound "--\r\n\r\n--" $boundary;
    }
    else 
    {
        
$headers[] = 'Content-Type: text/plain; charset="iso-8859-1"';
        
$headers[] = 'Content-Transfer-Encoding: 7bit' "\r\n";
        
$headers[] = $body "\r\n\r\n";
        
$headers[] = '--' $boundary;
    }
    
    
// attach files if they are there
    
if($file !== NULL)
    {
        
$files = (!is_array($file)) ? array($file) : $file;
            
        foreach(
$files as $attachment)
        {
            if(!
file_exists($attachment))
                continue;
            
$path pathinfo($attachment);
            
$headers[] = 'Content-Type: application/octet-stream; name="' $path['basename'] . '"';
            
$headers[] = 'Content-Disposition: attachment; filename="' $path['basename'] . '"';
            
$headers[] = "Content-Transfer-Encoding: base64\r\n";
            
$attach chunk_split(base64_encode(file_get_contents($attachment)));
            
$headers[] = $attach;
            
$headers[] = '--' $boundary;
        }
    }
    return 
mail($to$subject''implode("\r\n"$headers) . '--');
}
?>

Example

sendmail Can be used in the following way:

<?php

// Send an html file
sendmail('someone@domain.ext''Title''HTML CONTENT HERE');

// Send an email with an attachment
sendmail('someone@domain.ext''Title''Check out this picture''image.jpg');

// Send an html style email with multiple attachments
sendmail('someone@domain.ext''Title''HTML GOES HERE', array('product.jpg''productdetails.pdf'));

// Send a plaintext email from joeschmoe@domain.ext
sendmail('someone@domain.ext''Title''PLAIN TEXT GOES HERE'NULL'joeschmoe@domain.ext');

?>

Added on Nov 16th at 7 pm by Scott - 2 Comments
Updated on Feb 22nd at 7 pm.


excerpt

Information

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

Description

str excerpt( str excerpt, int length )

Returns an excerpt of the string excerpt with a maximum length of length. This function is useful if you have HTML in the excerpt because it will keep the HTML structure intact. It does this by keeping the HTML tags in the excerpt, and appending the empty HTML tags at the end of the excerpt.

Snippet

<?php
function excerpt($excerpt$length)
{
    
preg_match_all('/<[^>]+>/'$excerpt$matches);
    
$excerpt preg_replace('/<[^>]+>/''|!|'$excerpt);
    
$excerpt substr($excerpt0strrpos(substr($excerpt0$length), ' '));
    
$exhausted false;
    foreach(
$matches[0] as $match)
    {
        if(
strpos($excerpt'|!|') !== false)
        {
            
$excerpt preg_replace('/\|\!\|/'$match$excerpt1);
        }
        else 
        {
            if(!
$exhausted)
            {
                
$excerpt .= '...';
                
$exhausted true;
            }
            
$excerpt .= $match;
        }
    }
    return 
$excerpt;
}
?>

Example

excerpt Can be used in the following way:

<?php
$str 
'<p>Hello <a href="http://www.bigtoach.com">Test</a> This is just a test to see if <strong>Nested</strong> <em>Tags</em> <em>Will work</em></p>';

echo 
excerpt($str80);

// Produces The Following
// <p>Hello <a href="http://www.bigtoach.com">Test</a> This is just a test to see if <strong>Nested</strong> <em>Tags</em>...<em></em></p>
?>

Added on Jun 12th at 2 pm by Scott - 0 Comments


imagecopyresamplebicubic

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_imgimagecolorat($src_img$ot $p$ow $u));
                    
$r += $c['red'];
                    
$g += $c['green'];
                    
$b += $c['blue'];
                    
$a++;
                }
            }
            
imagesetpixel($dst_img$x$yimagecolorclosest($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(100100); // create an empty image
imagecopyresamplebicubic($dest$img0000100100750750);
header('Content-type: image/png');
imagepng($dest);
?>

Added on Aug 31st at 3 pm by Scott - 0 Comments


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($fmgtrue);
    
imagecopy($fmg$img0000$ix$iy);
    for(
$sx 0$sx $ix$sx += $wx)
    {
        for(
$sy 0$sy $iy$sy += $wy)
        {
            
imagecopy($fmg$wmg$sx$sy00min($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);
?>

Added on Jan 9th at 8 pm by Scott - 2 Comments
Updated on Jan 9th at 8 pm.


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
?>

Added on Oct 23rd at 9 am by Scott - 4 Comments


« 1 2 3 4 » ... 11