Welcome Guest, Not a member yet? Register   Sign In
Image library
#1

[eluser]Ener1[/eluser]
I have made this image library to post process and uploaded image, setting for it a new
name, and the size I want for it ( max with/ max height )

Code:
<?php
/**
* images class
*
* @package default
* @author fabian.mesaglio
*
* @description: this library helps you to work with images
* adjusting size and quality, adding a prefix if needed to
* setup thumbnails
**/

class Images
{
    private $image;
    private $width;
    private $height;
    private $path;
    private $complete_name;
    private $complete_name_pre;    
    private $prefix;
    private $nn;
    
    function __construct()
    {
        
    }    
    
    /**
     * process_image function
     *
     * @return void
     * @author fabian.mesaglio
         * @param $name //full image name
         * @param $path //the path where the image will be saved
         * @param $h //image conversion height
         * @param $w //image conversion width
         * @param $name //name for the new image
         * @param $exten //extension of the  image
        
     **/
    
    function process_image($name,$path, $h, $w, $t,$nn,$exten)
    {
        $this->height=$h;
        $this->width=$w;
        $this->imagen=$name;
        $this->path=$path;
        $this->nn=$nn;
        $this->complete_name=$this->path.$this->imagen;
        
        if($this->nn!=''){
            $new_image = $this->path.$this->nn;
            $new_image_pre = $this->path.$t.$this->nn;
        }else{
            $new_image = $this->path.$this->imagen;
            $new_image_pre = $this->path.$t.$this->imagen;
            
        }
        $this->complete_name_pre=$this->path.$t.$this->imagen;
    
        $size=GetImageSize($this->complete_name);
        $width_ratio  = ($size[0] / $this->width);
        $height_ratio = ($size[1] / $this->height);
        if($width_ratio >=$height_ratio) {
           $ratio = $width_ratio;
        }else{
           $ratio = $height_ratio;
        }
            
        
        $new_width    = ($size[0] / $ratio);
        if ($new_width>$size[0])
        {
            $new_width=$size[0];
        }
        $new_height   = ($size[1] / $ratio);
        if ($new_height>$size[1])
        {
            $new_height=$size[1];
        }
        $exten = strtolower($exten);
        if($exten=='.jpg'){
            $src_img = imagecreatefromjpeg($this->complete_name);
            $thumba = ImageCreateTrueColor($new_width,$new_height);
            ImageCopyResampled($thumba, $src_img, 0,0,0,0,($new_width+1),($new_height+1),$size[0],$size[1]);
            if($t!=''){
                imagejpeg($thumba,$new_image_pre);
            }else{
                imagejpeg($thumba,$new_image);
            }
            chmod ($this->complete_name,0644);
            ImageDestroy($thumba);
        }else if($exten=='.gif'){
            $src_img = imagecreatefromgif($this->complete_name);
            $thumba = ImageCreateTrueColor($new_width,$new_height);
            ImageCopyResampled($thumba, $src_img, 0,0,0,0,($new_width+1),($new_height+1),$size[0],$size[1]);
            if($t!=''){
                imagegif($thumba,$new_image_pre);
            }else{
                imagegif($thumba,$new_image);
            }
            chmod ($this->complete_name,0644);
        ImageDestroy($thumba);
        }else if($exten=='.png'){
            $src_img = imagecreatefrompng($this->complete_name);
            $thumba = ImageCreateTrueColor($new_width,$new_height);
            ImageCopyResampled($thumba, $src_img, 0,0,0,0,($new_width+1),($new_height+1),$size[0],$size[1]);
                if($t!=''){
                imagepng($thumba,$new_image_pre);
            }else{
                imagepng($thumba,$new_image);
            }
            chmod ($this->complete_name,0644);
            ImageDestroy($thumba);
        }
        
    }
    function delete_original(){
        unlink($this->path.$this->imagen);
    }
    
}
// END class
  

?>

An example of how to use it, lets say in a multiupload function:


Code:
foreach($_FILES['userfile']['name'] as $imagenn){
    if($imagenn!=''){
    $imagenn =         str_replace(' ', '_',$imagenn);
        $cont    +=     1;
        $now    =        strtotime("now");
        $exten    =        strstr($imagenn, ".");
        $nomm     =         str_replace($exten, '',$imagenn);
        $nomm     =         str_replace(' ', '_',$imagenn);
        $name   =        md5($nomm.'_'.$now.$cont).$exten;
        $imagen    =         new imagenes();
        $imagen->process_image($imagenn,'./images/',480,640,'',$name,$exten);//normal image
        $imagen->process_image($imagenn,'./images/',60,60,'t_',$name,$exten);//t_ prefix added to the thumbnail name
        $imagen->delete_original();
        $imge     =         'images/'.$nombre;
        $thum     =         'images/t_'.$nombre;
          }
}




I hope you like it, polish it and tell me what you think about it

Cheers
Fabian
#2

[eluser]danmontgomery[/eluser]
What does this offer that the codeigniter image manipulation library doesn't?
#3

[eluser]Ener1[/eluser]
In my case integrate with a multiupload plugin I had to use for a client site, for some reason the image manipulation library brokes with it, so I made this one to get this working on an ajax multiupload function like google attachment.

Plus I like the simple config

$imagen->process_image($imagenn,'./images/',480,640,'',$name,$exten);

That´s all you need. I hope this answer your question. and thanks for commenting
#4

[eluser]daparky[/eluser]
Can this library accept any image dimension and resize/crop to exact dimension thrown at it with out the image looking distorted etc?
#5

[eluser]Ener1[/eluser]
This library is mainly for resize, it has no crop methods,
but the resize works like a charm

cheers




Theme © iAndrew 2016 - Forum software by © MyBB