Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » PHP GD IMAGE » Image class resize and rounded mask

Image class resize and rounded mask

02/02/2012 by Dux   -   5/5 (1 ratings)

This PHP GD class offers several useful features. It resizes images, apply a rounded mask with various options :
- output size
- crop mode : crop to fill, fill to fit
- rounded corners or square
- color of the rounded mask

It is complete and usable in all cases.
This class use the PNG mask round_440.png

Demo.

PNG mask round_440.png (440 x 440px)
masque rond
3 types of image result
rounded mask image PHP GD blue rounded mask image PHP GD square mask image PHP GD

source code



 
<?php
ini_set
('memory_limit', '128M');


class
imagesite {
   
   private
$ext, $newimg;
   public
$url_img, $sizi, $crop, $bg;
   
   
/****************************************************/
   // FUNC : Direct send
   
private function direct_send() {
       
$img = file_get_contents($this->url_img);
       if (
$this->ext == "jpg" or $this->ext == "jpeg") header("Content-type: image/jpeg");
       if (
$this->ext == "gif") header("Content-type: image/gif");
       if (
$this->ext == "png") header("Content-type: image/png");
       echo
$img;
   }
   
   
/****************************************************/
   // FUNC : creating image
   
private function createimg() {
       if (
$this->ext == "jpg" or $this->ext == "jpeg") $this->img = imagecreatefromjpeg($this->url_img);
       if (
$this->ext == "gif") $this->img = imagecreatefromgif($this->url_img);
       if (
$this->ext == "png") $this->img = imagecreatefrompng($this->url_img);
       
       
$this->redim();
       if (
$this->carre == "") $this->format_circle();
       
$this->send_img();
       
$this->destroy_work();
   }
   
   
/****************************************************/
   // FUNC : resizing
   
private function redim() {
       
       list(
$x,$y) = getimagesize($this->url_img);
       
       if (
$this->crop == "") {
           
// Crop to fill
           
if ($x > $y) {
               
$yr = $this->sizi; $xr = $x / ($y/$this->sizi);
           } else {
               
$xr = $this->sizi; $yr = $y / ($x/$this->sizi);
           }
       
           
// New blank image
           
$this->newimg = imagecreatetruecolor($this->sizi, $this->sizi);
           
// Copy into new blank image
           
imagecopyresampled( $this->newimg , $this->img , -($xr-$this->sizi)/2 , -($yr-$this->sizi)/2 , 0 , 0 , $xr , $yr , $x , $y);
       } elseif (
$this->crop == "fit") {
           
// Fit to fill
           
if ($x < $y) {
               
$yr = $this->sizi; $xr = $x / ($y/$this->sizi);
           } else {
               
$xr = $this->sizi; $yr = $y / ($x/$this->sizi);
           }
       
           
// New blank image
           
$this->newimg = imagecreatetruecolor($xr, $yr);
           
// Copy into new blank image
           
imagecopyresampled( $this->newimg , $this->img , 0 , 0 , 0 , 0 , $xr , $yr , $x , $y);
       }
       
       
imagedestroy($this->img);
   }

   
/****************************************************/
   // FUNC : rounded squares
   
private function format_circle() {
       
// Masque rond
       
$masque = imagecreatefrompng('round_440.png');
       
       
// Background color
       
$r = hexdec(substr($this->bg, 0, 2));
       
$v = hexdec(substr($this->bg, 2, 2));
       
$b = hexdec(substr($this->bg, 4, 2));
       
$colorbg = imagecolorallocate($masque, $r, $v, $b);
       
imagefill($masque, 1,1, $colorbg);
       
imagefill($masque, 439,1, $colorbg);
       
imagefill($masque, 439,439, $colorbg);
       
imagefill($masque, 1,439, $colorbg);
       
       
imagecopyresampled( $this->newimg , $masque , 0 , 0 , 0 , 0 , $this->sizi , $this->sizi , 440 , 440);
       
imagedestroy($masque);
   }
   
   
/****************************************************/
   // Send image
   
private function send_img() {
       if (
$this->ext == "jpg" or $this->ext == "jpeg") {
           
header("Content-type: image/jpeg");
           
imageJPEG($this->newimg, '', 95);
       } elseif (
$this->ext == "gif") {
           
header("Content-type: image/gif");
           
imageGIF($this->newimg);
       } elseif (
$this->ext == "png") {
           
header("Content-type: image/png");
           
imagePNG($this->newimg);
       }
   }
   
   
// Destroy
   
private function destroy_work() {
       
imagedestroy($this->newimg);
   }
   
   
/****************************************************/
   // Init function
   
public function init_create() {
       
// File extension
       
$part = pathinfo($this->url_img);
       
$this->ext = strtolower($part['extension']);
       
       
// Resizing or not
       
if ($this->sizi == "original" or $this->sizi == "") $this->direct_send(); else $this->createimg();
   }

}




$img = new imagesite();
// Path to image
$img->url_img = "shakira.jpg";
// Output size (in pixels or set "original" for no resize)
$img->sizi = 170;
// Background color (format FFFFFF)
$img->bg = "FFFFFF";
// Crop mode : 0 = crop to fill, "fit" = fit to fill
$img->crop = 0;
// Corners type : 0 round, 1 = square
$img->carre = 0;
// Image creation
$img->init_create();


?>
Rate this code     |     Copied : 21x

Comments for Image class resize and rounded mask

No comment
Add a comment Nickname
ZOOM


Javascript opacity
Dynamic image resizing according to location
Javascript Image magnification
Isolate a color
Simple image gallery PHP
Zoom with CSS3
Automatically adjust brightness contrast of an image
Adjust the rate of image colors
Dynamically resizing height of textarea
Move dynamically a DOM objet up