Revision: 34517
Updated Code
at October 24, 2010 20:56 by sybrex
Updated Code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_image
{
private $ci;
private $upload_path;
private $copies;
private $allowed_types;
private $max_size;
private $input;
function __construct($props = array())
{
$this->ci =& get_instance();
$this->ci->load->library('upload');
$this->ci->load->library('image_lib');
if (count($props) > 0)
{
$this->initialize($props);
}
}
function initialize($config = array())
{
$defaults = array('copies' => false,
'allowed_types' => 'jpg|jpeg|gif|png',
'max_size' => '2000',
'input' => 'picture',
'copies' => array(),
'upload_path' => ''
);
foreach ($defaults as $key => $val)
{
$this->$key = isset($config[$key]) ? $config[$key] : $val;
}
}
function upload()
{
$config['upload_path'] = $this->upload_path;
$config['allowed_types'] = $this->allowed_types;
$config['max_size'] = $this->max_size;
$this->ci->upload->initialize($config);
if ($this->ci->upload->do_upload($this->input))
{
$upload = $this->ci->upload->data();
// Set image name
while (true)
{
$img_name = uniqid().$upload['file_ext'];
if (!file_exists($upload['file_path'].$img_name)) break;
}
rename($upload['full_path'], $upload['file_path'].$img_name);
// Creating image copies
if (count($this->copies) > 0)
{
foreach ($this->copies as $copy)
{
$resize_config = array('file_name' => $img_name,
'source_path' => $upload['file_path'],
'copy' => $copy);
$this->resize($resize_config);
}
}
return $img_name;
}
return false;
}
function resize($init)
{
$config = array('source_image' => $init['source_path'].$init['file_name'],
'new_image' => $init['copy']['upload_path'].$init['file_name']);
list($img['image_width'], $img['image_height'], $img['type'], $img['attr']) = getimagesize($config['source_image']);
if ($init['copy']['crop'])
{
// Resize to make smaller edge = selected size
if ($img['image_width'] > $img['image_height'])
{
$config['height'] = $init['copy']['height'];
$ratio = $init['copy']['height'] / $img['image_height'];
$config['width'] = $img['image_width'] * $ratio;
}
else
{
$config['width'] = $init['copy']['width'];
$ratio = $init['copy']['width'] / $img['image_width'];
$config['height'] = $img['image_height'] * $ratio;
}
$this->ci->image_lib->initialize($config);
$this->ci->image_lib->resize();
$config['width'] = $init['copy']['width'];
$config['height'] = $init['copy']['height'];
$config['maintain_ratio'] = false;
$config['source_image'] = $config['new_image'];
unset($config['new_image']);
$this->ci->image_lib->initialize($config);
$this->ci->image_lib->crop();
}
else
{
// Width adjustment
if ($img['image_width'] > $init['copy']['width'])
{
$config['width'] = $init['copy']['width'];
$ratio = $init['copy']['width'] / $img['image_width'];
$config['height'] = $img['image_height'] * $ratio;
}
// Height adjustment
if ($config['height'] > $init['copy']['height'])
{
$config['height'] = $init['copy']['height'];
$ratio = $init['copy']['height'] / $img['image_height'];
$config['width'] = $img['image_width'] * $ratio;
}
$this->ci->image_lib->initialize($config);
$this->ci->image_lib->resize();
}
}
}
// End of file
// Using
$this->load->library('upload_image');
$upload_path = './uploads/property/';
$copies[] = array('upload_path' => $upload_path.'mid/', 'width' => 90, 'height' => 75, 'crop' => false);
$copies[] = array('upload_path' => $upload_path.'small/', 'width' => 50, 'height' => 50, 'crop' => true);
$config = array('upload_path' => $upload_path.'original/', 'input' => 'picture', 'copies' => $copies);
$this->upload_image->initialize($config);
$result = $this->upload_image->upload();
echo $result ? 'Image '.$result.' was uploaded successfully' : 'Image was not uploaded';
Revision: 34516
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 23, 2010 16:55 by sybrex
Initial Code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_image
{
private $ci;
private $upload_path;
private $copies;
private $allowed_types;
private $max_size;
private $input;
function __construct($props = array())
{
$this->ci =& get_instance();
$this->ci->load->library('upload');
$this->ci->load->library('image_lib');
if (count($props) > 0)
{
$this->initialize($props);
}
}
function initialize($config = array())
{
$defaults = array('copies' => false,
'allowed_types' => 'jpg|jpeg|gif|png',
'max_size' => '2000',
'input' => 'picture',
'copies' => array(),
'upload_path' => ''
);
foreach ($defaults as $key => $val)
{
$this->$key = isset($config[$key]) ? $config[$key] : $val;
}
}
function upload()
{
$config['upload_path'] = $this->upload_path;
$config['allowed_types'] = $this->allowed_types;
$config['max_size'] = $this->max_size;
$this->ci->upload->initialize($config);
if ($this->ci->upload->do_upload($this->input))
{
$pic = $this->ci->upload->data();
// Set image name
$img_name = time().$pic['file_ext'];
rename($pic['full_path'], $pic['file_path'].$img_name);
// Creating image copies
if (count($this->copies) > 0)
{
foreach ($this->copies as $copy)
{
list($img['image_width'], $img['image_height'], $img['type'], $img['attr']) = getimagesize($pic['file_path'].$img_name);
$img['full_path'] = $copy['upload_path'].$img_name;
copy($pic['file_path'].$img_name, $img['full_path']);
$this->resize($img, $copy['width'], $copy['height'], $copy['crop']);
}
}
return $img_name;
}
return false;
}
function resize($img, $width, $height, $square_crop = false)
{
$config = array('source_image' => $img['full_path'],
'width' => $img['image_width'],
'height' => $img['image_height']);
if ($square_crop)
{
// Resize to make smaller edge = selected size
if ($img['image_width'] > $img['image_height'])
{
$config['height'] = $height;
$ratio = $height / $img['image_height'];
$config['width'] = $img['image_width'] * $ratio;
}
else
{
$config['width'] = $width;
$ratio = $width / $img['image_width'];
$config['height'] = $img['image_height'] * $ratio;
}
$this->ci->image_lib->initialize($config);
$this->ci->image_lib->resize();
$config['width'] = $width;
$config['height'] = $height;
$config['maintain_ratio'] = false;
$this->ci->image_lib->initialize($config);
$this->ci->image_lib->crop();
}
else
{
// Width adjustment
if ($img['image_width'] > $width)
{
$config['width'] = $width;
$ratio = $width / $img['image_width'];
$config['height'] = $img['image_height'] * $ratio;
}
// Height adjustment
if ($config['height'] > $height)
{
$config['height'] = $height;
$ratio = $height / $img['image_height'];
$config['width'] = $img['image_width'] * $ratio;
}
$this->ci->image_lib->initialize($config);
$this->ci->image_lib->resize();
}
}
}
// End of file
// Using
$this->load->library('upload_image');
$upload_path = './uploads/property/';
$copies[] = array('upload_path' => $upload_path.'mid/', 'width' => 90, 'height' => 75, 'crop' => false);
$copies[] = array('upload_path' => $upload_path.'small/', 'width' => 50, 'height' => 50, 'crop' => true);
$config = array('upload_path' => $upload_path.'original/', 'input' => 'picture', 'copies' => $copies);
$this->upload_image->initialize($config);
$result = $this->upload_image->upload();
echo $result ? 'Image '.$result.' was uploaded successfully' : 'Image was not uploaded';
Initial URL
Initial Description
Initial Title
CI Upload_image library
Initial Tags
codeigniter
Initial Language
PHP