Texting with PHP

Ever needed to text someone through PHP? It’s very simple! Most cellular providers give an email address with your phone number that can be used to text that number. This is known as “email to SMS gateway”. For example if my number is (555) 555-5555 and I used AT&T then emailing 5555555555@txt.att.net would send a text to my phone. So you can probably guess how the PHP looks, but here’s an example:

<?php
    $from = "Lil Josh <info@liljosh.com>"; // sender
    $subject = "Subject appears in the text message";
    $message = "This is the contents of the text";
    
    // send text message
    mail("5555555555@vtext.com",$subject,$message,"From: $from\n"); // Verizon
    mail("5555555555@txt.att.net",$subject,$message,"From: $from\n"); // AT&T
    mail("5555555555@tmomail.net",$subject,$message,"From: $from\n"); // T-Mobile
    mail("5555555555@vmobl.com",$subject,$message,"From: $from\n"); // Virgin Mobile
    mail("5555555555@messaging.sprintpcs.com",$subject,$message,"From: $from\n"); // Sprint
    mail("5555555555@messaging.nextel.com",$subject,$message,"From: $from\n"); // Nextel
?>

For a more complete list of providers visit http://www.ukrainecalling.com/email-to-text.aspx

Recursion in PHP

Using recursion can be a little hard to grasp if you are new to the concept. In it’s simplest form it’s a function that calls upon itself to create a loop. It is commonly used for traversing tree structures. This example shows PHP using recursion to get a list of all the child pages under a folder structure.


<?php

function getPages($folder, $pages=array()) {
  //function returns everything in the folder 1 level
  $assets = getChildren($folder); 

  foreach($assets as $asset) {
    if($asset->type == "folder") {
      $pages = getPages($asset->path, $pages);
    } else if ($asset->type == "page") {
      array_push($pages, $asset->name);
    }
  }

  return $pages;
}

?>

recursion

jQuery Mobile Dynamicly Appending to Listview via AJAX issue

With jQuery Mobile you can create a Listview of list items but I wanted to populate that list dynamically via AJAX. The problem I was getting is it would work fine when I loaded it the first time but then I wanted to nuke (clear) the list and repopulate it with new content. If you’ve gone through half the trouble I went through to get this to work then I know this is going to help you out! So I won’t drag this out any longer… here’s the solution:

//empty the list
$("#myList").empty();

//do some stuff to populate the content
myListContent += '<li><a>some content</a></li>';

//append the new content and create a new listview and refresh it
$("#myList").append(myListContent).listview().trigger("create");
$("#myList").listview('refresh');

timthumb and PHP thumbnails rotate with large images

I’ve been banging my head against the wall trying to figure out why large images were getting rotated when I made them into a thumbnail (either through timthumb or PHP’s imagejpeg). It turns out that with images that were in portrait the width and the height were swapped for very high resolution images (~3000 pixels wide and ~ 2000 pixels tall). So I wrote my own function that looks to see if the EXIF information says this is a portrait image (which is defined by the integer value 6). If it is then I check to see if the width is greater than the height. If so – it’s going to end up in landscape so I simply rotate the image 270 degrees (counter clockwise) and it works! So I’ve made the function to take two arguments; a path to the image, and the desired width of the thumbnail. So here it is:


header('Content-Type:image/jpeg');

//can be called like this:
//script.php?w=200&src=img/myPicture.jpg
echo thumb($_GET["src"],$_GET["w"]);

function thumb( $path, $thumbWidth )
{

 $info = pathinfo($path);
 // continue only if this is a JPEG image
 if ( strtolower($info['extension']) == 'jpg' )
 {

// load image and get image size
 $img = imagecreatefromjpeg( "{$path}" );
 $width = imagesx( $img );
 $height = imagesy( $img );

// calculate thumbnail size
 $new_width = $thumbWidth;
 $new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new temporary image
 $tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
 imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
 $exif = exif_read_data($_GET["src"]);
 $orientation = $exif["Orientation"]; //6 for portrait and 1 for landscape
 if($orientation == 6 && $width > $height) {
 $tmp_img = imagerotate($tmp_img, 270, 0);
 }
 return imagejpeg($tmp_img);

 }
}