Advertisement
Guest User

Untitled

a guest
May 18th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.18 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', '1');
  4. ini_set('memory_limit', '500M');
  5. set_time_limit(0);
  6.  
  7. function custom_error_handler($number, $message, $file, $line, $vars){
  8.     $email = "
  9.         <p>An error ($number) occurred on line
  10.         <strong>$line</strong> and in the <strong>file: $file.</strong>
  11.         <p> $message </p>";
  12.     $email .= "<pre>" . print_r($vars, 1) . "</pre>";
  13.     $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  14.     error_log($email, 1, 'braunson@gmail.com', $headers);
  15. }
  16.  
  17. set_error_handler('custom_error_handler');
  18.  
  19.         class resize
  20.         {
  21.             // *** Class variables
  22.             private $image;
  23.             private $width;
  24.             private $height;
  25.             private $imageResized;
  26.  
  27.             function __construct($fileName)
  28.             {
  29.                 // *** Open up the file
  30.                 $this->image = $this->openImage($fileName);
  31.  
  32.                 // *** Get width and height
  33.                 $this->width  = imagesx($this->image);
  34.                 $this->height = imagesy($this->image);
  35.             }
  36.  
  37.             ## --------------------------------------------------------
  38.  
  39.             private function openImage($file)
  40.             {
  41.                 // *** Get extension
  42.                 $extension = strtolower(strrchr($file, '.'));
  43.  
  44.                 switch($extension)
  45.                 {
  46.                     case '.jpg':
  47.                     case '.jpeg':
  48.                         $img = @imagecreatefromjpeg($file);
  49.                         break;
  50.                     case '.gif':
  51.                         $img = @imagecreatefromgif($file);
  52.                         break;
  53.                     case '.png':
  54.                         $img = @imagecreatefrompng($file);
  55.                         break;
  56.                     default:
  57.                         $img = false;
  58.                         break;
  59.                 }
  60.                 return $img;
  61.             }
  62.  
  63.             ## --------------------------------------------------------
  64.  
  65.             public function resizeImage($newWidth, $newHeight, $option="auto")
  66.             {
  67.                 // *** Get optimal width and height - based on $option
  68.                 $optionArray = $this->getDimensions($newWidth, $newHeight, $option);
  69.  
  70.                 $optimalWidth  = $optionArray['optimalWidth'];
  71.                 $optimalHeight = $optionArray['optimalHeight'];
  72.  
  73.  
  74.                 // *** Resample - create image canvas of x, y size
  75.                 $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
  76.                 imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
  77.  
  78.  
  79.                 // *** if option is 'crop', then crop too
  80.                 if ($option == 'crop') {
  81.                     $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
  82.                 }
  83.             }
  84.  
  85.             ## --------------------------------------------------------
  86.            
  87.             private function getDimensions($newWidth, $newHeight, $option)
  88.             {
  89.  
  90.                switch ($option)
  91.                 {
  92.                     case 'exact':
  93.                         $optimalWidth = $newWidth;
  94.                         $optimalHeight= $newHeight;
  95.                         break;
  96.                     case 'portrait':
  97.                         $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  98.                         $optimalHeight= $newHeight;
  99.                         break;
  100.                     case 'landscape':
  101.                         $optimalWidth = $newWidth;
  102.                         $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  103.                         break;
  104.                     case 'auto':
  105.                         $optionArray = $this->getSizeByAuto($newWidth, $newHeight);
  106.                         $optimalWidth = $optionArray['optimalWidth'];
  107.                         $optimalHeight = $optionArray['optimalHeight'];
  108.                         break;
  109.                     case 'crop':
  110.                         $optionArray = $this->getOptimalCrop($newWidth, $newHeight);
  111.                         $optimalWidth = $optionArray['optimalWidth'];
  112.                         $optimalHeight = $optionArray['optimalHeight'];
  113.                         break;
  114.                 }
  115.                 return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  116.             }
  117.  
  118.             ## --------------------------------------------------------
  119.  
  120.             private function getSizeByFixedHeight($newHeight)
  121.             {
  122.                 $ratio = $this->width / $this->height;
  123.                 $newWidth = $newHeight * $ratio;
  124.                 return $newWidth;
  125.             }
  126.  
  127.             private function getSizeByFixedWidth($newWidth)
  128.             {
  129.                 $ratio = $this->height / $this->width;
  130.                 $newHeight = $newWidth * $ratio;
  131.                 return $newHeight;
  132.             }
  133.  
  134.             private function getSizeByAuto($newWidth, $newHeight)
  135.             {
  136.                 if ($this->height < $this->width)
  137.                 // *** Image to be resized is wider (landscape)
  138.                 {
  139.                     $optimalWidth = $newWidth;
  140.                     $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  141.                 }
  142.                 elseif ($this->height > $this->width)
  143.                 // *** Image to be resized is taller (portrait)
  144.                 {
  145.                     $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  146.                     $optimalHeight= $newHeight;
  147.                 }
  148.                 else
  149.                 // *** Image to be resizerd is a square
  150.                 {
  151.                     if ($newHeight < $newWidth) {
  152.                         $optimalWidth = $newWidth;
  153.                         $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  154.                     } else if ($newHeight > $newWidth) {
  155.                         $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  156.                         $optimalHeight= $newHeight;
  157.                     } else {
  158.                         // *** Sqaure being resized to a square
  159.                         $optimalWidth = $newWidth;
  160.                         $optimalHeight= $newHeight;
  161.                     }
  162.                 }
  163.  
  164.                 return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  165.             }
  166.  
  167.             ## --------------------------------------------------------
  168.  
  169.             private function getOptimalCrop($newWidth, $newHeight)
  170.             {
  171.  
  172.                 $heightRatio = $this->height / $newHeight;
  173.                 $widthRatio  = $this->width /  $newWidth;
  174.  
  175.                 if ($heightRatio < $widthRatio) {
  176.                     $optimalRatio = $heightRatio;
  177.                 } else {
  178.                     $optimalRatio = $widthRatio;
  179.                 }
  180.  
  181.                 $optimalHeight = $this->height / $optimalRatio;
  182.                 $optimalWidth  = $this->width  / $optimalRatio;
  183.  
  184.                 return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  185.             }
  186.  
  187.             ## --------------------------------------------------------
  188.  
  189.             private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
  190.             {
  191.                 // *** Find center - this will be used for the crop
  192.                 $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
  193.                 $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
  194.  
  195.                 $crop = $this->imageResized;
  196.                 //imagedestroy($this->imageResized);
  197.  
  198.                 // *** Now crop from center to exact requested size
  199.                 $this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
  200.                 imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
  201.             }
  202.  
  203.             ## --------------------------------------------------------
  204.  
  205.             public function saveImage($savePath, $imageQuality="100")
  206.             {
  207.                 // *** Get extension
  208.                 $extension = strrchr($savePath, '.');
  209.                 $extension = strtolower($extension);
  210.  
  211.                 switch($extension)
  212.                 {
  213.                     case '.jpg':
  214.                     case '.jpeg':
  215.                         if (imagetypes() & IMG_JPG) {
  216.                             imagejpeg($this->imageResized, $savePath, $imageQuality);
  217.                         }
  218.                         break;
  219.  
  220.                     case '.gif':
  221.                         if (imagetypes() & IMG_GIF) {
  222.                             imagegif($this->imageResized, $savePath);
  223.                         }
  224.                         break;
  225.  
  226.                     case '.png':
  227.                         // *** Scale quality from 0-100 to 0-9
  228.                         $scaleQuality = round(($imageQuality/100) * 9);
  229.  
  230.                         // *** Invert quality setting as 0 is best, not 9
  231.                         $invertScaleQuality = 9 - $scaleQuality;
  232.  
  233.                         if (imagetypes() & IMG_PNG) {
  234.                              imagepng($this->imageResized, $savePath, $invertScaleQuality);
  235.                         }
  236.                         break;
  237.  
  238.                     // ... etc
  239.  
  240.                     default:
  241.                         // *** No extension - No save.
  242.                         echo "Wrong Ext, upload failed :(";
  243.                         break;
  244.                 }
  245.  
  246.                 imagedestroy($this->imageResized);
  247.             }
  248.  
  249.  
  250.             ## --------------------------------------------------------
  251.  
  252.         }
  253. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement