Skip to content

Instantly share code, notes, and snippets.

@miguelfrmn
Last active May 9, 2023 11:23
Show Gist options
  • Save miguelfrmn/908143 to your computer and use it in GitHub Desktop.
Save miguelfrmn/908143 to your computer and use it in GitHub Desktop.
SimpleImage PHP Class
<?php
/**
* File: SimpleImage.php
* Author: Simon Jarvis
* Modified by: Miguel Fermín
* Based in: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*/
class SimpleImage {
public $image;
public $image_type;
public function __construct($filename = null){
if (!empty($filename)) {
$this->load($filename);
}
}
public 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);
} else {
throw new Exception("The file you're trying to open is not supported");
}
}
public function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null) {
if ($image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image,$filename,$compression);
} elseif ($image_type == IMAGETYPE_GIF) {
imagegif($this->image,$filename);
} elseif ($image_type == IMAGETYPE_PNG) {
imagepng($this->image,$filename);
}
if ($permissions != null) {
chmod($filename,$permissions);
}
}
public function output($image_type=IMAGETYPE_JPEG, $quality = 80) {
if ($image_type == IMAGETYPE_JPEG) {
header("Content-type: image/jpeg");
imagejpeg($this->image, null, $quality);
} elseif ($image_type == IMAGETYPE_GIF) {
header("Content-type: image/gif");
imagegif($this->image);
} elseif ($image_type == IMAGETYPE_PNG) {
header("Content-type: image/png");
imagepng($this->image);
}
}
public function getWidth() {
return imagesx($this->image);
}
public function getHeight() {
return imagesy($this->image);
}
public function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = round($this->getWidth() * $ratio);
$this->resize($width,$height);
}
public function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = round($this->getHeight() * $ratio);
$this->resize($width,$height);
}
public function square($size) {
$new_image = imagecreatetruecolor($size, $size);
if ($this->getWidth() > $this->getHeight()) {
$this->resizeToHeight($size);
imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopy($new_image, $this->image, 0, 0, ($this->getWidth() - $size) / 2, 0, $size, $size);
} else {
$this->resizeToWidth($size);
imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopy($new_image, $this->image, 0, 0, 0, ($this->getHeight() - $size) / 2, $size, $size);
}
$this->image = $new_image;
}
public function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getHeight() * $scale/100;
$this->resize($width,$height);
}
public function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
public function cut($x, $y, $width, $height) {
$new_image = imagecreatetruecolor($width, $height);
imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopy($new_image, $this->image, 0, 0, $x, $y, $width, $height);
$this->image = $new_image;
}
public function maxarea($width, $height = null) {
$height = $height ? $height : $width;
if ($this->getWidth() > $width) {
$this->resizeToWidth($width);
}
if ($this->getHeight() > $height) {
$this->resizeToheight($height);
}
}
public function minarea($width, $height = null) {
$height = $height ? $height : $width;
if ($this->getWidth() < $width) {
$this->resizeToWidth($width);
}
if ($this->getHeight() < $height) {
$this->resizeToheight($height);
}
}
public function cutFromCenter($width, $height) {
if ($width < $this->getWidth() && $width > $height) {
$this->resizeToWidth($width);
}
if ($height < $this->getHeight() && $width < $height) {
$this->resizeToHeight($height);
}
$x = ($this->getWidth() / 2) - ($width / 2);
$y = ($this->getHeight() / 2) - ($height / 2);
return $this->cut($x, $y, $width, $height);
}
public function maxareafill($width, $height, $red = 0, $green = 0, $blue = 0) {
$this->maxarea($width, $height);
$new_image = imagecreatetruecolor($width, $height);
$color_fill = imagecolorallocate($new_image, $red, $green, $blue);
imagefill($new_image, 0, 0, $color_fill);
imagecopyresampled( $new_image,
$this->image,
floor(($width - $this->getWidth())/2),
floor(($height-$this->getHeight())/2),
0, 0,
$this->getWidth(),
$this->getHeight(),
$this->getWidth(),
$this->getHeight()
);
$this->image = $new_image;
}
}
// Usage:
// Load the original image
$image = new SimpleImage('lemon.jpg');
// Resize the image to 600px width and the proportional height
$image->resizeToWidth(600);
$image->save('lemon_resized.jpg');
// Create a squared version of the image
$image->square(200);
$image->save('lemon_squared.jpg');
// Scales the image to 75%
$image->scale(75);
$image->save('lemon_scaled.jpg');
// Resize the image to specific width and height
$image->resize(80,60);
$image->save('lemon_resized2.jpg');
// Resize the canvas and fill the empty space with a color of your choice
$image->maxareafill(600,400, 32, 39, 240);
$image->save('lemon_filled.jpg');
// Output the image to the browser:
$image->output();
@LeChatNoir69
Copy link

Hi,

When jpg file has exif information, sometimes, orientation is revert while resizing using this class.
In the load function, I've fixed it like this :

function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
$exif = exif_read_data($filename);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$this->image = imagerotate($this->image,90,0);
break;
case 3:
$this->image = imagerotate($this->image,180,0);
break;
case 6:
$this->image = imagerotate($this->image,-90,0);
break;
}
}
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
} elseif( $this->image_type == IMAGETYPE_BMP ) {
$this->image = imagecreatefrombmp($filename);
}

}

@xavianaxw
Copy link

Hi guys.

I was using simpleimage to do some image formatting on my project. Currently I have a setup using resize() which I intend to resize all image to 700x600 pixels. I have manage to setup everything and the project is LIVE'd already but during production, there was a case where the image would resize to 699x600 while the others are all 700x600 thus breaking the layout.

Was wondering if anyone faced this before?

Thanks

@Kurariahunter
Copy link

Kurariahunter commented Dec 15, 2016

I offer a to screen output rather then using headers. Post kepts eating my img tags =( lol

    public function output($image_type = IMAGETYPE_JPEG, $quality = 80)
    {
        if ($image_type == IMAGETYPE_JPEG) {
            //header("Content-type: image/jpeg");
            // start buffering
            ob_start();
            // output jpeg (or any other chosen) format & quality
            imagejpeg($this->image, NULL, 85);
            // capture output to string
            $contents = ob_get_contents();
            // end capture
            ob_end_clean();
            // be tidy; free up memory
            imagedestroy($this->image);
            $data = base64_encode($contents);
            echo '<img src="data:image/jpg;base64,'. $data .'" />';
        } elseif ($image_type == IMAGETYPE_GIF) {
            //header("Content-type: image/gif");
            // start buffering
            ob_start();
            // output gif (or any other chosen) format & quality
            imagegif($this->image);
            // capture output to string
            $contents = ob_get_contents();
            // end capture
            ob_end_clean();
            // be tidy; free up memory
            imagedestroy($this->image);
            $data = base64_encode($contents);
            echo '<img src="data:image/gif;base64,'. $data .'" />';
        } elseif ($image_type == IMAGETYPE_PNG) {
            //header("Content-type: image/png");
            // start buffering
            ob_start();
            // output png (or any other chosen) format & quality
            imagepng($this->image);
            // capture output to string
            $contents = ob_get_contents();
            // end capture
            ob_end_clean();
            // be tidy; free up memory
            imagedestroy($this->image);
            $data = base64_encode($contents);
            echo '<img src="data:image/png;base64,'. $data .'" />';
        }
    }

@crisdany6
Copy link

Hello

I wanted to use this function for my image by downloading the images with file_get_contents

My code does not work
$url=ImageCreateFromString(file_get_contents('http://www.bicivendita.it/WebRoot/registerit3/Shops/990241304/509A/9674/BCB8/5136/52FE/C0A8/8007/4E62/BICICLETTE_DA_CITTA_LEGGERE_DA_UOMO_FUORISERIE.jpg'));
$image = new SimpleImage($url);
It's possible?
Regards

@zaherkh
Copy link

zaherkh commented Jun 12, 2020

Great code. Thanks

@webmavie
Copy link

Hi i added new function text watermark
`function watermark_text($waterMarkText, $font=4, $r=158, $g=158, $b=158) {
$width = imagefontwidth($font) * strlen($waterMarkText) ;

   $height = imagefontheight($font) ;
    
   $image=$this->image;
    
   $x = $this->getWidth() - $width - 10;
    
   $y = $this->getHeight() - $height - 10;
    
   $backgroundColor = imagecolorallocate ($image, 255,255,255);
    
   $textColor = imagecolorallocate ($image, $r,$g,$b);
    
   imagestring ($image, $font, $x, $y, $waterMarkText, $textColor);

   $this->image = $image;

}`

Use:
$img->watermark_text('For Example.com', 8, 215, 13, 213)

@miguel-estevez
Copy link

Miguel, gracias por la librería. Considera incluir la aportación de LeChatNoir69
LeChatNoir69
Perfect fix.
Tks!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment