Skip to content

Instantly share code, notes, and snippets.

@garyrojas
Last active July 3, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garyrojas/d0e70635680343cf2c4864818872b2c7 to your computer and use it in GitHub Desktop.
Save garyrojas/d0e70635680343cf2c4864818872b2c7 to your computer and use it in GitHub Desktop.
<?php
class ResizeImage {
var $image;
var $image_type;
static function makeDir($path) {
return is_dir($path) || mkdir($path, 0777, true);
}
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
$image_type=$this->image_type;
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagealphablending($this->image, false);
imagesavealpha($this->image,true);
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resizeBackground($width,$height,$forcesize='n', $bg="transparent") {
if ($forcesize == 'n') {
if ($width > $this->getWidth() && $height > $this->getHeight()){
$width = $this->getWidth();
$height = $this->getHeight();
}
}
$new_image = imagecreatetruecolor($width, $height);
if(($this->image_type == IMAGETYPE_GIF) || ($this->image_type==IMAGETYPE_PNG)){
//Por defecto es transparente
$bgSet=imagecolorallocatealpha($new_image, 255, 255, 255, 127);
//Aquí el color blanco
if($bg=="white") {
$bgSet=imagecolorallocate($im,255,255,255);
}
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
imagefilledrectangle($new_image, 0, 0, $width, $height, $bgSet);
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
/*
$image = new ResizeImage();
$image->load('picture.jpg');
$image->resizeBackground(500,500,'n','white');
$image->save('picture2.jpg');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment