Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17012 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php
namespace LeadersLinked\Library;


class Image 
{
    
    /**
     *
     * @var \LeadersLinked\Library\Image
     */
    private static $_instance;
    
    /**
     *
     * @var \LeadersLinked\Library\Storage
     */
    private $storage;
    
    /**
     * 
     * @var array
     */
    private $config;
    
    /**
     *
     * @param array $config
     */
    private function __construct($config)
    {
        $this->config = $config;
        $this->storage = Storage::getInstance($config);
    }
    
    /**
     *
     * @param array $config
     * @return \LeadersLinked\Library\Image
     */
    public static function getInstance($config)
    {
        if(self::$_instance == null) {
            self::$_instance = new Image($config);
        }
        
        return self::$_instance;
    }
    
    /**
     * 
     * @return \LeadersLinked\Library\Storage
     */
    public function getStorage()
    {
        return $this->storage;
    }
    
   
    
    /**
     * 
     * @param string $source
     * @param string $target_path
     * @param string $target_code
     * @param string $target_filename
     * @param number $target_width
     * @param number $target_height
     * @param boolean $crop_to_dimensions
     * @param boolean $unlink_source
     * @return boolean
     */
    public function uploadImageChangeSize($source, $target_path, $target_code, $target_filename, $target_width, $target_height,  $crop_to_dimensions, $unlink_source)
    {
        
       
        
        try {

            $data = file_get_contents($source);
            $img = imagecreatefromstring($data);
            
            
            if($img) {
                list($source_width, $source_height) = getimagesize($source);
                
                if($crop_to_dimensions) {
                    $width_ratio = $target_width / $source_width;
                    $height_ratio = $target_height / $source_height;
                    if($width_ratio > $height_ratio) {
                        $resized_width = $target_width;
                        $resized_height = $source_height * $width_ratio;
                    } else {
                        $resized_height = $target_height;
                        $resized_width = $source_width * $height_ratio;
                    }
                } else {
                    $width_ratio = $target_width / $source_width;
                    $resized_width = $target_width;
                    $resized_height = $source_height * $width_ratio;
                    if($resized_height > $target_height) {
                        $height_ratio = $target_height / $resized_height;
                        $resized_height = $target_height;
                        $resized_width = $resized_width * $height_ratio;
                    }
                }
 
                    
                $resized_width = round($resized_width);
                $resized_height = round($resized_height);
                    
                $offset_width = round(($target_width - $resized_width) / 2);
                $offset_height = round(($target_height - $resized_height) / 2);
                    
                    
                $new_image = imageCreateTrueColor($target_width, $target_height);
                imageAlphaBlending($new_image, False);
                imageSaveAlpha($new_image, True);
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
                imagefill($new_image, 0, 0, $transparent);
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
                
                    
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
                imagepng($new_image, $temp_filename);
                
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
                unlink($temp_filename);
                
                
                if($unlink_source) {
                    @unlink($source);
                }
                
                
                
                return $result;
            } else {
                
                
                if($unlink_source) {
                    @unlink($source);
                }
                
            
                return true;
            }
            
        }
        catch (\Throwable $e)
        {
            
            if($unlink_source) {
                @unlink($source);
            }
            
            
            error_log($e->getTraceAsString());
            return false;
        }
    }
    
    /**
     * 
     * @param string $source
     * @param string $target_path
     * @param string $target_code
     * @param string $target_filename
     * @param boolean $unlink_source
     * @return boolean
     */
    public function uploadImageRaw($source, $target_path, $target_code, $target_filename, $unlink_source)
    {
        
        
        
        try {
            $data = file_get_contents($source);
    
            
            $img = imagecreatefromstring($data);

            if($img) {
                list($source_width, $source_height) = getimagesize($source);
                
                $target_width = $source_width;
                $target_height =  $source_height;
                
                $width_ratio    = $target_width / $source_width;
                $height_ratio   = $target_height / $source_height;
                if($width_ratio > $height_ratio) {
                    $resized_width = $target_width;
                    $resized_height = $source_height * $width_ratio;
                } else {
                    $resized_height = $target_height;
                    $resized_width = $source_width * $height_ratio;
                }
                
                $resized_width = round($resized_width);
                $resized_height = round($resized_height);
                
                $offset_width = round(($target_width - $resized_width) / 2);
                $offset_height = round(($target_height - $resized_height) / 2);
                
                
                $new_image = imageCreateTrueColor($target_width, $target_height);
                imageAlphaBlending($new_image, False);
                imageSaveAlpha($new_image, True);
                $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
                imagefill($new_image, 0, 0, $transparent);
                imageCopyResampled($new_image, $img , $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
                
                
                $target = $target_path . DIRECTORY_SEPARATOR . $target_filename;
                if(file_exists($target)) {
                    @unlink($target);
                }
                
                $temp_filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $target_filename;
                imagepng($new_image, $temp_filename);
                
                $result = $this->storage->putFile($target_path, $target_code, $temp_filename);
                unlink($temp_filename);
                
                if($unlink_source) {
                    unlink($source);
                }
                
                return $result;
            }
            
            if($unlink_source) {
                unlink($source);
            }
            
            
            return true;
            
        }
        catch (\Throwable $e)
        {
            if($unlink_source) {
                unlink($source);
            }
            
            error_log($e->getTraceAsString());
            return false;
        }
    }
}