CodeIgniter Forums

Full Version: Cropping image....
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]haris244808[/eluser]
can someone help me calculate and crop the center of the image ?:

Code:
function image_crop($sourceHeight, $sourceWidth, $image){

  $target_path = FCPATH.'pictures/cropped/';

  $targetHeight = '214';
  $targetWidth = '171';

  $centreX = round($sourceWidth / 2);
  $centreY = round($sourceHeight / 2);

  $cropWidth = round($targetWidth /2);
  $cropHeight = round($targetHeight / 2);
  
  

  $config = array( 'image_library'   => 'gd2',
       'source_image'   => $image,
       'new_image'    => $target_path,
       'create_thumb'   => TRUE,
       'thumb_marker'   => '',
       'x_axis'    => $x,
       'y_axis'    => $y
      );

  $this->image_lib->initialize($config);

  if($this->image_lib->crop()){

   return true;
  }
  else{

    return false;
  }

}//end of image_crop function

El Forum

[eluser]TheFuzzy0ne[/eluser]
This is untested, but might work.

Code:
<?php
function image_crop($image, $targetWidth = '200', $targetHeight = '200')
{
    $imageInfo = getimagesize($image);

    $config = array(
        'image_library' => 'gd2',
        'width' => $targetWidth,
        'height' => $targetHeight,
        'source_image' => $image,
        'new_image' => FCPATH.'pictures/cropped/'.pathinfo($image, PATHINFO_FILENAME),
        'x_axis' => ($targetWidth < $imageInfo[0])
            ? round(($imageInfo[0] - $targetWidth) / 2)
            : 0,
        'y_axis' => ($targetHeight < $imageInfo[1])
            ? round(($imageInfo[1] - $targetHeight) / 2)
            : 0,
    );

    $this->image_lib->initialize($config);

    if($this->image_lib->crop()) {
        return true;
    } else {
        return false;
    }
}//end of image_crop function

El Forum

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364172400"]This is untested, but might work.

Code:
&lt;?php
function image_crop($image, $targetWidth = '200', $targetHeight = '200')
{
    $imageInfo = getimagesize($image);

    $config = array(
        'image_library' => 'gd2',
        'width' => $targetWidth,
        'height' => $targetHeight,
        'source_image' => $image,
        'new_image' => FCPATH.'pictures/cropped/'.pathinfo($image, PATHINFO_FILENAME),
        'x_axis' => ($targetWidth < $imageInfo[0])
            ? round(($imageInfo[0] - $targetWidth) / 2)
            : 0,
        'y_axis' => ($targetHeight < $imageInfo[1])
            ? round(($imageInfo[1] - $targetHeight) / 2)
            : 0,
    );

    $this->image_lib->initialize($config);

    if($this->image_lib->crop()) {
        return true;
    } else {
        return false;
    }
}//end of image_crop function
[/quote]

ill try this but can u tell me also what getimagesize array returns, because i searched in the net but i couldn't find...i know its a php question but if you can answer i will appreciate it

El Forum

[eluser]TheFuzzy0ne[/eluser]
http://lmgtfy.com/?q=php+getimagesize

El Forum

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364172400"]This is untested, but might work.

Code:
&lt;?php
function image_crop($image, $targetWidth = '200', $targetHeight = '200')
{
    $imageInfo = getimagesize($image);

    $config = array(
        'image_library' => 'gd2',
        'width' => $targetWidth,
        'height' => $targetHeight,
        'source_image' => $image,
        'new_image' => FCPATH.'pictures/cropped/'.pathinfo($image, PATHINFO_FILENAME),
        'x_axis' => ($targetWidth < $imageInfo[0])
            ? round(($imageInfo[0] - $targetWidth) / 2)
            : 0,
        'y_axis' => ($targetHeight < $imageInfo[1])
            ? round(($imageInfo[1] - $targetHeight) / 2)
            : 0,
    );

    $this->image_lib->initialize($config);

    if($this->image_lib->crop()) {
        return true;
    } else {
        return false;
    }
}//end of image_crop function
[/quote]

cropping works fine...however i should resize first and then cropp...
how can i combine this two methods to resize and crop on the fly....
Code:
function image_resize($image){

  $target_path = FCPATH.'uploads/profile_pictures/171x214/';

  $config = array( 'image_library'   => 'gd2',
       'source_image'   => $image,
       'new_image'    => $target_path,
       'maintain_ratio'  => TRUE,
       'create_thumb'   => TRUE,
       'thumb_marker'   => '',
       'width'     => 171,
       'height'    => 214
      );

  $this->image_lib->initialize($config);

  if($this->image_lib->resize()){

   return true;
  }
  else{
    return false;
  }
}//end of image_resize function

Code:
function image_crop($image){

  $imageInfo = getimagesize($image);
  $target_path = FCPATH.'uploads/profile_pictures/135x135_crop/';

  $targetHeight = 135;
  $targetWidth = 135;
  //$imageInfo[0] = image width
  $x = ($targetWidth < $imageInfo[0]) ? round(($imageInfo[0] - $targetWidth) / 2) : 0;
  //$imageInfo[1] = image height
  $y =  ($targetHeight < $imageInfo[1]) ? round(($imageInfo[1] - $targetHeight) / 2) : 0;  
   $c 'image_library'   => 'gd2',
       'source_image'   => $image,
       'new_image'    => $target_path,
       'create_thumb'   => TRUE,
       'thumb_marker'   => '',
       'x_axis'    => $x,
       'y_axis'    => $y
      );

  $this->image_lib->initialize($config);

  if($this->image_lib->crop()){

   return true;
  }
  else{

    return false;
  }

El Forum

[eluser]TheFuzzy0ne[/eluser]
Why do you need to resize and then crop? Normally we only need to do one or the other.

El Forum

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364224665"]Why do you need to resize and then crop? Normally we only need to do one or the other.[/quote]

because when it crops large images...it crops only the center of it without resize..
ex...if i crop a large profile picture, it crops only the nose ...

El Forum

[eluser]TheFuzzy0ne[/eluser]
I see. So you want to resize the image, and then crop it to previously determined dimensions? That makes sense.

I don't see why you're having a problem. Calling both of those methods should work how you want it to. You just need to make sure you're cropping the image in-place. It might make more sense to do it all in a temporary directory and then move the end product to the final directory.

El Forum

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364225087"]I see. So you want to resize the image, and then crop it to previously determined dimensions? That makes sense.

I don't see why you're having a problem. Calling both of those methods should work how you want it to. You just need to make sure you're cropping the image in-place. It might make more sense to do it all in a temporary directory and then move the end product to the final directory.[/quote]

actually i want to resize the picture using resize_image() function and store to a directory....and i do that...it works fine but than i wan to do a crop of that image and store the cropped image to a different directory
here is the controller i use to resize :
Code:
function validate_edit_profile(){

  $user_id = $this->uri->segment(3) ? $this->uri->segment(3) : 0;

$picture = 'profile_pic';

  $user = $this->users_model->select_user_by_id($user_id);
  
  $pic_name = $user->profile_pic;

if($_FILES[$picture]['name'] && $_FILES[$picture]['size'] > 0 ){

    $picture_path = FCPATH.'uploads/profile_pictures/original/'.$pic_name;
    $thumb_path = FCPATH.'uploads/profile_pictures/171x214/'.$pic_name;

    unlink($picture_path);
    unlink($thumb_path);

    $config['upload_path'] = FCPATH.'uploads/profile_pictures/original/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    //when you change this, the upload_max_filesize in php.ini file must be changed too
    $config['max_size'] = '2000';
    $config['remove_spaces'] = TRUE;
    
    $this->load->library('upload', $config);
    if($this->upload->do_upload($picture)){ //if upload of picture is successfull

     $data = $this->upload->data(); //get all the uploads file data(name, path...)

     $image = $data['full_path'];

     $this->load->model('images_model'); //load images_model

     if ($this->images_model->image_resize($image)  == TRUE) {
      
      $profile_pic_name = $data['file_name']; //get the name of the picture

     }
    }
   }
}

El Forum

[eluser]TheFuzzy0ne[/eluser]
Why can't you figure that out yourself? People have written examples for you, but you need to understand what's happening. We can't keep writing your code for you.
Pages: 1 2