Still alive

General 1 Comment »

Hi to everyone, I know it passed a lot of time since I wrote something but I was busy meanwhile and I ignored completely my blog. I announce everyone that I’m ok and healthy and in few days some new post will appear, I have also in mind to go deeper with cakephp study and to write some nice examples and posts about it.

Valid XHTML 1.0 Strict pages with flash on them

JavaScript, Xhtml/CSS 1 Comment »

Everyone knows there is a problem in having a valid XHTML 1.0 strict page when you want to use some flash on it. In another post I described how this can be done but unfortunately that thing wasn't complying with the XHTML 1.0 strict instead was fine for the pages validating using the Transitional Standard of W3C XHTML specifications.
Now I came back on this subject with a solution that fix the "Strict" problem, for this I appealed to a little help from Javascript using the getElemementById method.
To insert a flash code in a web page according to the adobe specification you have to use a code almost similar with this one:

  1. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=",0,29,0" width="590" height="400">
  2.     <param name="movie" value="swf/flash.swf">
  3.     <param name="quality" value="high">
  4.     <embed src="swf/flash.swf" quality="high" pluginspage="" type="application/x-shockwave-flash" width="590" height="400"></embed>
  5.   </object>

Above code will insert an flash movie with the width of 590 and the height of 400, the flash movie is called flash.swf and it is situated in the swf directory. This code will cause some errors (I will not tell you here which) and the page fails to validate.
My method is to remove all the flash code from the page and to insert it using a small external javascript file.
The javascript file we will call it flash.js and place it in the /js directory inside the site root and should have the following content:

  1. function flash(){
  2. document.getElementById('flash').innerHTML = "<object width=\"590\" height=\"400\" codebase=\",0,29,0\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"><param value=\"swf/flash.swf\" name=\"movie\"/><param value=\"high\" name=\"quality\"/><embed width=\"590\" height=\"400\" type=\"application/x-shockwave-flash\" pluginspage=\"\" quality=\"high\" src=\"swf/flash.swf\"/></object>";
  3. }

Be careful to write all this code on a single line because it is a string and a string in javascript can't be stretched on multiple lines. As you see we are using the document.getElementById('flash').innerHTML method to write some content on a div with the id flash. The page should have the following code:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "">
  3. <html>
  4. <head>
  5. <title>Insert valid Flash in XHML 1.0 Strict</title>
  6. <script type="text/javascript" src="js/flash.js">   
  7. </script>
  8. <style type="text/css">
  9. #flash {
  10. width: 590px;
  11. height: 400px;
  12. background-color: #777;
  13. }
  14. </style>
  15. </head>
  16. <body onload="flash()">
  17. <h1>Insert flash animation using javascript</h1>
  18. <div id="flash"></div>
  19. </body>
  20. </html>

The result will display a valid page with a flash on it. As you see we include the javascript external file and we load the flash() function from it using the body event onload. The flash code is not displayed and everything we have at View source is the empty div.
That's all.

The Twelve Days of Scaleout

MySQL No Comments »

On the MySQL website can be found some articles posted under the name of: "The Twelve Days of Scaleout", this articles talks about success stories at clients which are using MySQL services. In this articles you can find case studies on website and services like: Wikipedia (the world largest online encyclopedia), Zimbra (a leader in open source collaboration software), Alcatel (where MySQL implemented Cluster Carrier Edition database), ofcourse there are more case studies all of them are largely described on the MySQL website on the Press Releases section.

Safari 3 for Windows

Internet No Comments »

Apple released a version of their browser Safari for Windows Xp and Vista. You can found it here and you can download it for free. Now Apple made both iTunes and Safari available for Windows. Safari 3 is a beta version, on the Apple website it's said that Safari is twice as fast as Internet Explorer.
I download it and tested and I'm quite impressed by it, but most of the time I will remain to the Firefox 2.0 I think.

CakePHP – Some controller functions

CakePHP No Comments »

Interacting with your views:

set($variable, $value);
string $variable;
mixed $value;

The principal use of this function is to extract data from a controller and transfer it to a view.
It can be used to transfer single values, whole arrays and so on etc. The moment you utilize set(), the variable can be accessed in your view: doing set(name, john) in your controller makes the variable name available in the view.

validateErrors() - displays the number of all the errors produced by an unsuccessful save.
validate() - this function is used to validate a model data respecting the rules of validation defined in to the model.

render($action, $layout, $file) - this function may not always be needed because render is called by default when every controller action ends, so the view specific to your action is rendered. On the other side, you can call this function to render the view anywhere in the controller code.

CakePHP – About Controllers

CakePHP No Comments »

The role of a controller is that of administering the logic for a special section of your application. In general controllers are utilized in administering the logic for only one model. To take as an example if you were to make up a website that directs a rental-library, you could use a BooksController and a RentalController to administer both your rentals and your books.
Note that in Cake Controller names are only plural.

The controllers of your application are classes that extends the Cake ApplicationController class, which also extends a core Controller class. Controllers may comprise many kinds of actions: functions that are utilized in your web application to show off data in views.

ApplicationController class can be described in /app/appController.php and it may include methods that are distributed among controllers. It itself extends the Controller class which is a standardized in Cake Library.

A controller has an action as it’s single functionality. The Dispatcher directs it automatically if any incoming page request details in routes configuration. Going back to our example with the rental-library, our BooksController is supposed to include the view(), rent(), and search() actions.
The controller should be situated in /app/controllers/bookscontroller.php and include:

  1. class BooksController extends AppController{
  2.   function view($id)
  3.   {
  4.     //logic here...
  5.   }
  6.   function rent($customer_id, $video_id)
  7.   {
  8.     //logic here...
  9.   }
  10.   function search($query)
  11.   {
  12.     //logic here...
  13.   }
  14. }

You should be capable to access these actions using the example URLs:

The data displayed on this pages can be easily formated from the views area, for example for the rent() action we will have: /app/views/books/rent/index.thtml and so on.

Very common DTDs

Xhtml/CSS 1 Comment »

Despite writing a good valid code and a valid CSS for your webpage, if you don’t specify the right DOCTYPE or you won’t specify it at all meaning that your page will be rendered in quirks mode and it can behave strange displaying visual errors that can be very unpredictable damaging your layout.
Bellow is a list with the most common DTDs I in my developing process respectively for HTML 4.01, XHTML 1.0 Transitional, XHTML 1.0 Strict.

  2. "">
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  5. "">
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  8. "">


Back again writing on my blog

General No Comments »

Hi there, I've been busy for a while but now I'm back writing on my blog and hope to have more time from now on to do it.

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):

  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);
  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.     }
  27.     function getFileExtension($str) {
  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):

  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):

  1. class ImagesController extends AppController {
  2. var $components = array("Image"); // here is the image component that we described above
  4. function add(){
  6. if (strlen($this->data['Image']['name1']['name'])>4){
  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);
  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){
  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";
  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);
  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.

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