Advertisement
Guest User

Crop

a guest
Sep 18th, 2010
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.25 KB | None | 0 0
  1. <?php
  2.  
  3.     define('IMAGE_BORDER',  1);
  4.     define('IMAGE_SCALE',   2);
  5.     define('IMAGE_SQUASH',  3);
  6.     define('IMAGE_CROP',    4);
  7.    
  8. /*
  9.    
  10.     create_resource    
  11.         Creates a new resource for use by image_magick
  12.         Returns: internal resource id
  13.        
  14.     load_image          file name, resource id (default 1)
  15.         Loads image data into memory
  16.         Returns: nothing
  17.        
  18.     _load_image         resource id (default 1)
  19.         Loads full image into memory - usually called automatically
  20.         Returns: nothing
  21.        
  22.     is_image            resource id (default 1)
  23.         Checks if resource is for a valid image
  24.         Returns: false / image mime type
  25.        
  26.     get_dimensions      resource id (default 1)
  27.         Gets image dimensions
  28.         Returns: array width & height
  29.        
  30.     get_size            resource id (default 1)
  31.         Gets image size
  32.         Returns: byte size
  33.  
  34.     is_resource         resource id (default false)
  35.         Checks if resource exists
  36.         Returns true / false;
  37. */
  38.  
  39. class ims
  40. {
  41.     function ims()
  42.     {
  43.         $this->resource = array();
  44.         $this->image_loaded = array();
  45.         $this->create_resource();
  46.         //$tmp =& getInstance();
  47.         //$this->config = $tmp->config;
  48.        
  49.     }
  50.    
  51.     function create_resource()
  52.     {
  53.         $resource_id = count($this->resource) + 1;
  54.         $this->resource[$resource_id] = new Imagick();
  55.         $this->image_loaded[$resource_id] = false;
  56.         return $resource_id;
  57.     }
  58.    
  59.     function load_image($file_name, $resource_id = 1)
  60.     {
  61.         $this->is_resource($resource_id);
  62.        
  63.         $this->filename = $file_name;
  64.         // WE ONLY 1/2 LOAD THE IMAGE FOR PERFORMANCE
  65.         // USE _LOAD_IMAGE FOR FULL LOAD
  66.         $this->image_loaded[$resource_id] = $this->resource[$resource_id]->pingImage($this->filename) ?
  67.             'ping' : false;
  68.         //$this->image_loaded[$resource_id] = MagickPingImage($this->resource[$resource_id], $this->filename) ? 'ping' : false;
  69.     }
  70.    
  71.     function _load_image($resource_id = 1)
  72.     {
  73.         $this->is_resource($resource_id);
  74.        
  75.         $this->image_loaded[$resource_id] = $this->resource[$resource_id]->readImage($this->filename) ?
  76.             'full' : false;
  77.         //$this->image_loaded[$resource_id] = MagickReadImage($this->resource[$resource_id], $this->filename) ? 'full' : false;
  78.     }
  79.    
  80.     function is_image($resource_id = 1)
  81.     {
  82.         $this->is_resource($resource_id);
  83.        
  84.         return $this->image_loaded[$resource_id] ? $this->resource[$resource_id]->getImage() : false;
  85.         //return $this->image_loaded[$resource_id] ? MagickGetImageMimeType($this->resource[$resource_id]) : false;
  86.     }
  87.    
  88.     function get_dimensions($resource_id = 1)
  89.     {
  90.         if($this->image_loaded[$resource_id])
  91.         {
  92.             $dimensions['width'] = $this->resource[$resource_id]->getImageWidth();
  93.             $dimensions['height'] = $this->resource[$resource_id]->getImageHeight();
  94.             //$dimensions['width'] = MagickGetImageWidth($this->resource[$resource_id]);
  95.             //$dimensions['height'] = MagickGetImageHeight($this->resource[$resource_id]);
  96.         } else {
  97.             $dimensions['width'] = false;  
  98.             $dimensions['height'] = false;
  99.         }
  100.         return $dimensions;
  101.     }
  102.    
  103.     function get_size($resource_id = 1)
  104.     {
  105.         if($this->image_loaded[$resource_id])
  106.         {
  107.             //return MagickGetImageSize($this->resource[$resource_id]);
  108.             return $this->resource[$resource_id]->getImageSize();
  109.         } else {
  110.             return 0;
  111.         }
  112.     }
  113.    
  114.     function is_resource($resource_id = false)
  115.     {
  116.         if(!isset($this->resource[$resource_id]))
  117.             die('Resource Id: ' . $resource_id . ' does not exist');
  118.     }
  119.    
  120.     function set_bg($resource_id, $colour) {
  121.  
  122.     }
  123.    
  124.     function resize_image($resource_id = 1, $width, $height, $resize_type = IMAGE_BORDER, $border_colour = false)
  125.     {
  126.         $this->is_resource($resource_id);
  127.        
  128.         $this->set_bg($resource_id, '#FFFFFF');
  129.        
  130.         if($this->image_loaded[$resource_id] != 'full') $this->_load_image($resource_id);
  131.        
  132.         if($resize_type == IMAGE_BORDER)
  133.         {
  134.             // CALCULATE NEW SIZE
  135.             $new_dimensions = $this->calculate_size(
  136.                 $resource_id, $width, $height, 'minimum'
  137.             ); 
  138.            
  139.             // SCALE THE IMAGE
  140.             $this->_resize_image($resource_id, $new_dimensions);
  141.  
  142.             // CALCULATE BORDER OFFSET
  143.             $top = ($height - $new_dimensions['height']) / 2;
  144.             $left = ($width - $new_dimensions['width']) / 2;
  145.             if($top == 0 && $left == 0)
  146.             {
  147.            
  148.             } else {
  149.                 // ADD THE BORDERS
  150.                 $this->add_border($resource_id, $border_colour, $top, $left);
  151.             }
  152.            
  153.            
  154.         } else if($resize_type == IMAGE_CROP) {
  155.        
  156.             // CALCULATE NEW SIZE
  157.             $new_dimensions = $this->calculate_size($resource_id, $width, $height, 'maximum'); 
  158.  
  159.             // SCALE THE IMAGE
  160.             $this->_resize_image($resource_id, $new_dimensions);
  161.            
  162.             // CALCULATE BORDER OFFSET
  163.             $top = ($new_dimensions['height'] - $height) / 2;
  164.             $left = ($new_dimensions['width'] - $width) / 2;
  165.            
  166.            
  167.             /*# if gif, save as jpeg, reload and crop
  168.            
  169.             if(MagickGetImageFormat($this->resource[$resource_id]) == 'GIF') {
  170.                 $outFile = $this->config->tmp . 'banners/b4b-gif-' . time();
  171.                 $this->output_image($resource_id, 'JPG', $outFile , 100);
  172.                
  173.                 # re-read image
  174.                 $resource_id = $this->create_resource();
  175.                 $this->load_image($outFile, $resource_id);
  176.                 $this->_load_image($resource_id);
  177.                 unlink($outFile);
  178.             }*/
  179.            
  180.             // CROP THE IMAGE
  181.             $this->crop_image($resource_id, $width, $height, $top, $left);
  182.            
  183.         } else if($resize_type == IMAGE_SCALE) {
  184.        
  185.             // CALCULATE NEW SIZE
  186.             $new_dimensions = $this->calculate_size($resource_id, $width, $height, 'maximum'); 
  187.        
  188.             // SCALE THE IMAGE
  189.             $this->_resize_image($resource_id, $new_dimensions);
  190.            
  191.         } else if($resize_type == IMAGE_SQUASH) {
  192.            
  193.             $this->_resize_image($resource_id, array('width' => $width, 'height' => $height));
  194.            
  195.         } else {
  196.             die('You specified a resize method that does not exists');
  197.         }
  198.     }
  199.    
  200.     function toJpeg($in, $out) {
  201.    
  202.         $res = $this->create_resource();
  203.         $this->load_image($in, $res);
  204.         $dimensions = $this->get_dimensions($res);
  205.        
  206.         $im = imagecreatetruecolor($dimensions['width'], $dimensions['height']);
  207.         $col = imagecolorallocate($im, 255, 255, 255);
  208.         imagefill($im, 0, 0, $col);
  209.        
  210.         $image = imagecreatefromstring(file_get_contents($in));
  211.        
  212.         imagecopyresampled(
  213.             $im, $image, 0, 0, 0, 0, $dimensions['width'], $dimensions['height'],
  214.             $dimensions['width'], $dimensions['height']
  215.         );
  216.        
  217.         imagejpeg($im, $out, 100);
  218.         imagedestroy($im);
  219.     }
  220.    
  221.     function add_border($resource_id, $color, $top_border, $left_border)
  222.     {
  223.         $this->is_resource($resource_id);
  224.        
  225.         $this->resource[$resource_id]->borderImage($color, $left_border, $top_border);
  226.         //MagickBorderImage($this->resource[$resource_id], $color, $left_border, $top_border);
  227.     }
  228.    
  229.     function crop_image($resource_id, $width, $height, $x_offset, $y_offset)
  230.     {
  231.         $this->is_resource($resource_id);
  232.  
  233.         $this->resource[$resource_id]->cropImage($width, $height, round($y_offset, 0), round($x_offset, 0));
  234.         //MagickCropImage($this->resource[$resource_id], $width, $height, round($y_offset, 0), round($x_offset, 0));
  235.     }
  236.    
  237.     function output_image($resource_id, $type = 'JPG', $file_path = false, $quality = 80)
  238.     {
  239.         $valid_output_formats = array(
  240.             'JPG', 'JPEG', 'GIF', 'GIF87', 'PNG32', 'PNG24', 'PNG8'
  241.         );
  242.         $type = strtoupper($type);
  243.         if($file_path)
  244.         {
  245.             // SET OUTPUT FORMAT
  246.             if(in_array($type, $valid_output_formats))
  247.             {
  248.                 if($type == 'JPG' || $type = 'JPEG')
  249.                 {
  250.                     $this->resource[$resource_id]->setImageCompression($quality);
  251.                     $this->resource[$resource_id]->setImageColorspace(13);
  252.                     //$this->resource[$resource_id]->setImageQuality($quality);
  253.                     //MagickSetImageCompression($this->resource[$resource_id], MW_JPEGCompression);
  254.                     //$this->resource[$resource_id]->setImageCompress($quality);
  255.                     //MagickSetImageCompressionQuality($this->resource[$resource_id], $quality);
  256.                 }
  257.                 if(file_exists($file_path))
  258.                 {
  259.                     unlink($file_path);
  260.                 }
  261.                 $response = $this->resource[$resource_id]->writeImage($file_path);
  262.                 //$response = MagickWriteImage($this->resource[$resource_id], $file_path);
  263.                 $this->clear($resource_id);
  264.                 return $response;
  265.             } else {
  266.                 die('Invalid output format. Permitted: ' . implode(', ', $valid_output_formats) . '.');
  267.             }
  268.         } else {
  269.             $this->resource[$resource_id]->setImageFormat('jpg');
  270.             //MagickSetImageFormat($this->resource[$resource_id], 'jpg');
  271.             echo $this->resource[$resource_id]->getImageBlob();
  272.             //MagickEchoImageBlob($this->resource[$resource_id]);
  273.         }
  274.     }
  275.    
  276.     private function _resize_image($resource_id, $size)
  277.     {
  278.         $width = $size['width']; $height = $size['height'];
  279.         //print_r($size); die();
  280.         $this->resource[$resource_id]->scaleImage($width, $height);
  281.         //MagickScaleImage($this->resource[$resource_id], $width, $height);
  282.     }
  283.    
  284.     function clear($resource_id)
  285.     {
  286.         $this->is_resource($resource_id);
  287.        
  288.         $this->resource[$resource_id]->clear();
  289.         //ClearMagickWand($this->resource[$resource_id]);
  290.         unset($this->resource[$resource_id]);
  291.         unset($this->image_loaded[$resource_id]);
  292.     }
  293.    
  294.     function calculate_size($resource_id, $width, $height, $type = 'maximum')
  295.     {
  296.         $this->is_resource($resource_id);
  297.        
  298.         if($type == 'maximum')
  299.         {
  300.             $dimensions = $this->get_dimensions($resource_id);
  301.  
  302.             $scale = $dimensions['width'] / $width;
  303.             if($scale == 0) $scale = 1;
  304.            
  305.             # check if height is going to be too small
  306.             $nHeight = $dimensions['height'] / $scale;
  307.            
  308.             if($nHeight < $height) {
  309.                 $scale = $dimensions['height'] / $height;
  310.                 if($scale == 0) $scale = 1;
  311.             }
  312.            
  313.             $new_dimensions['width'] = round($dimensions['width'] / $scale, 0);
  314.             $new_dimensions['height'] = round($dimensions['height'] / $scale, 0);
  315.  
  316.         } else if($type == 'minimum') {
  317.             $dimensions = $this->get_dimensions($resource_id);
  318.             $scale = $dimensions['width'] / $width;
  319.            
  320.             if(($dimensions['height'] / $scale) > $height)
  321.                 $scale = $dimensions['height'] / $height;
  322.            
  323.             if($scale == 0) $scale = 1;
  324.            
  325.             $new_dimensions['width'] = round($dimensions['width'] / $scale, 0);
  326.             $new_dimensions['height'] = round($dimensions['height'] / $scale, 0);
  327.            
  328.             /*$dimensions = $this->get_dimensions($resource_id);
  329.            
  330.             $scale = $dimensions['width'] / $width;
  331.            
  332.             if(($dimensions['height'] / $scale) < $height)
  333.                 $scale = $dimensions['height'] / $height;
  334.                
  335.             if($scale == 0) $scale = 1;
  336.                
  337.             $new_dimensions['width'] = round($dimensions['width'] / $scale, 0);
  338.             $new_dimensions['height'] = round($dimensions['height'] / $scale, 0);*/    
  339.         } else {
  340.             die('Size calculation type invalid');
  341.         }
  342.        
  343.         return $new_dimensions;
  344.     }
  345. }
  346.  
  347. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement