Welcome Guest, Not a member yet? Register   Sign In
Crop 125px Square From Center
#1

[eluser]machouinard[/eluser]
I'm trying to crop a 125px square from the center of an image.

Is there a way to provide the dimensions to have the crop done in one method? I've tried cropping, then flipping and rotating and cropping some more, then rotating back to the original position. I can't seem to get the crop area right, not to mention that's a lot of code for something I thought a framework was supposed to make easier. I'm trying to redo a site I built before I even knew about frameworks and would like to use CI, but in this instance it just seems like more work. Is there an easier way to do this in CI? Here is how I am currently getting the cropped area:

Code:
function _crop_Image($source, $cropped) {
    $w = 125;
    $h = 125;
    $ext = strtolower($this->file_info['file_ext']);
    
    list($w_src, $h_src) = getimagesize($source);
    $start_x = ($w_src / 2) - ($w / 2);
    $start_y = ($h_src / 2) - ($h / 2);
    $img = "";
    if ($ext == "gif"){
    $img = imagecreatefromgif($source);
    } else if($ext =="png"){
    $img = imagecreatefrompng($source);
    } else {
    $img = imagecreatefromjpeg($source);
    }
    $tci = imagecreatetruecolor($w, $h);
    imagecopyresampled($tci, $img, 0, 0, $start_x, $start_y, $w, $h, $w, $h);
    
    if ($ext == "gif"){
        imagegif($tci, $cropped);
    } else if($ext =="png"){
        imagepng($tci, $cropped);
    } else {
        imagejpeg($tci, $cropped, 85);
    }
}

(Much help with above code came from Adam)

Anything helpful anyone has to offer would be greatly appreciated.

Thanks,
Mark

EDIT: In case anyone noticed, I had to change
Code:
if ($ext == "gif"){
to
Code:
if ($ext == ".gif"){
. The way I was getting the extension previously did not incorporate the dot.




Theme © iAndrew 2016 - Forum software by © MyBB