Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7170637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:15:09+00:00 2026-05-28T15:15:09+00:00

I’m struggling to get the CodeIgniter Image Manipulation working correctly. Either it’s a bug

  • 0

I’m struggling to get the CodeIgniter Image Manipulation working correctly. Either it’s a bug or I’m just not seeing it. I hope someone can help me with it. Thanks in advance!

On the script: I want to create thumbnails (176w x 132h). The input images are in different sizes and ratios. In order to always get this size I first resize them to fit the max width or height (depending on image orientation) and then crop in the center.
I’ve tried to do it all in 1 method. That didn’t work, so I created two seperate methods.

resize_img() 

and

crop_img().

When I run resize_img() on 3 different files, it works. If after that I use crop_img() on these thumbnails the 1th method created, it works. If I combine the two, or use them after one another, it doesn’t.
I’ve tried $this->image_lib->clear();, unsetting the config files. I even created two config files, just to be sure.

I’m getting different all kind of errors from GD2, but the problem is, that after resize_img() creates the thumbnail, the crop_img() won’t crop it. After that it all goes south, and the next images can’t be opened. Write premissions are checked, both on folder and files.

Unable to save the image. Please make sure the image and file
directory are writable. The path to the image is not correct. Your
server does not support the GD function required to process this type
of image.

Full code:

<?PHP

class Imagetest extends MY_Controller {

function __construct()
{
    parent::__construct();
    $this->load->library('image_lib');
}

function index()
{
    $testimg1 = 'uploads/test/1.png';
    $testimg2 = 'uploads/test/2.png';
    $testimg3 = 'uploads/test/3.png';

    $this->resize_img($testimg1);
    $this->crop_img($testimg1);

    $this->resize_img($testimg2);
    $this->crop_img($testimg2);

    $this->resize_img($testimg3);
    $this->crop_img($testimg3);
}


function image_thumb_name($img = null)
{
    if(!empty($img)) {
        $exploded = explode('.', $img);
        return $exploded['0'] . '_thumb.' . $exploded['1'];
    }
}

function get_axis($img)
{
    // Default values
    $output['x_axis'] = 0;
    $output['y_axis'] = 0;

    // Settings
    $config['height'] = 132;
    $config['width']  = 176;

    if ($img_dim = getimagesize($img)) {
        list($thumb_width, $thumb_height) = $img_dim;
    } else {
        echo '<h1> ERROR HERE TOO</h1>';
        return false;
    }

    if ($thumb_width > $config['width']) {

        $output['x_axis'] = (($thumb_width - $config['width']) / 2) ;               

    } else if ($thumb_height > $config['height']) {

        $output['y_axis'] = (($thumb_height - $config['height']) / 2);
    }       
    return $output;
} 

function resize_img($img) 
{
    $config = array();
    echo 'Processing: '. $img .'<br/>';  // Debug

    if ($img_dim = getimagesize($img)) {
        list($image_width, $image_height) = $img_dim;
    } else {
        echo '<h1> ERROR HERE </h1>';
    }

    // create a thumbnail
    $config['image_library'] = 'GD2';
    $config['source_image'] = $img;
    $config['quality'] = 100;
    $config['height'] = 132;
    $config['width'] = 176;
    $config['create_thumb']  = TRUE;
    $config['maintain_ratio']= TRUE;
    $config['master_dim'] = ($image_width > $image_height) ? 'height' : 'width';
    $this->image_lib->initialize($config);

    if (!$this->image_lib->resize()) { 
        echo $this->image_lib->display_errors();
    }

    echo '<img src="../'.$this->image_thumb_name($img).'" /> <br/>'; // Debug

    $this->image_lib->clear();
    unset($config);
}   




function crop_img($img)
{
    $config2 = array();

    // Crop that thumbnail
    $config2['image_library'] = 'GD2';
    $config2['quality'] = 100;
    $config2['height'] = 132;
    $config2['width'] = 176;
    $config2['source_image']  = $this->image_thumb_name($img);
    $axis = $this->get_axis($config2['source_image']);
    $config2['x_axis'] = $axis['x_axis'];
    $config2['y_axis'] = $axis['y_axis'];
    $config2['maintain_ratio'] = FALSE;
    $config2['create_thumb'] = FALSE;
    $this->image_lib->initialize($config2);

    if (!$this->image_lib->crop()) { 
        echo $this->image_lib->display_errors(); 
    }

    echo '<img src="../'.$config2['source_image'].'" /> <br/>'; // Debug

    $this->image_lib->clear();
    unset($config2);
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:15:10+00:00Added an answer on May 28, 2026 at 3:15 pm

    Got it!
    I’ve set the create_thumb option to FALSE, and used the new_image parameter in the resize_img method. The effect is the same, but the built-in create_tumb function is not being used.
    It’s a bug IMHO, but it’s working now 🙂

    $config['create_thumb'] = FALSE;
    $config['new_image'] = $this->image_thumb_name($img);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.