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

  • SEARCH
  • Home
  • 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 8910795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:46:47+00:00 2026-06-15T03:46:47+00:00

I have a image cropper on my project in CodeIgniter, which crops the images

  • 0

I have a image cropper on my project in CodeIgniter, which crops the images like picresize.com does (I’m using jCrop). It works great with vanilla code given below:

<?php
$save_to = $this->config->item('images_gallery_thumb_folder').$data['photo_image'];

$targ_w = $this->config->item('gallery_thumb_width');
$targ_h = $this->config->item('gallery_thumb_height');
$src = $this->config->item('images_gallery_folder').$data['photo_image'];

$types = array(1 => 'gif', 'jpeg', 'png');
list($width,$height,$type) = getimagesize($src);

switch ($types[$type]) {
    case 'jpeg':
        $img_r = imagecreatefromjpeg($src);
        break;
    case 'gif':
        $img_r = imagecreatefromgif($src);
        break;

    case 'png':
        $img_r = imagecreatefrompng($src);
        break;

    default:
        $img_r = imagecreatefromjpeg($src);
        break;
}

$dst_r = ImageCreateTrueColor($targ_w,$targ_h );

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);


switch ($types[$type]) {
    case 'jpeg':
        imagejpeg($dst_r, $save_to, 90); //90 = jpeg quality
        break;
    case 'gif':
        imagegif($dst_r, $save_to);
        break;
    case 'png':
        imagepng($dst_r, $save_to);
        break;
    default:
        imagejpeg($dst_r, $save_to, 90); //90 = jpeg quality
        break;
}


imagedestroy($dst_r);
?>

But I wanna do this the CodeIgniter way.

This is what I came up with so far:

<?php
$img_config = array(
    'source_image'      => $src,
    'new_image'         => $save_to,
    'maintain_ratio'    => false,
    'width'             => $targ_w,
    'height'            => $targ_h,
    'x_axis'            => $_POST['x'],
    'y_axis'            => $_POST['y']
);

$this->load->library('image_lib',$img_config);
//$this->image_lib->resize();
$this->image_lib->crop();
?>

The thing is, it crops from positions, but it does not resize (think like I’ve set a bigger crop square). It only crops from given position.

I’m also using image_moo library on the project, but I couldn’t also succeed with it.

Edit:
In Image_moo, here is the code what I’ve come up with so far:

$this->image_moo
                ->load($src)
                ->crop($_POST['x'],$_POST['y'],($_POST['x']+$_POST['w']),($_POST['y']+$_POST['h']))
                ->resize($targ_w,$targ_h)
                ->save($save_to,true);

The thing is that, it when I use resize parameter, it ignores the crop line altogether and resizes the whole image. If I resize before and call crop later, it just fails.
I can overcome it with using two image_moo calling, which I would not prefer.

This does not work either:

$this->image_moo
                ->load($src)
                ->crop($_POST['x'],$_POST['y'],($_POST['x']+$_POST['w']),($_POST['y']+$_POST['h']))
                //->resize($targ_w,$targ_h)
                ->save($save_to,true)
                ->resize($targ_w,$targ_h)
                ->save($save_to,true);

E.g: this way it works:

$this->image_moo
                ->load($src)
                ->crop($_POST['x'],$_POST['y'],($_POST['x']+$_POST['w']),($_POST['y']+$_POST['h']))
                //->resize($targ_w,$targ_h)
                ->save($save_to,true);
            $this->image_moo
                ->load($save_to)
                ->resize($targ_w,$targ_h)
                ->save($save_to,true);

So how do I resize+crop with given x/y offsets with CodeIgniter (or image_moo) way with one calling of image_moo or CI image_lib?

You should be probably asking why I’m concerned calling it twice. Well, the PQ is important, and I’m concerned because calling it twice will reduce the image quality.

Thanks in advance,

  • 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-06-15T03:46:48+00:00Added an answer on June 15, 2026 at 3:46 am

    Your given Codeigniter code is OK except for one piece of logic: You’re operating on and outputting to the same image file twice, so your output file gets overwritten by the last change to the original file.

    I believe this is a limitation of CI’s Image_Lib class, as each operation is done separately – there is no way to “resize and crop” in one go.

    You have to reinitialize the Image_Lib class between each action to make sure the next action gets called on the output file of the last operation.

    $img_config = array(
        'source_image'      => $src,
        'new_image'         => $save_to,
        'maintain_ratio'    => false,
        'width'             => $targ_w,
        'height'            => $targ_h,
        'x_axis'            => $_POST['x'],
        'y_axis'            => $_POST['y']
    );
    
    $this->load->library('image_lib', $img_config);
    $this->image_lib->resize();
    
    // Now change the input file to the one that just got resized
    // See also $this->image_lib->clear()
    $img_config['source_image'] = $save_to;
    $this->image_lib->initialize($img_config); 
    
    $this->image_lib->crop();
    

    You could also use two different config arrays:

    $this->load->library('image_lib');
    
    $this->image_lib->initialize(array(
        'source_image'      => $src,
        'new_image'         => $save_to,
        'maintain_ratio'    => false,
        'width'             => $targ_w,
        'height'            => $targ_h,
    ));
    $this->image_lib->resize();
    
    $this->image_lib->clear();
    
    $this->image_lib->initialize(array(
        'source_image'      => $save_to,
        'x_axis'            => $_POST['x'],
        'y_axis'            => $_POST['y']
    ));
    $this->image_lib->crop();
    

    Alternatively, you could create the copy of the image file first and then operate on that in each call to the image lib class, saving you the hassle of reinitializing with a new source_image:

    copy($src, $save_to);
    $this->load->library('image_lib', array(
        'source_image'      => $save_to,
        'maintain_ratio'    => false,
        'width'             => $targ_w,
        'height'            => $targ_h,
        'x_axis'            => $_POST['x'],
        'y_axis'            => $_POST['y']
    ));
    $this->image_lib->resize();
    $this->image_lib->crop();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the YUI Image cropper to, well crop images. I have the
i'm using jCrop and CodeIgniter, trying to make an image uploader. So i have
I have a image cropper that first crops the image and then saves it
I have an image uploader and cropper which creates thumbnails and I occasionally get
I have to import a large amount of image crops off of many images
I have my main project which gets images from certain sources (returns Uri). The
I have a photo attachment which I'm saving using Paperclip. However, I'd like to
I'm using jcrop and have written a dynamic way of saving the cropped image
I am using this github image cropper: https://github.com/iosdeveloper/ImageCropper In this image cropper, in the
I have image Array with two images out of that first image its showing

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.