Rev 1 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpnamespace LeadersLinked\Library;abstract class Image{/**** @param string $source* @param string $target_path* @param string $target_prefix* @param array $sizes* @param string $sufix* @return boolean*//*public static function upload($source, $target_path, $target_prefix, $sizes = [], $sufix = ''){try {$data = file_get_contents($source);$img = imagecreatefromstring($data);if(!file_exists($target_path)) {mkdir($target_path, 0755);}if($img) {list($source_width, $source_height) = getimagesize($source);foreach($sizes as $size){list($target_width, $target_height) = explode('x', $size);$x = false;if($x) {$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);}$width_ratio = $target_width / $source_width;$height_ratio = $target_height / $source_height;$radio = $width_ratio > $height_ratio ? $height_ratio : $width_ratio;$resized_width = round($source_width * $radio);$resized_height = round($source_height * $radio);$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_prefix . $size . $sufix . '.png';if(file_exists($target)) {@unlink($target);}imagepng($new_image, $target);}}unlink($source);return true;}catch (\Throwable $e){error_log($e->getTraceAsString());return false;}}*//**** @param string $source* @param string $target_path* @param string $target_filename* @param number $target_width* @param number $target_height* @return boolean*/public static function uploadImageWithoutCompletationSize($source, $target_path, $target_filename, $target_width, $target_height){try {$data = file_get_contents($source);$img = imagecreatefromstring($data);if(!file_exists($target_path)) {mkdir($target_path, 0755);}if($img) {list($source_width, $source_height) = getimagesize($source);$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);$new_image = imageCreateTrueColor($resized_width, $resized_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 , 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);$target = $target_path . DIRECTORY_SEPARATOR . $target_filename;if(file_exists($target)) {@unlink($target);}imagepng($new_image, $target);}unlink($source);return true;}catch (\Throwable $e){error_log($e->getTraceAsString());return false;}}/**** @param string $source* @param string $target_path* @param string $target_filename* @param number $target_width* @param number $target_height* @param boolean $crop_to_dimensions* @return boolean*/public static function uploadImage($source, $target_path, $target_filename, $target_width, $target_height, $crop_to_dimensions = true ){try {$data = file_get_contents($source);$img = imagecreatefromstring($data);if(!file_exists($target_path)) {mkdir($target_path, 0755);}if($img) {list($source_width, $source_height) = getimagesize($source);if($crop_to_dimensions) {$width_ratio = $target_width / $source_width;//print 'Width Ratio: ' . $width_ratio . '<br />';$height_ratio = $target_height / $source_height;//print 'Height Ratio: ' . $height_ratio . '<br />';if($width_ratio > $height_ratio) {//print 'Width Wins<br />';$resized_width = $target_width;$resized_height = $source_height * $width_ratio;} else {//print 'Height Wins<br />';$resized_height = $target_height;$resized_width = $source_width * $height_ratio;}} else {$width_ratio = $target_width / $source_width;//print 'Width Ratio: ' . $width_ratio . '<br />';$resized_width = $target_width;$resized_height = $source_height * $width_ratio;//print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />';if($resized_height > $target_height) {$height_ratio = $target_height / $resized_height;$resized_height = $target_height;//print 'Height Ratio: ' . $height_ratio . '<br />';$resized_width = $resized_width * $height_ratio;//print 'Resized: ' . $resized_width . 'x' . $resized_height . '<br />';}}/*$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);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);}imagepng($new_image, $target);}unlink($source);return true;}catch (\Throwable $e){error_log($e->getTraceAsString());return false;}}/**** @param string $source* @param string $target_path* @param string $target_filename* @return boolean*/public static function uploadFile($source, $target_path, $target_filename){try {$data = file_get_contents($source);$img = imagecreatefromstring($data);if(!file_exists($target_path)) {mkdir($target_path, 0755);}if($img) {list($source_width, $source_height) = getimagesize($source);$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);}imagepng($new_image, $target);}unlink($source);return true;}catch (\Throwable $e){error_log($e->getTraceAsString());return false;}}/**** @param string $path* @param string $filename* @return boolean*/public static function delete($path, $filename){try {if (is_dir($path)){if ($dh = opendir($path)) {while (($file = readdir($dh)) !== false){if($file == '.' || $file == '..') {continue;}if($file == $filename) {unlink($path . DIRECTORY_SEPARATOR . $file);}}closedir($dh);}}return true;}catch (\Throwable $e){error_log($e->getTraceAsString());return false;}}}