This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Pastebin - Paste and link to it

Posted by Ahmed Sabbour Mon 12th May 2008 19:56

  1. <?php
  2. /*
  3. File: /app/controllers/components/image.php
  4. */
  5. class ImageComponent extends Object
  6. {
  7.         /*
  8.         *       Uploads an image and its thumbnail into $folderName/big and $folderName/small respectivley.
  9.         *       the  generated thumnail could either have the same aspect ratio as the uploaded image, or could
  10.         *       be a zoomed and cropped version.
  11.        
  12.         *       Directions:
  13.         *       In view where you upload the image, make sure your form creation is similar to the following
  14.         *       <?= $form->create('FurnitureSet',array('type' => 'file')); ?>
  15.         *
  16.         *       In view where you upload the image, make sure that you have a file input similar to the following
  17.         *       <?= $form->file('Image/name1'); ?>
  18.         *       
  19.         *       In the controller, add the component to your components array
  20.         *       var $components = array("Image");
  21.         *
  22.         *       In your controller action (the parameters are expained below)
  23.         *       $image_path = $this->Image->upload_image_and_thumbnail($this->data,"name1",573,80,"sets",true);
  24.         *       this returns the file name of the result image.  You can  store this file name in the database
  25.         *
  26.         *       Note that your image will be stored in 2 locations:
  27.         *       Image: /webroot/img/$folderName/big/$image_path
  28.         *       Thumbnail:  /webroot/img/$folderName/small/$image_path
  29.         *
  30.         *       Finally in the view where you want to see the images
  31.         *       <?= $html->image('sets/big/'.$furnitureSet['FurnitureSet']['image_path']);
  32.         *       where "sets" is the folder name we saved our pictures in, and $furnitureSet['FurnitureSet']['image_path'] is the file name we stored in the database
  33.        
  34.         *       Parameters:
  35.         *       $data: CakePHP data array from the form
  36.         *       $datakey: key in the $data array. If you used <?= $form->file('Image/name1'); ?> in your view, then $datakey = name1
  37.         *       $imgscale: the maximum width or height that you want your picture to be resized to
  38.         *       $thumbscale: the maximum width or height that you want your thumbnail to be resized to
  39.         *       $folderName: the name of the parent folder of the images. The images will be stored to /webroot/img/$folderName/big/ and  /webroot/img/$folderName/small/
  40.         *       $square: a boolean flag indicating whether you want square and zoom cropped thumbnails, or thumbnails with the same aspect ratio of the source image
  41.         */     
  42.         function upload_image_and_thumbnail($data, $datakey, $imgscale, $thumbscale, $folderName, $square) {
  43.                 if (strlen($data['Image'][$datakey]['name'])>4){
  44.                                         $error = 0;
  45.                                         $tempuploaddir = "img/temp"; // the /temp/ directory, should delete the image after we upload
  46.                                         $biguploaddir = "img/".$folderName."/big"; // the /big/ directory
  47.                                         $smalluploaddir = "img/".$folderName."/small"; // the /small/ directory for thumbnails
  48.                                        
  49.                                         // Make sure the required directories exist, and create them if necessary
  50.                                         if(!is_dir($tempuploaddir)) mkdir($tempuploaddir,true);
  51.                                         if(!is_dir($biguploaddir)) mkdir($biguploaddir,true);
  52.                                         if(!is_dir($smalluploaddir)) mkdir($smalluploaddir,true);
  53.                                        
  54.                                         $filetype = $this->getFileExtension($data['Image'][$datakey]['name']);
  55.                                         $filetype = strtolower($filetype);
  56.  
  57.                                         if (($filetype != "jpeg")  && ($filetype != "jpg") && ($filetype != "gif") && ($filetype != "png"))
  58.                                         {
  59.                                                 // verify the extension
  60.                                                 return;
  61.                                         }
  62.                                         else
  63.                                         {
  64.                                                 // Get the image size
  65.                                                 $imgsize = GetImageSize($data['Image'][$datakey]['tmp_name']);
  66.                                         }
  67.  
  68.                                         // Generate a unique name for the image (from the timestamp)
  69.                                         $id_unic = str_replace(".", "", strtotime ("now"));
  70.                                         $filename = $id_unic;
  71.                                           
  72.                                         settype($filename,"string");
  73.                                         $filename.= ".";
  74.                                         $filename.=$filetype;
  75.                                         $tempfile = $tempuploaddir . "/$filename";
  76.                                         $resizedfile = $biguploaddir . "/$filename";
  77.                                         $croppedfile = $smalluploaddir . "/$filename";
  78.                                        
  79.                                        
  80.                                         if (is_uploaded_file($data['Image'][$datakey]['tmp_name']))
  81.                     {                   
  82.                                                 // Copy the image into the temporary directory
  83.                         if (!copy($data['Image'][$datakey]['tmp_name'],"$tempfile"))
  84.                         {
  85.                             print "Error Uploading File!.";
  86.                             exit();
  87.                         }
  88.                                                 else {       
  89.                                                         /*
  90.                                                          *      Generate the big version of the image with max of $imgscale in either directions
  91.                                                          */
  92.                                                         $this->resize_img($tempfile, $imgscale, $resizedfile);             
  93.                                                        
  94.                                                         if($square) {
  95.                                                                 /*
  96.                                                                  *      Generate the small square version of the image with scale of $thumbscale
  97.                                                                  */
  98.                                                                 $this->crop_img($tempfile, $thumbscale, $croppedfile);
  99.                                                         }
  100.                                                         else {
  101.                                                                 /*
  102.                                                                  *      Generate the big version of the image with max of $imgscale in either directions
  103.                                                                  */
  104.                                                                 $this->resize_img($tempfile, $thumbscale, $croppedfile);
  105.                                                         }
  106.                                                        
  107.                                                         // Delete the temporary image
  108.                                                         unlink($tempfile);
  109.                                                 }
  110.                     }
  111.  
  112.                      // Image uploaded, return the file name
  113.                                          return $filename;   
  114.                 }
  115.         }
  116.        
  117.         /*
  118.         *       Deletes the image and its associated thumbnail
  119.         *       Example in controller action:  $this->Image->delete_image("1210632285.jpg","sets");
  120.         *
  121.         *       Parameters:
  122.         *       $filename: The file name of the image
  123.         *       $folderName: the name of the parent folder of the images. The images will be stored to /webroot/img/$folderName/big/ and  /webroot/img/$folderName/small/
  124.         */
  125.         function delete_image($filename,$folderName) {
  126.                 unlink("img/".$folderName."/big/".$filename);
  127.                 unlink("img/".$folderName."/small/".$filename);
  128.         }
  129.        
  130.     function crop_img($imgname, $scale, $filename) {   
  131.                 $filetype = $this->getFileExtension($imgname);
  132.                 $filetype = strtolower($filetype);
  133.                           
  134.                 switch($filetype){
  135.                         case "jpeg":
  136.                         case "jpg":
  137.                           $img_src = ImageCreateFromjpeg ($imgname);
  138.                          break;
  139.                          case "gif":
  140.                           $img_src = imagecreatefromgif ($imgname);
  141.                          break;
  142.                          case "png":
  143.                           $img_src = imagecreatefrompng ($imgname);
  144.                          break;
  145.                 }
  146.                
  147.             $width = imagesx($img_src);
  148.             $height = imagesy($img_src);
  149.             $ratiox = $width / $height * $scale;
  150.             $ratioy = $height / $width * $scale;
  151.                 
  152.             //-- Calculate resampling
  153.             $newheight = ($width <= $height) ? $ratioy : $scale;
  154.             $newwidth = ($width <= $height) ? $scale : $ratiox;
  155.                
  156.             //-- Calculate cropping (division by zero)
  157.             $cropx = ($newwidth - $scale != 0) ? ($newwidth - $scale) / 2 : 0;
  158.             $cropy = ($newheight - $scale != 0) ? ($newheight - $scale) / 2 : 0;
  159.                
  160.             //-- Setup Resample & Crop buffers
  161.             $resampled = imagecreatetruecolor($newwidth, $newheight);
  162.             $cropped = imagecreatetruecolor($scale, $scale);
  163.                
  164.             //-- Resample
  165.             imagecopyresampled($resampled, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  166.             //-- Crop
  167.             imagecopy($cropped, $resampled, 0, 0, $cropx, $cropy, $newwidth, $newheight);
  168.  
  169.                 // Save the cropped image
  170.                 switch($filetype)
  171.                 {
  172.                         case "jpeg":
  173.                         case "jpg":
  174.                          imagejpeg($cropped,$filename,80);
  175.                          break;
  176.                          case "gif":
  177.                          imagegif($cropped,$filename,80);
  178.                          break;
  179.                          case "png":
  180.                          imagepng($cropped,$filename,80);
  181.                          break;
  182.                 }
  183.     }
  184.        
  185.         function resize_img($imgname, $size, $filename) {
  186.                 $filetype = $this->getFileExtension($imgname);
  187.                 $filetype = strtolower($filetype);
  188.  
  189.                 switch($filetype) {
  190.                         case "jpeg":
  191.                         case "jpg":
  192.                         $img_src = ImageCreateFromjpeg ($imgname);
  193.                         break;
  194.                         case "gif":
  195.                         $img_src = imagecreatefromgif ($imgname);
  196.                         break;
  197.                         case "png":
  198.                         $img_src = imagecreatefrompng ($imgname);
  199.                         break;
  200.                 }
  201.  
  202.                 $true_width = imagesx($img_src);
  203.                 $true_height = imagesy($img_src);
  204.  
  205.                 if ($true_width>=$true_height)
  206.                 {
  207.                         $width=$size;
  208.                         $height = ($width/$true_width)*$true_height;
  209.                 }
  210.                 else
  211.                 {
  212.                         $width=$size;
  213.                         $height = ($width/$true_width)*$true_height;
  214.                 }
  215.                 $img_des = ImageCreateTrueColor($width,$height);
  216.                 imagecopyresampled ($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height);
  217.  
  218.                 // Save the resized image
  219.                 switch($filetype)
  220.                 {
  221.                         case "jpeg":
  222.                         case "jpg":
  223.                          imagejpeg($img_des,$filename,80);
  224.                          break;
  225.                          case "gif":
  226.                          imagegif($img_des,$filename,80);
  227.                          break;
  228.                          case "png":
  229.                          imagepng($img_des,$filename,80);
  230.                          break;
  231.                 }
  232.         }
  233.  
  234.     function getFileExtension($str) {
  235.  
  236.         $i = strrpos($str,".");
  237.         if (!$i) { return ""; }
  238.         $l = strlen($str) - $i;
  239.         $ext = substr($str,$i+1,$l);
  240.         return $ext;
  241.     }
  242. } ?>
  243.  

The following amendments have been posted:
PermaLink to this entry http://paste.uni.cc/18691 Download 18691.txt
Submit a correction or amendment below. (click here to make a fresh posting)

Your Name
Syntax:
Remember my name in a cookie

Type If you are unable to read this image, click the help link to the right of the input box into this box... (help)
Reload the image, I can't read it!

Code: To ensure legibility, keep your code lines under 80 characters long.

You can highlight lines: start with "@#" and end with "!#"

Description: You can leave a small description for your code. (Errors, etc)

Please note that your script will be available publicly.
Page generated in 5.414 seconds. Powered by Register your domain name and build your site at UNI.CC Hosted By Simgames.net Maintained by Allowee
Parse error: syntax error, unexpected $end in /hsphere/local/home/allowee/paste.uni.cc/bbclone/var/access.php on line 12301