Jump to content

image crop script: modify alignment


kristinac

Recommended Posts

i have this awesome script i've used on multiple websites and only now is it giving me trouble. it's always aligned/cropped to the middle of the image but for some reason the smaller thumbnails are cropping to the bottom.

 

<?php


header ("Content-type: image/jpeg");
$file_name=$_GET['f'];
$crop_height=$_GET['h'];
$crop_width=$_GET['w'];
$file_type= explode('.', $file_name);
$file_type = $file_type[count($file_type) -1];
$file_type=strtolower($file_type);

$original_image_size = getimagesize($file_name);
$original_width = $original_image_size[0];
$original_height = $original_image_size[0];

if($file_type=='jpg') {
$original_image_gd = imagecreatefromjpeg($file_name);
}

if($file_type=='gif') { 
$original_image_gd = imagecreatefromgif($file_name);
}

if($file_type=='png') {
$original_image_gd = imagecreatefrompng($file_name);
}

$cropped_image_gd = imagecreatetruecolor($crop_width, $crop_height);
$wm = $original_width /$crop_width;
$hm = $original_height /$crop_height;
$h_height = $crop_height/2;
$w_height = $crop_width/2;

if($original_width > $original_height ) {
$adjusted_width =$original_width / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;

imagecopyresampled($cropped_image_gd ,$original_image_gd ,-$int_width,0,0,0, $adjusted_width, $crop_height, $original_width , $original_height );
}

elseif(($original_width < $original_height ) || ($original_width == $original_height )) {
$adjusted_height = $original_height / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;

imagecopyresampled($cropped_image_gd , $original_image_gd ,0,-$int_height,0,0, $crop_width, $adjusted_height, $original_width , $original_height );
}

else {
imagecopyresampled($cropped_image_gd , $original_image_gd ,0,0,0,0, $crop_width, $crop_height, $original_width , $original_height );
}

imagejpeg($cropped_image_gd); 

?>

 

if i modify the value of original_image_size, it crops to the middle, but if the height is greater than the width it will add black on either side of the image to fill out the designated value.

 

$original_image_size = getimagesize($file_name);
$original_width = $original_image_size[0];
$original_height = $original_image_size[1];

 

i can't figure out how to get it do both.

 

0, 0 outputs this:

http://www.loudtechinc.com/images/scriptissue/00.png

http://www.loudtechinc.com/images/scriptissue/00n2.png

 

0, 1 outputs this:

http://www.loudtechinc.com/images/scriptissue/01.png

http://www.loudtechinc.com/images/scriptissue/01n2.png

 

 

The end goal is to have it fill the desired height and width while cropping to the middle.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.