0
votes

i am very new in PHP and i need a help with this function, i want to make it with Width: 300px and height: auto (aspect ratio) and i want it to not crop images only resize here is the function

if ( ! function_exists('create_rect_thumb'))
{

function create_rect_thumb($img,$dest,$ratio=3)
{

    $seg = explode('.',$img);   //explde the source to get the image extension
    $thumbType    = 'jpg';      //generated thumb will be of type jpg
    $thumbPath    = $dest;  //destination path of the thumb -- original image name will be appended
    $thumbQuality = 90;             //quality of the thumbnail (in percent)

    //chech the image type and create image accordingly
    if($seg[2]=='jpg' || $seg[2]=='JPG' || $seg[2]=='jpeg')
    {
        if (!$full = imagecreatefromjpeg($img)) {
            return 'error';
        }
    }
    else if($seg[2]=='png')
    {
        if (!$full = imagecreatefrompng($img)) {
            return 'error';
        }           
    }
    else if($seg[2]=='gif')
    {
        if (!$full = imagecreatefromgif($img)) {
            return 'error';
        }           
    }

    $width  = imagesx($full);
    $height = imagesy($full);

    /*wourk out the thumbnail size*/
    $resizedHeight  = min($width*$ratio/8,$height);
    $resizedWidth   = $width;

    /* work out starting point */
    $thumbx = 0;    // x always starts at zero -- the thumbnail gets the same width as the source image
    $extra_height = $height - $resizedHeight;
    $thumby = floor(($extra_height) / 2);

    /* create the small smaller version, then crop it centrally to create the thumbnail */
    $resized  = imagecreatetruecolor($resizedWidth, $resizedHeight);
    imagecopy($resized, $full,0,0,$thumbx,$thumby,$resizedWidth,$resizedHeight);

    $name = name_from_url($img);

    imagejpeg($resized, $thumbPath.str_replace('_large.jpg', '_thumb.jpg', $name), $thumbQuality);
}

}

right now it is creating image banners with full width as original image and half height of original image

1

1 Answers

0
votes

Use ci Image Manipulation Class

http://www.codeigniter.com/user_guide/libraries/image_lib.html

You can use this code

$this->load->library('image_lib');

$config['image_library'] = 'gd2';

$config['source_image'] = '/path/to/image/mypic.jpg';

$config['create_thumb'] = TRUE;

$config['maintain_ratio'] = TRUE;

$config['width'] = 300;

$config['height'] = 50;

$this->load->library('image_lib', $config);

$this->image_lib->resize();

If you use ci File Uploading Class? then you can get image full path using $this->upload->data('upload_path'); this function where your image stored and put it in $config['source_image']

example: $config['source_image'] = $this->upload->data('upload_path');