Forum Moderators: coopster

Message Too Old, No Replies

Generated thumbnails have black fills

         

archker

12:35 pm on Jul 6, 2010 (gmt 0)

10+ Year Member



Hello,

I've managed to get this script work just fine for what I need it for, however there seems to be an issue with the cropping of images for thumbnails. I cant make it work any other way than this.. but there must be a way?

Can you guys help me out so that the script dynamically changes the size of the thumbnail instead of forcing it into a certain size and fills the gap with black?


$upload_image_limit = 5; // How many images you want to upload at once?
$upload_dir= "images/"; // default script location, use relative or absolute path
$enable_thumbnails= 1 ; // set 0 to disable thumbnail creation

# THUMBNAIL

function make_thumbnails($updir, $img){

$thumbnail_width= 315;
$thumbnail_height= 315;
$thumb_preword= "";
$image_pastword= "stor";

$arr_image_details= GetImageSize("$updir"."$img");
$original_width= $arr_image_details[0];
$original_height= $arr_image_details[1];

if( $original_width > $original_height ){
$new_width= $thumbnail_width;
$new_height= intval($original_height*$new_width/$original_width);
} else {
$new_height= $thumbnail_height;
$new_width= intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);



if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF"; }
if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG"; }
if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG"; }


if( $imgt ) {
$old_image= $imgcreatefrom("$updir"."$img");
$new_image= imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresized($new_image,$old_image,$dest_x, $dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
$imgt($new_image,"$updir"."$thumb_preword"."$img");

$skit2 = substr($img, strrpos($img, '.') + 1);
$info = pathinfo($img);
$skit = basename($img,'.'.$info['extension']);

$imgt($old_image,"$updir"."$skit"."$image_pastword."."$skit2");


}

}

# UPLOAD IMAGE(S)

foreach($_FILES as $k => $v){

$img_type = "";

if( !$_FILES[$k]['error'] && preg_match("#^image/#i", $_FILES[$k]['type'])){

$img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" : $img_type ;
$img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" : $img_type ;
$img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" : $img_type ;

$img_rname = $_FILES[$k]['name'];
$img_path = $upload_dir.$img_rname;

copy( $_FILES[$k]['tmp_name'], $img_path );
if($enable_thumbnails) make_thumbnails($upload_dir, $img_rname);
$feedback .= "Image and thumbnail created $img_rname<br />";

}
}


# THE FORM
while($i++ < $upload_image_limit){
$form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';
}

$htmo .= '
<p>'.$feedback.'</p>
<form method="post" enctype="multipart/form-data">
'.$form_img.' <br />
<input type="submit" value="Upload Images!" style="margin-left: 50px;" />
</form>
';

echo $htmo;

rocknbil

3:52 pm on Jul 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is your upload process first? You have it after resizing here, guessing it would have to be first, if not, it should be . . .

I don't have the time ATM to fully code it out and test, but think I see what's happening:

$thumbnail_width= 315;
$thumbnail_height= 315;

......

if( $original_width > $original_height ){
....... etc.

Your resize is pretty standard - find out what's largest, with or height, and size that to the maximum of either, but then you force whatever results from that into a 315 x 315.

What you need to do is determine what's smaller, width or height, size that to 315, then apply the crop to it. The surplus of the other dimension, W or H, will get cropped off.

if( $original_width >= $original_height ){
$new_height= $thumbnail_height;
$new_width= intval($original_width*$new_height/$original_height);
} else {
$new_width = $thumbnail_width;
$new_height= intval($original_height*$new_width/$original_width);
}

See that? Just switched them around. Or more simply, just change this

if( $original_width <= $original_height ){

But important to note, >= or <=. If they are equal, nothing will be set for these and you'll get an undefined variable error.

archker

8:01 am on Jul 7, 2010 (gmt 0)

10+ Year Member



Hi!

Great thanks that worked perfect! I've never been good at math ;)