Rev 17015 | 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 array
*/
private $config;
/**
*
* @param array $config
*/
private function __construct($config)
{
$this->config = $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;
}
/**
*
* @param string $source
* @param string $target_filename
* @param number $target_width
* @param number $target_height
* @param boolean $crop_to_dimensions
* @return boolean
*/
public function uploadProcessChangeSize($source, $target_filename, $target_width, $target_height, $crop_to_dimensions)
{
$temp_filename = '';
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 = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $target_filename;
imagepng($new_image, $temp_filename);
} else {
$temp_filename = '';
}
}
catch (\Throwable $e)
{
$temp_filename = '';
}
@unlink($source);
return $temp_filename;
}
/**
*
* @param string $source
* @param string $target_filename
* @return boolean
*/
public function uploadProcessWithOutChangeSize($source, $target_filename)
{
$temp_filename = '';
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);
$temp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $target_filename;
imagepng($new_image, $temp_filename);
} else {
$temp_filename = '';
}
}
catch (\Throwable $e)
{
$temp_filename = '';
}
@unlink($source);
return $temp_filename;
}
}