okay, so I have two separate configs for the same library in the same controller function. The first one runs just fine, but the second does not. How can I make it so codeigniter unsets the first one and uses the second config.
// Retrieve the data from the upload
$data = $this->upload->data();
//Re-size the large image and re-save it
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto';
$config['width'] = 600;
$config['height'] = 516;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
else
{
$this->image_lib->crop();
echo 'Image Resized!';
}
//Create a thumbnail for the image
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/thumbnails/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 120;
$config['height'] = 120;
$this->load->library('image_lib', $config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
else
{
echo 'Image Thumbnail Created!';
}
So it re-sizes the image and then is supposed to thumbnail an image.
You can use the initialize method provided by the library, so you call it instead of loading the library each time:
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto';
$config['width'] = 600;
$config['height'] = 516;
$this->image_lib->initialize($config);
....
$config['image_library'] = 'gd2';
$config['source_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/'.$file_name.''.$data['file_ext'];
$config['new_image'] = $this->config->item('upload_path').''.$this->aauth->get_user_id().'/thumbnails/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 120;
$config['height'] = 120;
$this->image_lib->initialize($config);
Related
it does not show any error, and do nothing, I can not understand whats wrong with it!
public function resize($path,$filename) {
$config['source_image'] = $path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$config['new_image'] = './img/uploads/users/'.$filename . '.jpg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->clear();
if(!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
Problem is you are calling clear method before resize method, it clears all settings try this method, i have tested with same directory structure it works fine
public function resize($path, $filename)
{
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['create_thumb'] = true;
$config['maintain_ratio'] = true;
$config['width'] = 75;
$config['height'] = 50;
$config['new_image'] = './img/uploads/users/' . $filename . '.jpg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
// $this->image_lib->clear();
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
Here is my code from controller
`
$type= explode('.', $_FILES['picture']['name']);
$type = $type[count($type)-1];
$url = "uploads/products/images/".uniqid(rand()).".".$type;
if(in_array($type, array('jpg','jpeg','png','JPG','JPGE','PNG') ) )
{
if(is_uploaded_file($_FILES['picture']['tmp_name']))
{
move_uploaded_file($_FILES['picture']['tmp_name'],$url);
}
}
'
Since you're using CodeIgniter, it has it's own Image Manipulation class:
https://www.codeigniter.com/userguide3/libraries/image_lib.html
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.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();
Set the width and height variables to what you need to resize the image to.
I am not able to save the image in the destination folder, the directory remains empty.
$this->load->library('image_lib');
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$config['image_library'] = 'gd2';
$config['source_image'] = '/arquivos_upload/link/'. $slug .'.jpg';
$config['allowed_types'] = 'jpg|png';
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['width'] = 405;
$config['height'] = 405;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
I have wrote a system for my personal website with CodeIgniter and used GD2 library to manipulate images according to my needs. It worked just fine when on localhost and a free hosting that I used for testing. But now that I am on a paid hosting from a different provider, It doesn't work for the jpg files. It shows the broken-image icon. But works just fine with PNG files. I have tried displaying the manipulation errors but nothing was displayed.
Here is my media controller, which manipulates the images;
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Media extends CI_Controller
{
function sandbox($imageName , $width)
{
if(!is_file('uploads/sandbox/'.$width.'-'.$imageName)) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/original/'.$imageName;
$config['new_image'] = 'uploads/sandbox/'.$width.'-'.$imageName;
$config['dynamic_output'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'width';
$config['width'] = $width;
$config['height'] = $width;
$this->image_lib->initialize($config);
return $this->image_lib->resize();
} else {
return true;
}
}
public function covers($imageName)
{
if($this->sandbox($imageName, 920)) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/sandbox/'.'920-'.$imageName;
$config['dynamic_output'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 920;
$config['height'] = 450;
$this->image_lib->initialize($config);
$this->image_lib->crop();
}
}
public function images($imageName)
{
if($this->sandbox($imageName, 440)) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/sandbox/'.'440-'.$imageName;
$config['dynamic_output'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 440;
$config['height'] = 300;
$this->image_lib->initialize($config);
if(!$this->image_lib->crop()){
echo $this->image_lib->display_errors();
}
}
}
public function facebook($imageName)
{
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/original/'.$imageName;
$config['dynamic_output'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 256;
$config['height'] = 256;
$this->image_lib->initialize($config);
echo $this->image_lib->resize();
}
}
Jpeg must be enabled when you install GD2; http://php.net/manual/en/image.installation.php
I am using following code but unable to resize image even $this->image_lib->resize() is returning true, don't know what is going wrong:
if(file_exists($_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".str_replace('_','-',$image)))
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".str_replace('_','-',$image);
}
else if (!file_exists("./uploads/avatars/".$image) || $image=="")
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/avatars/photo.jpg';
}
else
{
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']."/uploads/avatars/".$image;
}
$config['image_library'] = 'gd2';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['dynamic_output'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Try the new_image tag:
$config['new_image'] = '/path/to/new_image.jpg';
Better to use SimpleImage.php as helper in CodeIgniter
$source=base_url().'/uploads/avatars/photo.jpg';
$image = new SimpleImage();
$image->load($source);
$image->resize(600,400);
$image->save($name_and_location);