Busy this days

General No Comments »

Unfortunately, I was very busy last few days and I couldn’t write something in my blog. I will be busy until the end of this week and then I can come with new stuff to write about.

CakePHP: upload and resize images

CakePHP 17 Comments »

I searched on Google for a code example of how to upload and resize an image using CakePHP and I didn't get to much results on this, so I decided to make it on my own way. First of all I created the /foto/ directory in the webroot (app/webroot) and inside it other 2 directories, one for the uploaded image /big/ (this is the big version of the image) and one for the image that will be resized /small/ (this can be the thumbnail), be sure that they have write permissions.

Now create a component called image.php and inside it you will have the code that will handle getting the file extension and resizing the uploaded image (app/controllers/components/image.php):

PHP:
  1. class ImageComponent extends Object
  2. {
  3.    function resize_img($imgname,$size)
  4.    {
  5.       //Header("Content-Type: image/jpeg");
  6.       $img_src = ImageCreateFromjpeg ($imgname);
  7.       $true_width = imagesx($img_src);
  8.       $true_height = imagesy($img_src);
  9.  
  10.       if ($true_width>=$true_height)
  11.       {
  12.         $width=$size;
  13.         $height = ($width/$true_width)*$true_height;
  14.       }
  15.       else
  16.       {
  17.        // $height=$size;
  18.        $width=$size;
  19.        $height = ($width/$true_width)*$true_height;
  20.        // $width = ($height/$true_height)*$true_width;
  21.       }
  22.         $img_des = ImageCreateTrueColor($width,$height);
  23.         imagecopyresampled ($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height);
  24.         return $img_des;
  25.     }
  26.  
  27.     function getFileExtension($str) {
  28.  
  29.         $i = strrpos($str,".");
  30.         if (!$i) { return ""; }
  31.         $l = strlen($str) - $i;
  32.         $ext = substr($str,$i+1,$l);
  33.         return $ext;
  34.     }
  35. }

Now let's see the view that will show the input type file element and the form (app/views/controller_name/upload.thtml):

PHP:
  1. <form method="post" enctype="multipart/form-data" action="<?php echo $this->webroot;?>images/add">
  2.     <p>
  3.        <p>Image: &nbsp; &nbsp; &nbsp; &nbsp; <?php echo $html->file('Image/name1', array('size' => '40'))?>
  4.     </p>
  5.     <p>
  6.         <?php echo $html->submit('Upload image'); ?>
  7.     </p>
  8. </form>


At the end comes the last part and the "hardest", when comes to write the controller code (app/controllers/images_controller.php):

PHP:
  1. class ImagesController extends AppController {
  2. var $components = array("Image"); // here is the image component that we described above
  3.  
  4. function add(){
  5.  
  6. if (strlen($this->data['Image']['name1']['name'])>4){
  7.  
  8.                    $error = 0;
  9.                    $uploaddir1 = "foto/big"; // the /big/ directory
  10.                    $uploaddir2 = "foto/small"; // the /small/ directory with resized images
  11.                    $filetype = $this->Image->getFileExtension($this->data['Image']['name1']['name']);
  12.                    $filetype = strtolower($filetype);
  13.  
  14.                    if (($filetype != "jpeg")  && ($filetype != "jpg"))
  15.                    {
  16.                     // verify the extension
  17.                     $error=1;
  18.                    }
  19.                    else
  20.                    {
  21.                     $imgsize = GetImageSize($this->data['Image']['name1']['tmp_name']); // image size
  22.                    }
  23.                    if (($imgsize[0]> 800) || ($imgsize[1]> 600)){
  24.                      // verify to see if the image exceds 800 x 600 px
  25.                      unlink($this->data['Image']['name1']['name']); // delete the image in case is to big
  26.                      $error=1;
  27.                     }
  28.                     if ($error==0){
  29.  
  30.                       // here is generated an unic id for the image name
  31.                       $stamp = strtotime ("now");
  32.                       $orderid = $stamp;
  33.                       $orderid = str_replace(".", "", $orderid);
  34.                       $id_unic = $orderid;
  35.                       $temp = $id_unic;
  36.                       settype($temp,"string");
  37.                       $temp.= ".";
  38.                       $temp.=$filetype;
  39.                       $newfile = $uploaddir1 . "/$temp";
  40.                     if (is_uploaded_file($this->data['Image']['name1']['tmp_name']))
  41.                     {
  42.                         if (!copy($this->data['Image']['name1']['tmp_name'],"$newfile"))
  43.                         {
  44.                             print "Error Uploading File1.";
  45.                             exit();
  46.                         }
  47.                     }
  48.                     $newfile2 = $uploaddir2 . "/$temp";
  49.  
  50.                      $picture_location=$newfile;
  51.                      $size=110; // the size for the resized image
  52.                      $img_des= $this->Image->resize_img($picture_location, $size); //here resizing
  53.                      imagejpeg($img_des,$newfile2,80);
  54.  
  55.                      // here you can have some code for example to insert in the database
  56.                      // Image uploaded
  57.                    }
  58.               }else{
  59.                       // Image not uploaded
  60.               }
  61.      }
  62. }

This is all.
This script above need to have GD installed on the server.

SWFIR – great way to style images

JavaScript No Comments »

SWFIR (swf Image Replacement), it is a great way to add some style to images inside a web page escaping from the limitations imposed by the HTML but respecting the standard design concepts. This is done using JavaScript and Flash. Using swfir is simple and you don't need any advanced knowledge about Flash or JavaScript. Just download the swfir_v1.zip and unzip it, then upload the 2 files (swfir.js and swfir.swf) from it to the same directory in your website. To make it work all you need to do is include the JavaScript file and then to choose the images that will be styled and features applied to them.
The .js file need to be included in the "head" of the document:

PHP:
  1. <script type="text/javascript" src="swfir.js"></script>

I will use some styling features to the following image:

PHP:
  1. <img src="picture.jpg" alt="Here is some alt text" id="pic2" />

To call swfIR at work you have to use something like:

PHP:
  1. <script type="text/javascript">
  2. window.onload = function(){
  3.   round = new swfir();
  4.   round.specify('src', 'swfir.swf');
  5.   round.specify('border-color', '#cccccc');
  6.   round.specify('border-width', '1');
  7.   round.specify('border-radius', '10');
  8.   round.specify('shadow-blur', '5');
  9.   round.swap("#pic2");
  10. }
  11. </script>

The code above need to be situated inside the "body" section. I used some parameters (there are more that can be used/tested) to style my "pic2" image.
The result is something like this:

Example

It works fine in Firefox 2.0 and IE 6.0, unfortunately on some cumputers with SP2 failed to run in IE 6.0, nut sure why but you can write to the SWFIR team.

I will fix the errors

General 1 Comment »

I see some errors in my blog caused by the code inserted in some examples, I have to find a way to fix this, maybe using a wordpress plugin to deal with the "plain text" code. Also it will help visitors to distinguish easier the post's text from the code examples.

CakePHP, Simple Contact Form, email sending

CakePHP 15 Comments »

In this post a I will show an example of a simple contact form which sends an email to a specified address, done using CakePHP.
The form has 3 fields: name, email address and message.
Firs of all let's see the "view" for the form displaying. It looks like this (app/views/controller_name/contact.thtml):

PHP:
  1. <?php if (isset($succes)){?> // $succes is the variable which is verified to see if email was sent
  2.      <p>The message has been sent.
  3.      <br />Thank you!
  4.      </p> // Message displayed if success
  5. <?php }else{?>
  6.      <form method="post" enctype="multipart/form-data" action=”<?php echo $this->webroot;?>controllername/contact/”>
  7.      </form>
  8.  
  9. <?php if (isset($erroare)){?> // Verify if is an error and then display error message
  10.       <p>Please complete all fields!</p>
  11. <?}?>
  12.  
  13. <p>
  14.     Name: &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $html->input(’Page/name’, array(’size’ => ‘25))?>
  15. </p>
  16.  
  17. <p>
  18.     Email: &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $html->input(’Page/email’, array(’size’ => ‘25))?>
  19. </p>
  20.  
  21. <p>
  22.    Message: &nbsp; &nbsp; &nbsp; <?php echo $html->textarea(’Page/message’, array(’cols’ => ‘40′, ‘rows’ => ‘10))?>
  23. </p>
  24.  
  25. <p>
  26.    <?php echo $html->submit(’Send email’)?>
  27. </p>
  28. <p>
  29.    All fields have to be completed!
  30. </p>
  31. </form>
  32.  
  33. <?php }?>

Now we need a small component to verify the email address to be a valid one and also to contain the code necessary for the email sending. The component (app/controllers/components/email.php) is looking like this:

PHP:
  1. <?php
  2.  
  3. class EmailComponent extends Object
  4. {
  5.  
  6.     function email($email, $name, $message)
  7.     {
  8.       $f_mail = "name@domain.com";
  9.       $f_name = "WebMaster domain.com";
  10.       $f_message ="\nName : $name";   // start building the message
  11.       $f_message .="\n\nE-mail : $email";
  12.       $f_message .="\n\nMessage \n : $message";
  13.  
  14.       $headers = "From: www.domain.com <administrator@domain.com>\n";
  15.       $headers .= "X-Sender: <admin @domain.com>\n";
  16.       $headers .= "X-Mailer: PHP\n";
  17.       $headers .="Reply-To:$f_mail\n\n";
  18.      
  19.       $subject = "Message from your website domain.com";
  20.      
  21.       mail("$f_mail", "$subject", "$f_message",$headers); // send the email
  22.  
  23.     }
  24.  
  25.  
  26.     function validMail($email) // this function checks to see if the email supplied by the visitor is a valid one
  27.     {
  28.         $pattern="^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$";
  29. // the regex for an email addres
  30.      
  31.      if(eregi($pattern, $email))
  32.         $ll=1;
  33.      else
  34.         $ll=0;
  35.      return $ll;
  36.     }
  37. }
  38.  
  39. ?>

And now becomes the logic part, the controller (app/controllers/controller_name.php):

PHP:
  1. <?php
  2.  
  3. class NameController extends AppController {
  4.  
  5.     var $uses = null;
  6.     var $components = array('Email');
  7.  
  8.     function contact(){
  9.  
  10.         $this->pageTitle = 'Contact';
  11.  
  12.         if (!empty($this->data))
  13.         {
  14.  
  15.           $email = $this->data['Page']['email'];
  16.           $name = $this->data['Page']['name'];
  17.           $message = $this->data['Page']['message'];
  18.  
  19.           $error = 0;
  20.  
  21.            // start filter vars
  22.           if(strlen(trim($email))<3)
  23.           {
  24.             $error=1;
  25.           }
  26.  
  27.           if(strlen(trim($message))<4)
  28.           {
  29.             $error=1;
  30.           }
  31.  
  32.          // verify email address
  33.           if( (strlen(trim($email))<3)||($this->Email->validMail($email)==0))
  34.           {
  35.             $error=1;
  36.           }
  37.  
  38.           if ($error==0)
  39.           {
  40.  
  41.             $this->Email->email($email, $name, $message)//here is the email sent
  42.             $this->set('succes', 'The message was sent <br />Thank you!');
  43.             // setting "succes" variable for displaying if email was sent.
  44.  
  45.           }else
  46.           {
  47.             $this->set('error', 'Please complete all fields');
  48.             // this is in case of error
  49.           }
  50.  
  51.       }else{
  52.        
  53.       }
  54.  
  55.   }
  56.  
  57. }?>

 

About Linkbaiting

SEO No Comments »

One of the most “productive” way to attract visitors to your website / webblog relying on your content is to use a technique called linkbaiting.
Everything you need to do, to be able to use this feature, is to ensure that you wrote a good (unique) content on your website, a content that people will like and their attention will be caught in such manner that they will post a link towards your website.
This content you have is also called a possible “hook” for the visitor that will point a link to you. Through time there were some attempts to categorize this “hooks”, most encountered are:
- Informational Hooks – described as a content full with good information that can be usefull
- News Hooks – content related with fresh informations about some domains
- Hummor Hooks – funny content including jokes, pictures, movies
- Evil Hooks – means writing some bad things about a well known product / blogger / website, that sometimes can attract a lot of audience.
- Tool Hooks – creating a useful online tool that people will link to it.

Theme designed by N.Design Studio
Entries RSS Comments RSS Log in