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
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();
}
}
This is the code which I am trying to run..
I have stored the image in images folder whose path is: C:\xampp\htdocs\ci\images, and is at the same level at that of the application folder
public function display(){
$config['image_library'] = 'gd2';
$config['source_image'] = '/images/mypic.jpg';
$config['new_image']='/images/re_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();
}
I am not getting any error or something
Add this and check your code errors
if ( ! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
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);
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);
What I am basically trying to do is resize a image into two different size images. However I am only being able to resize the first featured_$filename I am not being able to resize thumb_$filename .
Initially I tried creating a single function and passed the configuration as an array and it didnt worked but same thing, only single image was resized.
$this->resizeImage($imagePath, $file['upload_data']['file_name']);
$this->resizeThumb($imagePath, $file['upload_data']['filename']);
public function resizeImage($imagePath, $filename){
$config['image_library'] = 'gd2';
$config['source_image'] = $imagePath;
$config['create_thumb'] = FALSE;
$config['new_image'] = 'featured_'.$filename;
$config['maintain_ratio'] = TRUE;
$config['width'] = 570;
$config['height'] = 372;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
public function resizeThumb($imagePath, $filename){
$config['image_library'] = 'gd2';
$config['source_image'] = $imagePath;
$config['create_thumb'] = FALSE;
$config['new_image'] = 'thumb_'.$filename;
$config['maintain_ratio'] = TRUE;
$config['width'] = 180;
$config['height'] = 135;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
}
You need to load the library only once but initialize the library twice with different configs. E.g. :
$this->load->library('image_lib');
/* size 64*72 for comments */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['create_thumb'] = TRUE;
$configThumb['new_image'] = './profile_images/thumbs/';
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 64;
$configThumb['height'] = 72;
$configThumb['thumb_marker'] = "";
//$this->load->library('image_lib');
/* size 64*72 for comments */
/* size 167*167 for profile page */
$configThumbMedium = array();
$configThumbMedium['image_library'] = 'gd2';
$configThumbMedium['create_thumb'] = TRUE;
$configThumbMedium['new_image'] = './profile_images/medium/';
$configThumbMedium['maintain_ratio'] = TRUE;
$configThumbMedium['width'] = 167;
$configThumbMedium['height'] = 167;
$configThumbMedium['thumb_marker'] = "";
/* size 167*167 for profile page */
if(!$this->upload->do_upload('image')){
return 0;
}
$uploadedDetails = $this->upload->data();
if($uploadedDetails['is_image'] == 1){
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this->image_lib->initialize($configThumbMedium);
$this->image_lib->resize();
}
Hope it helps.
hi load library first and use $this->image_lib->initialize($config) to pass config and also in new_image config pass path with new image name.
$this->load->library('image_lib');
$config['new_image'] = 'image_dir/thumb_'.$filename
//ALL your other configs
$this->image_lib->initialize($config)