I want to create a new photo using dynamic width only so that the Height then changes depending on the width automatically.
The following code is not working with me.
$w = $width;
$w = $w / 4 ;
$mconfig['width'] = $w;
$mconfig['maintain_ratio'] = TRUE;
$mconfig['source_image'] = 'uploads/watermark.png';
$mconfig['new_image'] = 'uploads/images/watermark/watermark.png';
$this->image_lib->initialize($mconfig);
$this->image_lib->resize();
$this->image_lib->clear();
Try to set the Height also
for example
$config['height'] = 50;
Try this:
//Imagine you need Width 1000px
$height = ($fdata["image_height"] * 1000) / $fdata['image_width'];
$resize_height = (int)$height;
$mconfig['width'] = 1000;
$mconfig['height'] = $resize_height;
$mconfig['maintain_ratio'] = TRUE;
$mconfig['source_image'] = 'uploads/watermark.png';
$mconfig['new_image'] = 'uploads/images/watermark/watermark.png';
$this->image_lib->initialize($mconfig);
$this->image_lib->resize();
$this->image_lib->clear();
Here $fdata["image_height"] is uploaded image height and $fdata['image_width'] is uploaded image width.
Try this,
$uploaded_data = $this->upload->data();
$w = $width / 4;
$config['width'] = $w;
$config['height'] = round($uploaded_data['image_height']/($uploaded_data['image_width']/$w), 2);
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/watermark.png';
$config['new_image'] = 'uploads/images/watermark/watermark.png';
$config['quality'] = "100%";
$config['maintain_ratio'] = true;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
if ($this->image_lib->resize()){
$this->image_lib->clear();
return true;
}else{
return $this->image_lib->display_errors();
}
I need to resize image to fit 64x64 thumbnail. What I'm doing? I set resize width and height to 64px. If I set $config['maintain_ratio'] = false, image lose proportions. What I did: I set $config['maintain_ratio'] = TRUE; and $config['master_dim'] = 'width'; Now width is always 64px, but height is larger. I have somehow to crop image height to 64px from bottom. How can I achieve this? I tried to set up crop function, but I failed. Maybe there is another solution...
My resize function
private function resize($up_data) {
$config['image_library'] = 'gd2';
$config['source_image'] = $up_data['full_path'];
$config['new_image'] = './images/autoriaithumb/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['thumb_marker'] = '_thumb';
$config['width'] = 64;
$config['height'] = 64;
$config['quality'] = 100;
$config['master_dim'] = 'width';
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
try this
define 'upload config' as $image_config and define your crop config....as $config
$this->load->library('upload', $image_config);
if ($this->upload->do_upload()) {
$this->load->library('image_lib',$config);
$this->image_lib->resize();
}
I have problem with image class in CI?
This is example
On controller:
$this->load->library( array('image_lib') );
On view i have this:
foreach($results as $row)
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
But i got no error, and thumb image is not created?
I dont know where is mistake?
You shouldn't load image_lib in foreach. Try to use code below
$this->load->library('image_lib');
foreach($results as $row) {
$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
If it wont work - check the upload folder permissions.
function uploadimage()
{
$file_name = $this->input->post('fileName');
$imageName = 'set_'.file_name($_FILES["file"]['name'],date('Ymd_his'));
$config['image_library'] = 'gd2';
$config['source_image'] = $imageName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'width';
upload_resize('file','settings', $imageName );
$config['width'] = 200;
$config['height'] = 62;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
$json_data = array(
'error' =>false,
'tmp_name' =>$file_name,
'file_name' =>base_url().'assets/uploads/settings/'.$imageName.'?dummy='.rand(0,1000),
'file_path' =>$imageName
);
echo json_encode($json_data);
}
}
this code upload image to folder before save to table.
$config['upload_path'] = './berkas/content/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 0;
$config['max_width'] = 1024000;
$config['max_height'] = 7680000;
$config['encrypt_name'] = TRUE;
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$params['gambar'] = $file_name;
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = './berkas/content/'.$file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 550;
$config['height'] = 350;
$params = array('gambar' => $file_name);
If the destination image contains a different aspect ratio than the source image, then the source image will be cropped in the same ratio of the destination image have.
public function resize_image($image_data){
$this->load->library('image_lib');
$w = $image_data['image_width']; // original image's width
$h = $image_data['image_height']; // original images's height
$n_w = 273; // destination image's width
$n_h = 246; // destination image's height
$source_ratio = $w / $h;
$new_ratio = $n_w / $n_h;
if($source_ratio != $new_ratio){
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/uploaded_image.jpg';
$config['maintain_ratio'] = FALSE;
if($new_ratio > $source_ratio || (($new_ratio == 1) && ($source_ratio < 1))){
$config['width'] = $w;
$config['height'] = round($w/$new_ratio);
$config['y_axis'] = round(($h - $config['height'])/2);
$config['x_axis'] = 0;
} else {
$config['width'] = round($h * $new_ratio);
$config['height'] = $h;
$size_config['x_axis'] = round(($w - $config['width'])/2);
$size_config['y_axis'] = 0;
}
$this->image_lib->initialize($config);
$this->image_lib->crop();
$this->image_lib->clear();
}
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/uploaded_image.jpg';
$config['new_image'] = './uploads/new/resized_image.jpg';
$config['maintain_ratio'] = TRUE;
$config['width'] = $n_w;
$config['height'] = $n_h;
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
} else {
echo "done";
}}
I have prepared a step by step guide on how to resize image in codeigniter without loosing quality
I have tried below code. Please check.
if (isset($_FILES['image']) && !empty($_FILES['image']['name'])) {
$upload_path="uploads/foldername/";
$newFileName = explode(".",$_FILES['image']['name']);
$filename = time()."-".rand(00,99).".".end($newFileName);
$filename_new = time()."-".rand(00,99)."_new.".end($filename);
$config['file_name'] = $filename;
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image'))
{
//Image Resizing
$config1['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config1['new_image'] = 'uploads/foldername/'.$filename_new;
$config1['maintain_ratio'] = FALSE;
$config1['width'] = 181;
$config1['height'] = 181;
$this->load->library('image_lib', $config1);
if ( ! $this->image_lib->resize()){
$this->session->set_flashdata('message', $this->image_lib->display_errors('', ''));
}
$post_data['image'] = $filename;
}
else
{
$this->session->set_flashdata('msg',$this->upload->display_errors());
}
}
The problem is that you are loading the library inside the loop, that is the error, if you edit 10 images you will load the library 10 times, big mistake, the library must be loaded in the constructor or out of the loop.
Wrong
foreach($results as $row)
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
Right
$this->load->library('image_lib');
foreach($results as $row)
{
$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
}
May this function will help someone
function compressimg($gbr,$path){
$wd =$gbr['image_width']*0.4; //0.4 mean compress to 40% image_size
$hh =$gbr['image_height']*0.4; //modify yours
//Compress Image
$config['image_library'] = 'gd2';
$config['source_image'] = $path.$gbr['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['quality'] = '50%';
$config['width'] = round($wd);
$config['height'] = round($hh);
$config['new_image'] = $path.$gbr['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
return $gbr['full_path'];
// Put This On Uploader , by user4xn
// return $this->compressimg($this->upload->data(),$config['upload_path']);
}
I have tried to crop image by using the library function of codeigniter. But $this->image_lib->crop() function couldn't change the image.
Here is my code-
<?php
class Cropimg extends CI_Controller {
function index()
{
$config['image_library'] = 'gd2';
$config['source_image'] = 'C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg';
$config['x_axis'] = '300';
$config['y_axis'] = '200';
$config['maintain_ratio'] = FALSE;
$config['new_image'] = 'C:\Users\Public\Pictures\Sample Pictures\new_crop_img.jpg';
// $config['width'] = $width-10;
//$config['height'] = $height-10;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->crop())
echo $this->image_lib->display_errors();
else
echo "<strong>Your image has been cropped successfully..!!</strong>";
}
}
?>
Please try this code its working fine. Do you get any errors while cropping?
$config['image_library'] = 'GD2';
$config['source_image'] = 'C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg';
$config['new_image'] = 'C:\Users\Public\Pictures\Sample Pictures\new_crop_img.jpg';
$config['height'] = '200';
$config['width'] = '300';
$config['maintain_ratio'] = FALSE;
$this->image_lib->initialize($config);
if ( ! $this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}else
{
echo "<strong>Your image has been cropped successfully..!!</strong>";
}
Provide height as well as width for this.
Height & width is necessary to tell that from a point(X-axis, Y-axis) what will be length of image towards right side & what will be length (as a height) towards downside. These will be considered as height & width of that image.
So un-comment
$config['height'] = '200';
$config['width'] = '300';
I am trying to create a thumbnail of a watermarked image, but can't get it to work. The script always seems to resize the original watermarked image without making the thumbnail.. anyway to accomplish both?
Here is my code
$config = array();
$config['source_image'] = $data['json']->{'file_path'};
$config['new_image'] = 'copy_' . $data['json']->{'file_path'};
$config['image_library'] = 'gd2';
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = getcwd() . '/design/new_transparency.png';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'center';
$config['wm_vrt_offset'] = 20;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 125;
$config['height'] = 125;
$this->image_lib->initialize($config);
$this->image_lib->watermark();
$this->image_lib->clear();
$this->image_lib->resize();
is there something I am doing wrong?
In the image manipulation class (user guide):
http://codeigniter.com/user_guide/libraries/image_lib.html
I need to make several sizes of thumbnails in my application, so instead of using the built in thumbnail functionality I just use:
$this->image_lib->clear();
And then use the resize command.