Advertisement
petschko

Image Class

Aug 5th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.20 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Author: Peter Dragicevic [peter-91@hotmail.de]
  4.  * Authors-Website: http://petschko.org/
  5.  * Date: 04.08.2015
  6.  * Time: 14:52
  7.  * Update: 09.04.2016
  8.  * Version: 1.0.2 (Changed Class-Name & Website)
  9.  *
  10.  * Licence: http://creativecommons.org/licenses/by-sa/4.0/
  11.  * Notes: todo improve whole class
  12.  */
  13.  
  14. /**
  15.  * Class Image
  16.  */
  17. class Image {
  18.     private $path;
  19.     private $image_dir;
  20.     private $image_name;
  21.     private $image_ext;
  22.     private $image = false;
  23.     private $height;
  24.     private $width;
  25.     private $type;
  26.     private static $allowed_ext = array('png', 'gif', 'jpeg', 'jpg');
  27.  
  28.     /**
  29.      * Creates a new instance
  30.      *
  31.      * @param string $path - path of the image
  32.      * @param string|bool $type - Type of the new image
  33.      * @param int|bool $new_height - Height of the new image
  34.      * @param int|bool $new_width - Width of the new image
  35.      * @param bool $new - Create an new image? Set true to make sure that this get a new image
  36.      * @throws Exception - Image type not supported
  37.      */
  38.     public function __construct($path, $type = false, $new_height = false, $new_width = false, $new = false) {
  39.         // Set Path infos
  40.         $path = str_replace('/', DS, $path);
  41.         $this->setPath($path);
  42.         $this->setImageDir($path);
  43.         $this->setImageName($path);
  44.         $this->setImageExt($path);
  45.  
  46.         if(file_exists($path) && $new === false) {
  47.             $details = getimagesize($this->getPath());
  48.             $this->setType($details[2]);
  49.             $this->setHeight($details[1]);
  50.             $this->setWidth($details[0]);
  51.             unset($details);
  52.  
  53.             // Load image
  54.             $this->loadImg();
  55.         } else if($type !== false) {
  56.             $this->setType($type);
  57.             $this->setHeight($new_height);
  58.             $this->setWidth($new_width);
  59.  
  60.             // Create new image
  61.             $this->createImg();
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Clears Memory
  67.      */
  68.     public function __destruct() {
  69.         unset($this->path);
  70.         unset($this->image_dir);
  71.         unset($this->image_name);
  72.         unset($this->image_ext);
  73.         unset($this->height);
  74.         unset($this->width);
  75.         unset($this->type);
  76.  
  77.         // Delete image
  78.         imagedestroy($this->getImage());
  79.         unset($this->image);
  80.     }
  81.  
  82.     /**
  83.      * Returns the name of the image without extension
  84.      *
  85.      * @return string - Image name without extension
  86.      */
  87.     public function getImageName() {
  88.         return $this->image_name;
  89.     }
  90.  
  91.     /**
  92.      * Set the name of the image (without ext)
  93.      *
  94.      * @param string $image_name - path or filename with extension OR filename without extension
  95.      */
  96.     private function setImageName($image_name) {
  97.         if(mb_strripos($image_name, DS) !== false) {
  98.             $filename = mb_substr($image_name, mb_strripos($image_name, DS) + 1);
  99.             if(mb_strripos($image_name, '.') !== false)
  100.                 $this->image_name = mb_substr($filename, 0, mb_strripos($filename, '.') - 1);
  101.             else
  102.                 $this->image_name = $filename;
  103.         } else
  104.             $this->image_name = $image_name;
  105.     }
  106.  
  107.     /**
  108.      * Get the image extension (dot included)
  109.      *
  110.      * @return string - extension of the filename (with ".")
  111.      */
  112.     public function getImageExt() {
  113.         return $this->image_ext;
  114.     }
  115.  
  116.     /**
  117.      * Set the image extension based on the path or filename - sets invalid.dist if there is no extension
  118.      *
  119.      * @param string $image_ext - path or filename or extension of the image file
  120.      */
  121.     private function setImageExt($image_ext) {
  122.         if(mb_strripos($image_ext, DS) !== false)
  123.             $image_ext = mb_substr($image_ext, mb_strripos($image_ext, DS) + 1);
  124.  
  125.         if(mb_strripos($image_ext, '.') !== false)
  126.             $this->image_ext = mb_strtolower(mb_substr($image_ext, mb_strripos($image_ext, '.')));
  127.         else
  128.             $this->image_ext = 'invalid.dist';
  129.     }
  130.  
  131.     /**
  132.      * Returns the path to the image including the image name plus extension
  133.      *
  134.      * @return string - Full Image path
  135.      */
  136.     public function getPath() {
  137.         return $this->path;
  138.     }
  139.  
  140.     /**
  141.      * Set the path to the image including the image name plus extension
  142.      *
  143.      * @param string $path - Full Image path
  144.      */
  145.     private function setPath($path) {
  146.         $this->path = $path;
  147.     }
  148.  
  149.     /**
  150.      * Returns the image resource of this object
  151.      *
  152.      * @return resource - resource of the image in memory
  153.      */
  154.     public function getImage() {
  155.         return $this->image;
  156.     }
  157.  
  158.     /**
  159.      * Set a image resource to this object
  160.      *
  161.      * @param resource $image - the image resource
  162.      */
  163.     private function setImage($image) {
  164.         $this->image = $image;
  165.     }
  166.  
  167.     /**
  168.      * returns the height of this images
  169.      *
  170.      * @return int - height of the image
  171.      */
  172.     public function getHeight() {
  173.         return $this->height;
  174.     }
  175.  
  176.     /**
  177.      * Set the height og this image
  178.      *
  179.      * @param int $height - height of the image
  180.      */
  181.     private function setHeight($height) {
  182.         $this->height = $height;
  183.     }
  184.  
  185.     /**
  186.      * Returns the width of this image
  187.      *
  188.      * @return int - width of the image
  189.      */
  190.     public function getWidth() {
  191.         return $this->width;
  192.     }
  193.  
  194.     /**
  195.      * Set the width of this image
  196.      *
  197.      * @param int $width - width of the image
  198.      */
  199.     private function setWidth($width) {
  200.         $this->width = $width;
  201.     }
  202.  
  203.     /**
  204.      * Returns the image type of this image
  205.      *
  206.      * @return mixed type of the image
  207.      */
  208.     public function getType() {
  209.         return $this->type;
  210.     }
  211.  
  212.     /**
  213.      * Set the image type of this image
  214.      *
  215.      * @param mixed $type type of the image
  216.      */
  217.     private function setType($type) {
  218.         if(is_int($type))
  219.             $this->type = $type;
  220.         else
  221.             $this->setType(self::get_valid_type($type));
  222.     }
  223.  
  224.     /**
  225.      * Returns the path to the image but not the image-file itself
  226.      *
  227.      * @return string - the directory path where the image is
  228.      */
  229.     public function getImageDir() {
  230.         return $this->image_dir;
  231.     }
  232.  
  233.     /**
  234.      * Set the path to the image but not the filename itself
  235.      *
  236.      * @param string $image_dir - path where the image is, can be the full path to, we try to convert it
  237.      */
  238.     private function setImageDir($image_dir) {
  239.         if(mb_strripos($image_dir, DS) !== false) {
  240.             $this->image_dir = mb_substr($image_dir, 0, mb_strripos($image_dir, DS));
  241.         } else
  242.             $this->image_dir = '.' . DS;
  243.     }
  244.  
  245.     /**
  246.      * Loads an image into memory
  247.      *
  248.      * @throws Exception - Image-Type not found
  249.      */
  250.     private function loadImg() {
  251.         // Better use IMAGETYPE - IMG_ returns may a wrong value
  252.         switch($this->getType()) {
  253.             //case IMG_JPG:
  254.             //case IMG_JPEG:
  255.             case IMAGETYPE_JPEG:
  256.                 $this->setImage(imagecreatefromjpeg($this->getPath()));
  257.                 break;
  258.             //case IMG_PNG:
  259.             case IMAGETYPE_PNG:
  260.                 $this->setImage(imagecreatefrompng($this->getPath()));
  261.                 break;
  262.             //case IMG_GIF:
  263.             case IMAGETYPE_GIF:
  264.                 $this->setImage(imagecreatefromgif($this->getPath()));
  265.                 break;
  266.             default:
  267.                 throw new Exception('Image Type ' . $this->getType() . ' is not supported in class ' . __CLASS__ . ' for your image: ' . $this->getPath());
  268.         }
  269.     }
  270.  
  271.     /**
  272.      * Creates a new image
  273.      */
  274.     private function createImg() {
  275.         if($this->getHeight() > 0 && $this->getWidth() > 0) {
  276.             if(function_exists('imagecreatetruecolor'))
  277.                 $this->setImage(imagecreatetruecolor($this->getWidth(), $this->getHeight()));
  278.             else
  279.                 $this->setImage(imagecreate($this->getWidth(), $this->getHeight()));
  280.         }
  281.     }
  282.  
  283.     /**
  284.      * Get an valid image type from string
  285.      *
  286.      * @param string $type_string - extension of the image
  287.      * @return bool|int - false or the image type
  288.      * @throws Exception - MultiByte functions not found
  289.      */
  290.     public static function get_valid_type($type_string) {
  291.         if(function_exists('mb_strripos') && function_exists('mb_strripos')) {
  292.             $type_string = mb_strtolower($type_string);
  293.             if (mb_strripos($type_string, '.'))
  294.                 $type_string = mb_substr($type_string, mb_strripos($type_string, '.') + 1);
  295.  
  296.             if(in_array($type_string, self::$allowed_ext)) {
  297.                 switch($type_string) {
  298.                     case 'jpg':
  299.                     case 'jpeg':
  300.                         //return IMG_JPEG;
  301.                         return IMAGETYPE_JPEG;
  302.                     case 'png':
  303.                         //return IMG_PNG;
  304.                         return IMAGETYPE_PNG;
  305.                     case 'gif':
  306.                         //return IMG_GIF;
  307.                         return IMAGETYPE_GIF;
  308.                 }
  309.             }
  310.             // Default value
  311.             return false;
  312.         } else
  313.             throw new Exception('Please make sure, that you have installed the MultiBytes functions!');
  314.     }
  315.  
  316.     /**
  317.      * Save the current image to...
  318.      *
  319.      * @param string|bool $path - path where the image will be saved - on false use the image dir (this->getdir)
  320.      * @param string|int|bool $type - image type, as what the image should be saved
  321.      * @return bool - true on success
  322.      */
  323.     public function saveImg($path = false, $type = false) {
  324.         if(! $path)
  325.             $path = $this->getPath();
  326.  
  327.         if(is_string($type))
  328.             $type = self::get_valid_type($type);
  329.         else if(! $type)
  330.             $type = $this->getType();
  331.  
  332.         $saved = false;
  333.         switch($type) {
  334.             case IMAGETYPE_JPEG:
  335.             //case IMG_JPG:
  336.             //case IMG_JPEG:
  337.                 $saved = imagejpeg($this->getImage(), $path);
  338.                 break;
  339.             case IMAGETYPE_PNG:
  340.             //case IMG_PNG:
  341.                 $saved = imagepng($this->getImage(), $path);
  342.                 break;
  343.             case IMAGETYPE_GIF:
  344.             //case IMG_GIF:
  345.                 $saved = imagegif($this->getImage(), $path);
  346.                 break;
  347.         }
  348.  
  349.         return $saved;
  350.     }
  351.  
  352.     /**
  353.      * Resize an image
  354.      *
  355.      * @param int $new_height - height of the the new image
  356.      * @param int $new_width - width of the new image
  357.      * @param string|bool $focus_dimension - if you doesn't want to lose the aspect ratio, set which "new" value should be ignored or set this to false to scale the image from both dimensions
  358.      * @param string|int|bool $new_type - type of the new image on false we use the type of the original image
  359.      * @return bool|image - false on error on success the new image ressource
  360.      */
  361.     public function resizeImg($new_height = 0, $new_width = 0, $focus_dimension = false, $new_type = false) {
  362.         switch($focus_dimension) {
  363.             case 'width':
  364.                 $factor = $this->getWidth() / $new_width;
  365.                 if($factor < 1)
  366.                     $new_height = (int) ceil($this->getHeight() * $factor);
  367.                 else
  368.                     $new_height = (int) ceil($this->getHeight() / $factor);
  369.                 break;
  370.             case 'height':
  371.                 $factor = $this->getHeight() / $new_height;
  372.                 if($factor < 1)
  373.                     $new_width = (int) ceil($this->getWidth() * $factor);
  374.                 else
  375.                     $new_width = (int) ceil($this->getWidth() / $factor);
  376.                 break;
  377.         }
  378.  
  379.         if(is_string($new_type))
  380.             $new_type = self::get_valid_type($new_type);
  381.         else if(! $new_type)
  382.             $new_type = $this->getType();
  383.  
  384.         if($new_height <= 0 || $new_width <= 0)
  385.             return false;
  386.  
  387.         $tmp_img_path = $this->getImageDir() . DS . $this->getImageName() . '_tmp' . time() . $this->getImageExt();
  388.  
  389.         // Create new image
  390.         $new_image = new image($tmp_img_path, $new_type, $new_height, $new_width, true);
  391.  
  392.         // Resize
  393.         imagecopyresampled($new_image->getImage(), $this->getImage(), 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
  394.  
  395.         return $new_image;
  396.     }
  397.  
  398.     /**
  399.      * Resize image and cut away overlaying borders, so that the proportions are the same as before
  400.      *
  401.      * todo improve
  402.      *
  403.      * @param int $new_height - height of the new image
  404.      * @param int $new_width - width of the new image
  405.      * @param bool $new_type - type of the new image on default use the current image type of the "old" image
  406.      * @return image - center cutted image
  407.      */
  408.     public function centerCutImg($new_height = 0, $new_width = 0, $new_type = false) {
  409.         $start_height = 0;
  410.         $start_width = 0;
  411.  
  412.         // Detect new factor
  413.         if($new_height > $new_width) {
  414.             $new_factor = $new_height / $new_width;
  415.             $new_big_side = 'height';
  416.         } else {
  417.             $new_factor = $new_width / $new_height;
  418.             $new_big_side = 'width';
  419.         }
  420.  
  421.         // Detect image factor
  422.         if($this->getHeight() > $this->getWidth()) {
  423.             $factor = $this->getHeight() / $this->getWidth();
  424.             $big_side = 'height';
  425.         } else {
  426.             $factor = $this->getWidth() / $this->getHeight();
  427.             $big_side = 'width';
  428.         }
  429.  
  430.         // Check if factor matches, if not make correction
  431.         if($factor != $new_factor || $new_big_side != $big_side) {
  432.             unset($factor);
  433.             // Set default height/width
  434.             $tmp_height = $this->getHeight();
  435.             $tmp_width = $this->getWidth();
  436.  
  437.             // Calc sizes
  438.             // Calc down if image sizes are bigger than dest sizes
  439.             $bigger = false;
  440.             while($new_height < $tmp_height && $new_width < $tmp_width) {
  441.                 if($big_side == 'height') {
  442.                     $tmp_width--;
  443.                     $tmp_height = $tmp_height - (1 / $new_factor);
  444.                 } else {
  445.                     $tmp_height--;
  446.                     $tmp_width = $tmp_width - (1 / $new_factor);
  447.                 }
  448.                 $bigger = true;
  449.             }
  450.  
  451.             // Calc up image, if its smaller than dest img
  452.             while(($new_height > $tmp_height || $new_width > $tmp_width) && ! $bigger) {
  453.                 if($big_side == 'height') {
  454.                     $tmp_width++;
  455.                     $tmp_height = $tmp_height + (1 / $new_factor);
  456.                 } else {
  457.                     $tmp_height++;
  458.                     $tmp_width = $tmp_width + (1 / $new_factor);
  459.                 }
  460.             }
  461.  
  462.             // Resize image to size
  463.             $new_img = $this->resizeImg((int) floor($tmp_height), (int) floor($tmp_width), false, $new_type);
  464.  
  465.             // Calc Start Positions
  466.             $height_diff = $new_height - $tmp_height;
  467.             if($height_diff < 0)
  468.                 $height_diff = $height_diff * -1;
  469.             if($height_diff != 0)
  470.                 $start_height = (int) ceil($height_diff / 2);
  471.  
  472.             $width_diff = $new_width - $tmp_width;
  473.             if($width_diff < 0)
  474.                 $width_diff = $width_diff * -1;
  475.             if($width_diff != 0)
  476.                 $start_width = (int) ceil($width_diff / 2);
  477.  
  478.             // Cut of overlapping pixels and overwrite current img
  479.             return $new_img->cutImage($start_height, $start_width, $tmp_height - $height_diff, $tmp_width - $width_diff, $new_type);
  480.         }
  481.  
  482.         // Resize and return new image
  483.         return $this->resizeImg($new_height, $new_width, false, $new_type);
  484.     }
  485.  
  486.     /**
  487.      * Cut an image on the specific coordinates
  488.      *
  489.      * @param int $start_height - height, where you want to start cutting
  490.      * @param int $start_width - width, where you want to start cutting
  491.      * @param int $new_height - height of the new image
  492.      * @param int $new_width - width of the new image
  493.      * @param bool $new_type - type of the new image on default use the type of the "old" image
  494.      * @return image - cutted image
  495.      */
  496.     public function cutImage($start_height = 0, $start_width = 0, $new_height = 1, $new_width = 1, $new_type = false) {
  497.         if(is_string($new_type))
  498.             $new_type = self::get_valid_type($new_type);
  499.         else if(! $new_type)
  500.             $new_type = $this->getType();
  501.  
  502.         $new_image = new image($this->getImageDir() . DS . $this->getImageName() . '_tmp_cutImg' . time() . $this->getImageExt(), $new_type, $new_height, $new_width, true);
  503.  
  504.         imagecopy($new_image->getImage(), $this->getImage(), 0, 0, $start_width, $start_height, $new_width, $new_height);
  505.  
  506.         return $new_image;
  507.     }
  508.  
  509.     /**
  510.      * Add an text to the image
  511.      *
  512.      * @param int $start_height - height, where you want to start to place the text
  513.      * @param int $start_width - width where you want to start the text
  514.      * @param string $text - The text
  515.      * @param array $color_rgb - color RGB array
  516.      * @param int $font_size - size of the text
  517.      * @param string $ttf - font type
  518.      * @param int $angle - angle
  519.      * @param bool $ignore - ignore when the text is larger than the image
  520.      * @return array|bool - All four corners of the text with and height (8 object array) See: http://php.net/imagettftext | false on error
  521.      * @throws Exception - Can't draw
  522.      */
  523.     public function addText($start_height = 0, $start_width = 0, $text = "", $color_rgb = array(0, 0, 0), $font_size = 12, $ttf = 'ARIAL', $angle = 0, $ignore = false) {
  524.         if(! function_exists('imagettftext') || ! function_exists('imageftbbox'))
  525.             throw new Exception('Cannot draw Text on Image, Functions doesn\'t exists!...');
  526.  
  527.         // Get Text-Size informations
  528.         $positions = imageftbbox($font_size, $angle, $ttf, $text);
  529.  
  530.         // Add Start values to positions
  531.         $positions[0] = $positions[0] + $start_width;
  532.         $positions[1] = $positions[1] + $start_height;
  533.         $positions[2] = $positions[2] + $start_width;
  534.         $positions[3] = $positions[3] + $start_height;
  535.         $positions[4] = $positions[4] + $start_width;
  536.         $positions[5] = $positions[5] + $start_height;
  537.         $positions[6] = $positions[6] + $start_width;
  538.         $positions[7] = $positions[7] + $start_height;
  539.  
  540.         // Check if text overlaps image borders
  541.         if(! $ignore) {
  542.             if ($positions[1] > $this->getHeight() || $positions[2] > $this->getWidth() || $positions[3] > $this->getHeight() || $positions[4] > $this->getWidth())
  543.                 return false;
  544.             if ($positions[0] < 0 || $positions[5] < 0 || $positions[6] < 0 || $positions[7] < 0)
  545.                 return false;
  546.         }
  547.  
  548.         return imagettftext($this->getImage(), $font_size, $angle, $start_width, $start_height, $this->RGB($color_rgb[0], $color_rgb[1], $color_rgb[2]), $ttf, $text);
  549.     }
  550.  
  551.     /**
  552.      * Returns the with & height of a single Character
  553.      *
  554.      * @param string $char - Character
  555.      * @param int $font_size - Size of the text
  556.      * @param string $ttf - font type
  557.      * @return array - height and width of the character
  558.      */
  559.     public static function getCharWidth($char, $font_size = 12, $ttf = 'ARIAL') {
  560.         $sizes = imageftbbox($font_size, 0, $ttf, $char);
  561.  
  562.         return array('h' => $sizes[3], 'w' => $sizes[2]);
  563.     }
  564.  
  565.     /**
  566.      * Returns an RGB-Color
  567.      *
  568.      * @param int $r - Red 0 - 255
  569.      * @param int $g - Green 0 - 255
  570.      * @param int $b - Blue 0 - 255
  571.      * @return int|bool - The Color identifier | false on error
  572.      */
  573.     private function RGB($r = 0, $g = 0, $b = 0) {
  574.         return ImageColorAllocate($this->getImage(), $r, $g, $b);
  575.     }
  576.  
  577.     /**
  578.      * Returns an RGBA-Color
  579.      *
  580.      * @param int $r - Red 0 - 255
  581.      * @param int $g - Green 0 - 255
  582.      * @param int $b - Blue 0 - 255
  583.      * @param int $a - Alpha (Opacity) 0 - 127
  584.      * @return int|bool - The Color identifier | false on error
  585.      */
  586.     private function RGBA($r =0, $g = 0, $b = 0, $a = 127) {
  587.         return imagecolorallocatealpha($this->getImage(), $r, $g, $b, $a);
  588.     }
  589.  
  590.     /**
  591.      * Fill the Background of the Image with an specific color
  592.      *
  593.      * @param array $color - Color array
  594.      * @return bool - true on success and false on error
  595.      */
  596.     public function fillBG($color = array(0, 0, 0)) {
  597.         if(count($color) == 3)
  598.             return imagefill($this->getImage(), 0, 0, $this->RGB($color[0], $color[1], $color[2]));
  599.         else {
  600.             $tmp_color = $this->RGB($color[0], $color[1], $color[2]);
  601.             imagecolortransparent($this->getImage(), $tmp_color);
  602.             return imagefill($this->getImage(), 0, 0, $this->RGBA($color[0], $color[1], $color[2], $color[3]));
  603.         }
  604.     }
  605.  
  606.     /**
  607.      * Shows an image directly (HTML)
  608.      *
  609.      * @param string $alt_msg - Message if the image can't be shown
  610.      * @throws Exception - image type not supported
  611.      */
  612.     public function showImage($alt_msg = 'Image') {
  613.         // Create Image
  614.         switch($this->getType()) {
  615.             case IMAGETYPE_JPEG:
  616.             //case IMG_JPG:
  617.             //case IMG_JPEG:
  618.                 // Start Buffering
  619.                 ob_start();
  620.  
  621.                 imagejpeg($this->getImage());
  622.  
  623.                 // Get Buffer and delete it
  624.                 $bytes = ob_get_clean();
  625.                 $type = 'jpeg';
  626.                 break;
  627.             case IMAGETYPE_PNG:
  628.             //case IMG_PNG:
  629.                 // Start Buffering
  630.                 ob_start();
  631.  
  632.                 imagepng($this->getImage());
  633.  
  634.                 // Get Buffer and delete it
  635.                 $bytes = ob_get_clean();
  636.                 $type = 'png';
  637.                 break;
  638.             case IMAGETYPE_GIF:
  639.             //case IMG_GIF:
  640.                 // Start Buffering
  641.                 ob_start();
  642.  
  643.                 imagegif($this->getImage());
  644.  
  645.                 // Get Buffer and delete it
  646.                 $bytes = ob_get_clean();
  647.                 $type = 'gif';
  648.                 break;
  649.             default:
  650.                 ob_end_clean();
  651.                 throw new Exception('Image type is not supported!');
  652.         }
  653.  
  654.         echo "<img src=\"data:image/{$type};base64," . base64_encode($bytes) . "\" alt=\"{$alt_msg}\">";
  655.     }
  656.  
  657.     /**
  658.      * Add an other image on top/below of this image
  659.      *
  660.      * @param image $image - the new image object
  661.      * @param bool $above - is the new image above or below the old image (true = above | false = below)
  662.      * @param bool $new_type - new type of the image
  663.      * @return image - new image
  664.      * @throws Exception - Image is not an object nor an image object
  665.      */
  666.     public function add_image($image, $above = true, $new_type = false) {
  667.         if(is_string($new_type))
  668.             $new_type = self::get_valid_type($new_type);
  669.         else if(! $new_type)
  670.             $new_type = $this->getType();
  671.  
  672.         if(gettype($image) != 'object')
  673.             throw new Exception(__CLASS__ . '->' .__FUNCTION__ . ': Image is not an Object');
  674.         if(get_class($image) != 'image')
  675.             throw new Exception(__CLASS__ . '->' .__FUNCTION__ . ': Image is not an Image Object');
  676.  
  677.         if($above) {
  678.             $new_height = $this->getHeight();
  679.             $new_width = $this->getWidth();
  680.             $destimg = $this;
  681.             $srcimg = $image;
  682.         } else {
  683.             $new_height = $image->getHeight();
  684.             $new_width = $image->getWidth();
  685.             $destimg = $image;
  686.             $srcimg = $this;
  687.         }
  688.  
  689.         ImageCopy($destimg->getImage(), $srcimg->getImage(), 0, 0, 0, 0, $srcimg->getWidth(), $srcimg->getHeight());
  690.  
  691.         return $destimg;
  692.     }
  693. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement